TL;DR
Converting standard Transformers into hybrid attention models (combining full and linear attention) dramatically improves long-context efficiency, but choosing which layers to keep is a massive combinatorial challenge. This paper introduces FlashMorph, a method that replaces slow, isolated layer-scoring heuristics with a global, joint optimization framework using learnable gates and synthetic retrieval data. FlashMorph discovers superior hybrid architectures while slashing the compute cost of layer selection by orders of magnitude, requiring only 20 million tokens.
What Problem Does This Paper Address?
Standard Transformer models scale quadratically ($O(T^2)$) with sequence length due to softmax attention. This creates a massive computational bottleneck for long-context applications and causes the inference Key-Value (KV) cache to grow linearly, rapidly consuming GPU memory. Linear attention variants offer linear scaling ($O(T)$) and constant cache sizes, but they often struggle with complex, long-range information recall.
Hybrid attention architectures bridge this gap by keeping full attention in a few critical layers and replacing the rest with efficient linear attention. However, finding the optimal subset of layers to retain is a daunting combinatorial problem ($\binom{L}{K}$ possible configurations for an $L$-layer model with a budget of $K$ full-attention layers). Existing methods rely on rigid heuristics (like uniform interleaving) or evaluate each layer in isolation, completely ignoring how layers interact, complement, or duplicate each other globally.
Methods
FlashMorph reformulates hybrid layer selection as a budget-constrained, continuous subset optimization problem. The pipeline consists of four main stages:
- Morphable Layer Construction: Each full-attention layer in a pretrained Transformer is paired with a converted linear-attention branch trained via hidden-state alignment to mimic the original layer's outputs.
- Layer Selection via Joint Optimization: The weights of both the backbone and the linear branches are frozen. FlashMorph introduces a learnable scalar gate $\alpha^{(l)} \in [0,1]$ for each layer $l$ to continuously interpolate between the full-attention and linear-attention branches.
- Linearization Regularization: The gates are optimized jointly on synthetic long-context retrieval data. The loss function balances hidden-state alignment with a regularization term ($\lambda \sum \alpha^{(l)}$) that penalizes reliance on full attention, forcing the model to favor linear attention wherever safely possible.
- Discretization & Refinement: The learned gates are discretized by selecting the top-$K$ layers with the largest gate values to retain full attention. The remaining layers are fully converted to linear attention, followed by standard logits distillation and long-context finetuning.
Datasets
- DCLM Corpus: Used as the source text for the initial hidden-state alignment, distillation, and long-context finetuning stages.
- Synthetic Retrieval Data: Created specifically for the layer selection stage to provide a dense long-range signal. It inserts ten randomly generated passkeys at various depths within long-context documents, requiring the model to recall all of them at the end of the sequence.
- Evaluation Benchmarks: Long-context retrieval was evaluated using Needle-in-a-Haystack (NIAH) and RULER. General capabilities were tested across zero-shot commonsense reasoning tasks (ARC-e, ARC-c, PIQA, HellaSwag, WinoGrande) and real-world recall-intensive tasks (SQuAD, FDA, SWDE).
Key Insights and Ideas
- Capturing Inter-layer Dependencies: Layer importance is not an isolated property. Converting two layers that perform redundant functions might cause minimal damage, but converting two complementary layers could ruin performance. FlashMorph's joint optimization naturally captures these inter-layer dynamics.
- Targeted Supervision over Generic LM: Standard language modeling objectives are heavily dominated by local token dependencies, making them poor signals for identifying long-context-critical layers. Dense, synthetic passkey retrieval provides a highly targeted signal specifically for long-range information routing.
- Efficiency via Parameter Freezing: By freezing the heavy backbone weights and only optimizing a single scalar gate per layer, FlashMorph reduces the search space drastically, making architecture search incredibly cheap and scalable to large models.
Key Results
- Drastic Cost Reduction: FlashMorph slashes layer selection costs down to just 20M tokens and 2.1 GPU hours for a 1.7B model. This is a massive reduction compared to prior data-driven methods like PostNAS (50B tokens) and HALO (234M tokens).
- Superior Long-Context Retrieval: On the Needle-in-a-Haystack task, FlashMorph maintains near-perfect accuracy up to 256K context lengths on 0.6B and 1.7B backbones, outperforming or matching much more expensive baselines.
- Downstream Task Preservation: FlashMorph preserves strong zero-shot commonsense reasoning performance and achieves the highest recall averages on tasks like SQuAD and SWDE under a standard 3:1 linear-to-full attention ratio.
- Inference Speedups: The resulting hybrid model achieves up to a 2.81× prefilling speedup at 256K tokens and a 2.07× decoding speedup at 512K tokens compared to a pure-attention Qwen3-1.7B baseline, while avoiding out-of-memory errors at extreme lengths.
Limitations and Future Work
While FlashMorph demonstrates excellent robustness across various hybrid ratios (e.g., 6:1, 3:1, 1:1), the choice of the regularization hyperparameter $\lambda$ requires manual tuning depending on the target budget. Additionally, the authors note that while they focused primarily on linear attention variants (like Lightning Attention, GLA, and Gated DeltaNet), future work could explore extending this joint optimization framework to other efficient sequence mixers, such as state-space models (SSMs) like Mamba.
Summary updated