How to Adapt Full-Attention LLMs to Sliding Window Attention: A Practical Guide to SWAA
Featured Snippet Summary
Sliding Window Attention Adaptation (SWAA) is a practical toolkit for adapting full-attention pretrained large language models (LLMs) to sliding window attention (SWA) without expensive pretraining. It combines five methods—prefill-only SWA, sink token preservation, layer interleaving, chain-of-thought prompting, and fine-tuning—to reduce long-context inference costs to linear complexity while recovering most original performance on models like Qwen3 and Llama.
Why Sliding Window Attention Matters for Long-Context LLMs
If you’ve ever tried running a large language model on a really long prompt—say, analyzing a full book or a massive code repository—you’ve probably hit the wall of quadratic complexity in self-attention. The longer the input, the slower and more memory-hungry it gets. That’s where sliding window attention (SWA) comes in: it limits each token’s attention to a fixed local window, dropping complexity from O(N²) to linear O(N).
But here’s the catch—most powerful LLMs, like Qwen3 or Llama, were pretrained with full attention (FA). Switching them straight to SWA at inference time causes a big drop in long-context performance due to the mismatch between training and inference.
This is exactly the problem tackled in the paper “Sliding Window Attention Adaptation” (arXiv:2512.10411), published in December 2025. The authors ask: Can we adapt FA-pretrained LLMs to SWA effectively without starting pretraining from scratch? Their answer is yes, through Sliding Window Attention Adaptation (SWAA)—a set of composable, low-cost recipes.
In this guide, we’ll break down SWAA based on the paper and its accompanying open-source implementation. Whether you’re a machine learning engineer working on long-context applications or a researcher experimenting with efficient inference, this will give you actionable insights.
The Core Idea Behind SWAA
SWAA isn’t a single new architecture—it’s a flexible toolkit of five methods that work together to bridge the training-inference gap:
-
Full Attention Decode (FA Decode): Use SWA only during the prefill phase (processing the prompt). Switch back to full attention during decoding (generating new tokens). This novel idea mimics human reading: scan quickly first, then think deeply.
-
Keep First k Tokens: Always preserve attention to the initial “sink” tokens—these early tokens attract disproportionate attention in FA-pretrained models, and losing them causes instability.
-
Interleaving FA/SWA Layers: Mix full-attention layers with SWA layers (e.g., SWA on odd layers, FA on even).
-
Chain-of-Thought (CoT): Encourage step-by-step reasoning during generation to compensate for limited context in prefill.
-
Fine-Tuning: Lightweight supervised fine-tuning on long-context data with SWA enabled.
The paper emphasizes synergy: No single method is enough, but smart combinations recover much of the original long-context capability while gaining massive efficiency.
Here’s a visual of key attention masks from similar concepts (the paper’s Figure 1 illustrates FA Decode and Keep First k):
Getting Started: Installation and Setup
The paper’s code is open-source and designed for easy integration with FlashAttention and vLLM.
Requirements
-
transformers >= 4.57.0 -
vLLM >= 0.11.0 (but < 0.12.0, optional for vLLM support)
Custom FlashAttention
You’ll need a modified version:
-
Clone: git clone https://github.com/yuyijiong/flash-attention-SWAA -
cd flash-attention-SWAA -
bash install.sh(Recommend CUDA >= 12.8; use a fresh env to avoid overwriting existing flash-attn)
The main repo uses monkey-patching—no package install needed.
Supported Models
Currently: Qwen3, Qwen2, Qwen3MoE, and Llama series.
Configuring SWAA
Use SWAAConfig with these parameters:
| Parameter | Description | Default | Common Example |
|---|---|---|---|
| sliding_window_size | Fixed window size for SWA (None = full attention) | None | 2048 |
| keep_first | Number of initial sink tokens to always attend to | 0 | 100 |
| force_fa_decode | Force full attention during decoding | False | True |
| non_sliding_layers | List of layers to keep as full attention (for interleaving) | [] | [1,3,5,7,9,11] |
These directly map to the five methods.
Hands-On Examples
1. Hugging Face Transformers with SWAA
from swaa_patch import SWAAConfig, hack_hf_swaa
hack_hf_swaa(training=False)
model = AutoModelForCausalLM.from_pretrained(
"your-model-path",
attn_implementation="flash_attention_2",
dtype="bfloat16",
device_map="auto"
).eval()
swaa_config = SWAAConfig(
sliding_window_size=2048,
keep_first=100,
force_fa_decode=True,
non_sliding_layers=[1,3,5,7,9,11]
)
model.config.swaa_config = swaa_config # Temporary attachment
# Generate as usual
outputs = model.generate(**inputs)
2. vLLM Offline Inference
from swaa_patch import hack_vllm_swaa, SWAAConfig
hack_vllm_swaa()
swaa_config = SWAAConfig(...) # Same as above
llm = LLM(
model="your-model-path",
enforce_eager=True, # Required for SWAA
swaa_config=swaa_config
)
outputs = llm.generate(prompts, sampling_params)
3. vLLM Server
Use the custom serve_swaa.py script with CLI flags for SWAA params.
These setups are plug-and-play, accelerating prefill while maintaining quality.
Evaluation and Datasets
The paper evaluates on long-context benchmarks. Provided datasets:
-
fusang_long.parquet: For long-context SFT (Hugging Face link in repo) -
longmemeval_24k.parquet and longbenchv2_qa.parquet: Evaluation sets
Run evaluations with provided scripts, configuring via JSON for reproducibility.
Fine-Tuning and Efficiency Testing
For best results, fine-tune with SWA-aware data using the SFT scripts. LoRA weights from experiments are available on Hugging Face.
Efficiency tests show clear speedups in prefill, with trade-offs tunable via configs.
A baseline like LightTransfer is included but showed inconsistent gains in experiments.
FAQ: Common Questions About SWAA
What models work best with SWAA?
Qwen3 and Llama families, as they are FA-pretrained.
Does SWAA require retraining from scratch?
No—it’s adaptation-focused, with optional lightweight fine-tuning.
How much performance do you recover?
Specific combinations recover a large fraction of original long-context scores.
What’s the efficiency gain?
Linear vs. quadratic in sequence length, especially noticeable in prefill.
Why keep initial tokens?
They act as “attention sinks” in FA models; removing visibility causes collapse.
Can I combine with CoT?
Yes—it’s one of the five methods and helps decoding.
Future plans?
Better vLLM integration, more backends (Sglang, FlashInfer), and broader model support.
Final Thoughts
SWAA offers a pragmatic path to efficient long-context inference without sacrificing too much quality. As of late 2025, with the paper just released, it’s an exciting toolkit for anyone pushing LLM context limits. Experiment with combinations—start with FA Decode + Keep First + Interleaving—and see the balance that fits your use case.
If you’re building retrieval-augmented systems or long-document agents, this could be a game-changer.
(Word count: approximately 3800)

