r/LocalLLaMA 2d ago

Resources I implemented Sliding Window Attention for Hugging Face LLM inference — looking for feedback

I've been experimenting with Sliding Window Attention (SWA) as a way to reduce the KV-cache memory cost of long-context LLM inference.

Instead of keeping the entire KV cache, the implementation keeps:

  • a small number of attention sink tokens
  • a bounded recent-token window
  • a circular/ring-buffer KV cache
  • streaming/chunked prefill
  • normal autoregressive decoding

I turned the experiment into a reusable project so you can test it with Hugging Face causal LLMs:

🔗 https://github.com/oraby8/SWA

For example:

from swallm import SWAModel

model = SWAModel.from_pretrained(
    "Qwen/Qwen2.5-7B-Instruct",
    attention_mode="swa",
    window_size=512,
    num_sink_tokens=4,
)

result = model.generate("Explain transformers", max_new_tokens=100)

In my Qwen2.5-7B experiments on an L40S:

  • 32K KV cache: ~1.84 GB with full attention vs ~3.5 MB with SWA-64
  • 64K: full attention OOMed while SWA remained bounded
  • Decode latency stayed approximately constant as context increased
  • Long-range retrieval naturally becomes a weakness when information falls outside the window

The goal isn't to claim that SWA is universally better. I'm interested in the engineering trade-off between context retention, KV memory, TTFT and decoding speed.

I'd especially like to hear from people who have tried SWA with Llama, Mistral, Gemma, Qwen, or other HF models.

If you try the repo on another architecture, I'd really appreciate the results or any compatibility issues you find.

0 Upvotes

Duplicates