[KongchangAI]
· 2 min read· 1,469 words

FlashMorph2: Efficiently "Morphing" Pretrained Transformers into Hybrid Attention Models

FlashMorph2: Efficiently "Morphing" Pretrained Transformers into Hybrid Attention Models

FlashMorph2 improves hybrid attention layer selection with on-policy end-to-end evaluation and joint optimization.

FlashMorph2 addresses two off-policy mismatches in the previous FlashMorph's hybrid attention layer selection: training-inference distribution mismatch and frozen-weight vs. post-recovery weight mismatch. It directly samples discrete hybrid architectures, evaluates them end-to-end with their own hidden states, and jointly optimizes layer selection scores and weights under the recovery objective. On Qwen3 and linear attention variants, it improves RULER by +2.42, LongBench by +1.08, and recall-intensive tasks by +0.76, with layer selection taking only 0.72–3.57 GPU hours at 0.6B–8B scale.

Hybrid attention models combine full attention with linear attention to achieve a better balance between quality and efficiency. But when converting a pretrained Transformer into this structure, the key question is: under a given budget, which layers should retain full attention? The latest research, FlashMorph2, proposes an "on-policy" layer selection method that claims to more accurately reflect the model's true performance after conversion, and outperforms the previous generation method on multiple benchmarks.

Why Is Layer Selection So Hard?

Replacing full attention layers with linear attention layers can significantly reduce the computational overhead of long sequences, but not all layers are suitable for replacement. Although the existing FlashMorph method can automatically search for layer configurations, it suffers from two "off-policy" mismatches.

First, training uses continuous branch interpolation and teacher-forced hidden states, whereas inference uses a discrete hybrid architecture and the model's own generated hidden states—the two distributions are inconsistent. Second, during layer selection the model weights are frozen, but after selection the model undergoes recovery training, which changes the weights; the layers selected under frozen weights may not be optimal after recovery.

These two problems compound, causing the selected architecture to underperform in actual deployment. FlashMorph2's core idea is to pull layer selection from "offline evaluation" back to "online evaluation."

The Essential Difference Between Full Attention and Linear Attention: Standard full attention (softmax attention) needs to compute the similarity between every pair of tokens in the sequence, with O(n²) complexity, so memory and compute costs rise sharply on long sequences; linear attention instead reduces complexity to O(n) through kernel function approximation or recurrent state updates, greatly lowering the cost of long-context inference. But the price is that linear attention's compression of historical information is lossy, especially on tasks that require precise retrieval of content at specific positions (i.e., recall-intensive tasks), where it performs worse than full attention. The idea of hybrid attention is precisely to combine the two layer by layer: retain a small number of full attention layers responsible for precise retrieval, and use linear attention for the remaining layers to handle the bulk of context modeling, thereby balancing quality and efficiency.

Teacher Forcing and Exposure Bias: Teacher forcing means using the real preceding token (or real hidden state) as the next step's input during training, rather than the model's own generated output. This approach trains stably and converges quickly, but it creates a distribution mismatch between training and inference—at inference time the model can only rely on hidden states it produces itself, which may carry errors. This phenomenon is known in sequence modeling as exposure bias. In the layer selection scenario, evaluating a layer's importance using teacher-forced states systematically overestimates that layer's contribution during real autoregressive operation, because errors accumulate layer by layer in real execution.

Two Key Changes in FlashMorph2

End-to-End Evaluation of Discrete Architectures

FlashMorph2 no longer relies on continuous relaxation to approximate discrete choices, but instead directly samples discrete hybrid architectures and performs end-to-end evaluation using the architecture's own hidden states. In this way, the data distribution seen during layer selection matches the distribution at final inference, eliminating the first mismatch.

Joint Optimization of Layer Selection and Weights

To address the second mismatch, FlashMorph2 jointly optimizes the layer selection scores and model weights under the same recovery objective. That is, layer selection is no longer a one-time static decision but is coupled with subsequent recovery training, so the selected layers can reflect performance "after adaptation."

These two changes sound simple, but implementation requires simultaneously maintaining architecture sampling and weight updates, and computational cost is a design challenge. The paper reports that on model scales from 0.6B to 8B, completing layer selection requires only 0.72 to 3.57 GPU hours, indicating that the method remains efficient and controllable.

Trade-off Between Continuous Relaxation and Discrete Sampling: The previous FlashMorph method, for differentiability, used continuous relaxation—relaxing the discrete decision of "select or not select full attention" into continuous weights between 0 and 1, using branch interpolation so gradients can flow back. This allows end-to-end optimization of the search, but the optimal continuous solution, after rounding (e.g., thresholding) into a discrete architecture, often does not match performance under continuous evaluation. FlashMorph2 instead directly samples discrete architectures from a probability distribution (e.g., Gumbel-Softmax sampling or policy gradients), then computes forward passes with the sampled real architecture, so the evaluation signal is strictly aligned with the final deployed form; the cost is higher variance in gradient estimates and the need for more sampling steps to control noise.

Recovery Training: After replacing some layers with linear attention, there is a mismatch between the original pretrained weights and the new structure, requiring a period of lightweight continued training to let the model re-adapt; this process is recovery training. Layer selection occurs before recovery training, but final performance is determined after recovery training, so jointly optimizing layer selection scores and weight updates under the same objective essentially merges "architecture search" and "weight adaptation" into an alternating optimization problem: fix the scores and update weights, then fix the weights and update scores, repeating for several steps.

Experimental Performance: Stable Improvements Across Multiple Benchmarks

The research conducted experiments on the Qwen3 series models and various linear attention variants. Compared with FlashMorph, FlashMorph2 improves on average by +2.42 points on RULER, +1.08 points on LongBench, and +0.76 points on recall-intensive tasks.

These improvements are not aggressive, but their strength lies in "consistency"—they stably outperform existing layer selection methods across model scales and linear attention variants. For a method that mainly affects the architecture search stage, this stability is more meaningful than one-off high scores.

You may not have noticed that the improvement on recall-intensive tasks is the smallest (+0.76), which is also reasonable: linear attention is naturally weaker than full attention on precise recall, and layer selection can only alleviate, not eliminate, this gap.

FlashMorph2 paper source screenshot

RULER and LongBench: RULER is a synthetic benchmark suite for long-context capability, systematically measuring a model's effective context length by controllably varying sequence length and task types (e.g., multi-needle retrieval, variable tracking, aggregation statistics), exposing information loss on long sequences better than a single perplexity metric. LongBench is a collection of real long-document tasks, covering question answering, summarization, code completion, etc., and is closer to real application distributions. Together, the two can distinguish "degradation under synthetic stress tests" from "degradation on real tasks." Recall-intensive tasks (such as precise position recall in multi-needle retrieval) are the least friendly to linear attention because its fixed-size recurrent state struggles to preserve all historical details losslessly, which also explains why the improvement in this dimension is the smallest.

Implications for Engineering Practice

For teams looking to reduce the deployment cost of long-context models, FlashMorph2 offers an actionable path: instead of training a hybrid attention model from scratch, "morph" an existing pretrained Transformer into one.

Several practical points worth noting:

  • Layer selection is not a one-time job: If further fine-tuning is planned, layer selection should be tied to the fine-tuning objective; otherwise the selected configuration may quickly become outdated.
  • Evaluation must use the model's own hidden states: Evaluation under teacher forcing overestimates the value of certain layers, especially on long-sequence tasks.
  • Trade-offs under budget constraints: How many full attention layers to retain directly determines the balance point between efficiency and quality, and FlashMorph2's value lies in making this trade-off more predictable.

The paper currently focuses on Qwen3 and several linear attention variants, and whether the method can transfer to other architectures (such as MoE or combinations of mixture-of-experts with linear attention) remains to be verified. In addition, the training stability issues brought by joint optimization are not elaborated in the paper's abstract, and actual reproduction may require additional tuning.

Conclusion

FlashMorph2's contribution is not in proposing a new attention mechanism, but in making "architecture conversion" more honest: evaluating architectures using the model's true runtime states, and guiding selection with post-adaptation performance. For the hybrid attention technical route, this is a necessary engineering piece.

One-line Summary

FlashMorph2 uses on-policy end-to-end evaluation and joint optimization to improve hybrid attention layer selection.

Paragraph Summary

FlashMorph2 addresses two off-policy mismatches in the previous FlashMorph's hybrid attention layer selection: first, the distribution mismatch between training with continuous interpolation and teacher-forced hidden states versus inference with discrete architectures and self-generated states; second, the mismatch where weights are frozen during layer selection but change after recovery training, making the selected layers suboptimal. The method directly samples discrete hybrid architectures and performs end-to-end evaluation using their own hidden states, while jointly optimizing layer selection scores and model weights under the recovery objective. On the Qwen3 series and various linear attention variants, it improves on average by +2.42 points on RULER, +1.08 points on LongBench, and +0.76 points on recall-intensive tasks, with layer selection requiring only 0.72–3.57 GPU hours at 0.6B–8B scale.

#FlashMorph2#hybrid attention#linear attention#layer selection#Transformer#long context#on-policy#RULER#LongBench#Qwen3
Share:

Related articles