Applying Sliding Window Attention to pretrained LLMs at inference time [P]
Developer ahsoar8 has implemented a sliding window attention (SWA) solution for the pre-trained Hugging Face causal language model, aimed at restricting access to generated tokens to the entire historical KV cache. This implementation serves as a reusable inference layer, including bounded KV cache, circular buffer storage, and streaming prefixes, without the need to modify or retrain the model. In the Qwen2.5-7B experiments, SWA-64 reduced the TPOT time under 16K context from approximately 38.4 ms to 30.5 ms, and significantly decreased the KV cache memory usage from 1.84 GB to 3.5 MB. However, this solution involves trade-offs: if the task requires information beyond the active window, performance may decline. The author is further studying whether this phenomenon is inherent to SWA or a specific behavior of the implementation/model, and seeking community feedback to optimize the verification architecture, fault cases, and integration with existing HF inference workflows.