Add fused_mla_lora_proj config flag to fuse MLA LoRA up-projections#3424
Open
abhinavgoel95 wants to merge 1 commit intoAI-Hypercomputer:mainfrom
Open
Add fused_mla_lora_proj config flag to fuse MLA LoRA up-projections#3424abhinavgoel95 wants to merge 1 commit intoAI-Hypercomputer:mainfrom
abhinavgoel95 wants to merge 1 commit intoAI-Hypercomputer:mainfrom
Conversation
gobbleturk
approved these changes
Mar 16, 2026
8e0fcfb to
be3169b
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
89a0056 to
14328b4
Compare
Fuses the Q and KV LoRA up-projections (wq_a + wkv_a) into a single matmul (wq_kv_a: emb → q_lora_rank + kv_lora_rank + rope_head_dim), halving the number of kernel launches for the LoRA up-projection step. Enabled via fused_mla_lora_proj: True (requires q_lora_rank > 0 and attention_type=mla). Modeled after the existing fused_qkv flag. Includes a unit test verifying that fused and unfused paths produce numerically identical outputs given equivalent weights.
14328b4 to
3b46b3c
Compare
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.
Add a
fused_mla_lora_projboolean config (default False) that fuses the separate wq_a (emb→q_lora_rank) and wkv_a (emb→kv_lora_rank+rope_head_dim) MLA LoRA up-projection matmuls into a single wq_kv_a matmul (emb→q_lora_rank+kv_lora_rank+rope_head_dim), followed by a split.This halves the number of matmul kernel launches for the LoRA up-projection step. The flag is modelled after the existing
fused_qkvconfig and requiresattention_type='mla'andq_lora_rank > 0.Note: wq_kv_a uses a different weight name than wq_a/wkv_a, so checkpoints are not cross-compatible between fused and unfused modes.
Ends up with 3% speedup in MLA computation on GB200 GPUs.
Description
Add a
fused_mla_lora_projboolean config flag (defaultFalse) that fusesthe two separate MLA LoRA up-projection matmuls —
wq_a(emb → q_lora_rank)and
wkv_a(emb → kv_lora_rank + rope_head_dim) — into a singlewq_kv_amatmul (
emb → q_lora_rank + kv_lora_rank + rope_head_dim), followed by ajnp.split. This halves the number of matmul kernel launches for the LoRAup-projection step when
q_lora_rank > 0, which is the common case for MLAmodels like DeepSeek-V3.
The flag is modelled after the existing
fused_qkvconfig:configs/base.yml: defaultFalseconfigs/types.py: field definition + validation (requiresattention_type='mla'and
q_lora_rank > 0)layers/attention_mla.py: newelif self.config.fused_mla_lora_projbranch in_init_projectionsinitialiseswq_kv_ainstead ofwq_a/wkv_a; fuseddispatch in
__call__; passthrough guards inmla_query_projectionandmla_kv_projectionShortcoming / compatibility note:
wq_kv_auses a different weight name thanwq_a/wkv_a, so checkpoints are not cross-compatible betweenfused_mla_lora_proj=TrueandFalse. A future improvement would be acheckpoint conversion utility.
Tests
Tested via config validation:
fused_mla_lora_proj=True, q_lora_rank=0raises ValueErrorfused_mla_lora_proj=True, attention_type='global'raises ValueErrorEnd-to-end numerical equivalence between fused and unfused paths with a
DeepSeek-V3 config (
q_lora_rank=1536, kv_lora_rank=512, qk_rope_head_dim=64) has been verified.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.