Repository navigation
[2/6] refactor(draft): extract GQA/MLA attention into reusable layers - #1026
Open
maocheng23 wants to merge 1 commit into
Open
maocheng23 wants to merge 1 commit into
maocheng23 wants to merge 1 commit into
Conversation
maocheng23
requested review from
FlamingoPg,
FrankLeeeee,
shuaills and
sleepcoo
as code owners
October 27, 2026 20:42
This was referenced Oct 27, 2026
Split from #1023. DFlash-family attention moves into specforge/modeling/draft/layers: - attention/base.py: Attention/AttentionContext/AttentionOutput contract and DenseAttention (projections + cache update + backend dispatch + o_proj) - attention/{gqa,mla,rotary}.py: GQA/MHA and expanded-MLA projections, RoPE - attention/backends/dense.py: eager/SDPA/Flash/Flex dispatch, unchanged from Qwen3DFlashAttentionBase.forward - config.py (LayerKernels, AttentionConfig), factory.py (build_attention, build_ffn), decoder.py (pre-norm DecoderLayer) Qwen3DFlashAttention/Qwen3DFlashMLAAttention remain as compatibility constructors; DFlashKernels aliases LayerKernels. Mask handling is unchanged. The parity test embeds the pre-refactor classes verbatim and checks outputs, gradients and two AdamW steps bit for bit across DFlash/DFlash2/DSpark/Domino, GQA/MLA, eager/SDPA, sliding and causal layouts, plus cached block decoding. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
maocheng23
force-pushed
the
maocheng/pr1023-2-attention
branch
from
October 27, 2026 20:45
1edd72f to
e237fc8
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This PR makes DFlash-family attention reusable: GQA/MLA become components that any draft can build. Behavior must not change.
Modifications
layers/attention/base.py:Attention[State]contract,AttentionContext(position embeddings, mask, KV prefix, cache position) andAttentionOutput.DenseAttention, which holds the shared execution formerly inQwen3DFlashAttentionBase.forward: KV-cache update, backend dispatch, zeroing of empty queries, ando_proj.layers/attention/{gqa,mla,rotary}.py: the GQA/MHA and expanded-MLA projections and RoPE, moved out ofdflash.py.layers/attention/backends/dense.py: eager/SDPA/Flash/Flex dispatch, unchanged from main. Masks are used as supplied, and eager's boolean→additive conversion is kept.layers/config.py(LayerKernels,AttentionConfig),layers/factory.py(build_attention,build_ffn) andlayers/decoder.py(a pre-normDecoderLayer).DecoderLayer. DFlash2 keeps its convolution placement and calls attention through the new context.Qwen3DFlashAttentionandQwen3DFlashMLAAttentionremain as compatibility constructors.DFlashKernelsis an alias ofLayerKernels.Differences from the corresponding part of #1023: the mask-handling changes (causal/window applied by logical position, SDPA/Flash zeroing of empty queries, Flex LSE) are moved to the visibility PR. This PR's numerics are therefore exactly main's.
Accuracy Test
tests/test_modeling/test_layers/test_refactor_parity.pyembeds the pre-refactorQwen3DFlashAttentionBase, GQA/MLA attention and DFlash/DFlash2 decoder forwards verbatim from e3ab28d. It swaps them into a copy of each model and requires zero-tolerance equality on:is_causal=Truelayoutsattention_bias=True, with a dropped draft blockDynamicCacheA mutation check confirms the test isn't vacuous. Each of these fails it:
tests/test_modelingon CPU: 169 tests, 23 skipped. The only errors are the 4sglang/yunchangimport errors described in the move PR.Stack
This is part of a split of #1023 (by @jiapingW) into reviewable layers. Each PR targets the branch of the previous one, and only its own commits show in the diff.
The code is @jiapingW's from #1023 (commits are authored by them). The split adds a parity test, legacy import shims, and two fixes in the visibility PR, each noted in the PR it belongs to.
🤖 Generated with Claude Code