Papers
Topics
Authors
Recent
Search
2000 character limit reached

Interleaving FA/SWA Layers in Transformers

Updated 14 December 2025
  • Interleaving FA/SWA layers is a hybrid technique that alternates full causal and sliding-window attention to balance global context and local efficiency.
  • This approach reduces compute complexity by selectively applying sliding-window attention, thereby cutting quadratic costs while preserving long-range signal propagation.
  • Empirical results demonstrate nearly 90% recovery of baseline accuracy on long-context tasks with significant memory savings using optimized FA Decode and sink token strategies.

Interleaving FA/SWA Layers constitutes a hybridization technique in Transformer-based LLMs where full causal self-attention (FA) layers are systematically interleaved with sliding-window attention (SWA) layers. This approach enables pretrained FA models to efficiently adapt to long-context inference using SWA without requiring full retraining, mitigating the accuracy loss associated with naive SWA deployment. The method strategically alternates global (FA) and local (SWA) receptive fields to maintain high accuracy on deep-context tasks, while achieving substantial reductions in memory and computational cost (Yu et al., 11 Dec 2025).

1. Motivation for Interleaving

Transformer models pretrained with full causal attention possess global context propagation at every layer. Switching all layers to local sliding-window attention with window size WW drastically reduces the attention's receptive field, leading to severe degradation in performance for tasks requiring long-range context (e.g., question answering with inputs >16K tokens). Pure SWA delivers O(NW)\mathcal{O}(N \cdot W) inference, substantially improving speed and lowering memory use, but at the cost of downstream accuracy due to the lack of long-range information flow. Interleaving FA and SWA layers serves as a compromise: maintaining a subset of layers with global context re-injects distant information that otherwise would be forgotten by SWA, thereby restoring downstream task performance while still retaining most of the efficiency gains of SWA (Yu et al., 11 Dec 2025).

2. Layer Scheduling and Algorithmic Implementation

Interleaving operates by defining a periodic pattern across the LL self-attention layers of a Transformer. Every nnth layer is designated as an FA layer, with the remainder as SWA layers. Let ll index the layers, nn the interleaving period, and offset{0,,n1}offset \in \{0, \dots, n-1\} the phase. For typical settings, n=2n=2 (half FA, half SWA) and offset=0offset=0 or $1$, resulting in interleaved patterns such as FA at layer indices [0,2,4,][0,2,4,\dots] or [1,3,5,][1,3,5,\dots]. The choice of offsetoffset is model- and calibration-dependent.

The algorithm is summarized as follows:

1
2
3
4
5
6
7
8
for layer l in 0  L1:
    if (l mod n) == offset:
        compute_attention(FA_mask)
    else:
        if keep_first_k_tokens:
            compute_attention(SWA_with_sink_mask(W, k_sink))
        else:
            compute_attention(SWA_mask(W))
In practical configurations, W{2K,4K,8K}W \in \{2K, 4K, 8K\} and ksink{0,10,100,1000}k_{sink}\in\{0,10,100,1000\} are reported (Yu et al., 11 Dec 2025).

3. Attention Masks and Signal Propagation

Attention masking determines whether a position ii in the sequence can attend to another position jj. For each block, a pre-softmax logit mask MRN×NM \in \mathbb{R}^{N \times N} is constructed as:

  • Full causal attention (FA):

Mi,jFA={0if j<i otherwiseM_{i,j}^{FA} = \begin{cases} 0 & \text{if } j < i \ -\infty & \text{otherwise} \end{cases}

  • Sliding-window attention (SWA) of window WW:

Mi,jSWA={0if iWj<i otherwiseM_{i,j}^{SWA} = \begin{cases} 0 & \text{if } i-W \leq j < i \ -\infty & \text{otherwise} \end{cases}

  • SWA with kk “sink” tokens:

Mi,jSWA_sink={0if j<k 0if iWj<i otherwiseM_{i,j}^{SWA\_sink} = \begin{cases} 0 & \text{if } j < k \ 0 & \text{if } i-W \leq j < i \ -\infty & \text{otherwise} \end{cases}

The output at position ii is then

Ai=jsoftmax(QiKjd+Mi,j)Vj.A_i = \sum_j \text{softmax}\left(\frac{Q_i K_j^\top}{\sqrt{d}} + M_{i,j}\right) V_j.

Switching MM per layer interleaves access to global and local context, thus enabling recovery of critical global signals otherwise inaccessible in pure SWA (Yu et al., 11 Dec 2025).

4. Role of Sink Tokens in Interleaving

While interleaving alone does not require special tokens, empirical adaptation strategies often combine it with the “Keep First k Tokens” approach. This ensures that in every SWA block, the first kk input tokens are always visible (“sink” tokens), thereby stabilizing attention distributions on context “hubs” and reducing catastrophic context collapse in zero-train adaptation settings. The sink tokens’ mask is implemented as a logical OR between the standard SWA mask and a mask enabling unimpeded access to the initial kk positions. When SWA-aware fine-tuning is performed, sink-token preservation is rendered optional, but in inference-only adaptation, it is essential (Yu et al., 11 Dec 2025).

5. Hyperparameter Regimes and Rationale

Empirically, several hyperparameters govern the efficacy of interleaving FA/SWA layers:

  • Window size WW: An aggressive W=2W=2K window highlights auxiliary method gains; practical trade-offs favor W=4W=4K or $8$K, as increasing WW after interleaving and FA Decode typically closes most of the performance gap.
  • Interleaving period nn: n=2n=2 (half FA/SWA) achieves the bulk of accuracy recovery while halving the quadratic cost. More aggressive sparsification (e.g., n=4n=4) further reduces compute but degrades performance.
  • Phase offset: The optimal phase (even vs. odd layers using FA) varies by architectural family (e.g., Qwen3-4B favors offset=1; Qwen3-30B and Llama3.1 the opposite), so a small calibration sweep is recommended.
  • Sink token count ksinkk_{sink}: For inference-only, k=10k=10 confers most benefit; k=100k=100 yields marginal improvement, with diminishing returns and negligible runtime cost above k=1000k=1000.
  • FA Decode: Using SWA for prefilling and reverting to FA at decode time improves performance without architecture changes, but doubles KV memory unless recomputation per turn is enabled.

A robust default is: W=4W=4K, n=2n=2, offset swept, ksink=10k_{sink}=10–$100$ (Yu et al., 11 Dec 2025).

6. Empirical Ablations and Performance Outcomes

Ablations in (Yu et al., 11 Dec 2025) on Qwen3-4B-Thinking and -Instruct with LongMemEval_24k demonstrate:

Configuration Think Acc. Non-Think Acc. Efficiency (TTFT)
Pure SWA (2K) 3.2% 11.0% 8×8\times speedup
Half Interleaved (no Keep-First/FA Decode, n=2) 13–18% 3×\approx3\times speedup
Interleaving + FA Decode 32% 26%
Interleaving + FA Decode + Keep-First k=10k=10 59.2% 34.8%
Best Zero-Train (Int. [1,3,5,…], k=100, FA Decode) 68.8% 50.6%
FA Baseline (All Layers FA) 73.0% 62.0% Baseline (slowest)

A plausible implication is that interleaving strategy, especially combined with FA Decode and light sink-token preservation, achieves nearly 90% recovery of baseline accuracy with only half the quadratic cost. With SWA-aware fine-tuning, interleaving alone suffices to approach baseline accuracy, rendering sink-token strategies largely optional (Yu et al., 11 Dec 2025). Efficiency trade-offs indicate that pure SWA achieves 8×8\times speedup, half-interleaving yields 3×3\times, and increasing WW to 4K maintains most efficiency.

7. Deployment Recommendations and System Integration

Integration of interleaving FA/SWA layers in existing serving stacks (e.g., Flash-Attention–2 or vLLM) is operationally straightforward. For inference-only adaptation without fine-tuning, the combination of FA Decode, Interleaving, and Keep-First k=10k=10–$100$ provides a strong default. When some SWA-aware fine-tuning is feasible (e.g., LoRA), interleaving with n=2n=2 and offset calibrated suffices, and Keep-First becomes unnecessary. Increasing window size (WW) improves accuracy with little adverse impact on throughput. FA Decode can also be introduced for further gains as needed.

In summary, interleaving FA/SWA layers is identified as the single most cost-effective component of Sliding Window Attention Adaptation, offering substantial memory and speed gains, and accurately restores long-context model competence with minimal modification to existing architectures (Yu et al., 11 Dec 2025).

Definition Search Book Streamline Icon: https://streamlinehq.com
References (1)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to Interleaving FA/SWA Layers.