Skip to content

Fused MLA override - #4134

Merged
SherlockNoMad merged 8 commits into
mainfrom
fused-mla-override
Aug 18, 2026
Merged

SherlockNoMad merged 8 commits into
mainfrom
fused-mla-override

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Fused DeepSeek-V3 MLA

Summary

Adds an opt-in fused MLA implementation for TorchTitan's DeepSeek-V3 attention.

The override:

  • Applies Q RoPE in place, avoiding Q concatenation.
  • Fuses K RoPE, head expansion, and K materialization.
  • Keeps V as a zero-copy view.
  • Fuses KV-gradient packing, positional-gradient reduction, and inverse RoPE.
  • Exposes the kernels as torch.library custom operators so they remain visible
    to GraphTrainer and make_fx.
  • Uses 64-bit indexing for production DeepSeek-V3 tensor sizes.

The core fusion strategy is inspired by Megatron Core's fused MLA design. This
is an independent TorchTitan implementation using BSHD layouts, ComplexRoPE,
explicit position IDs, and GraphTrainer-compatible custom operators; no
Megatron Core or Transformer Engine kernel code is copied.

Performance

Measured on one NVIDIA GB300 with BF16 tensors, B=16, L=4096, H=128.

MLA assembly microbenchmark

The activation-checkpointing workload is two forwards plus one backward
(2F + 1B).

Operation Stock eager Tuned fused Reduction
Forward 19.160 ms 1.276 ms 93.3%
Backward 15.579 ms 1.949 ms 87.5%
2F + 1B 53.898 ms 4.501 ms 91.6%

The latest head-tiling tuning also makes the fused boundary 3.82x faster
than the initial fused implementation.

DeepSeek-V3 671B GraphTrainer

Configuration:

  • Virtual FSDP degree: 256
  • Expert parallel degree: 64
  • Local/global batch: 16 / 4096
  • Sequence length: 4096
  • Gradient accumulation: 1
  • GraphTrainer fake communication backend
  • AOT FX graph with selective rematerialization and FSDP overlap scheduling
Metric Stock Fused MLA Change
Throughput 2,060 tok/s 2,277 tok/s +10.53%
Model TFLOP/s 579.30 640.32 +10.53%
MFU 23.17% 25.61% +2.44 pp
GPU kernel span 31.658 s 28.805 s -9.01%
Active-step memory 182.46 GiB 180.55 GiB -1.91 GiB

Both graphs retain the same 361 FSDP overlap dependencies.

In the profiled window, the targeted 4-D MLA CatArrayBatchedCopy kernel
accounts for 309 launches and 2.601 seconds in the stock run. The fused run
removes all of those launches; its three MLA kernels consume approximately
273 ms in total.

Overall CUDA kernel time falls from 29.954 seconds to 26.906 seconds.

Validation

pytest -q tests/unit_tests/test_fused_mla_override.py

Result: 10 passed.

Coverage includes:

  • BF16 and FP32 forward/backward parity
  • Exact copying of Q-NoPE, K-NoPE, V, and packed KV gradients
  • Module-level attention parity
  • Storage and aliasing behavior
  • Position broadcasting
  • State-dict compatibility
  • Custom-op visibility under fake tensors and make_fx

The full trainer's step-1 loss and gradient norm match the stock implementation.

Formatting, lint, spelling, documentation, type, and Python compilation checks
also pass.

Scope and limitations

  • The override currently supports DeepSeek-V3 MLA with ComplexRoPE.
  • CosSinRoPE, MRoPE, and non-MLA attention modules are not supported.
  • The E2E benchmark uses one physical GB300 with fake 256-rank communication.
    It measures full local graph execution and scheduling, but not real multi-node
    networking or communication contention.
  • CUDA graph conversion was skipped for both compared variants.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 13, 2026
@SherlockNoMad
SherlockNoMad marked this pull request as ready for review August 13, 2026 20:33

Activate with::

--override.imports torchtitan.overrides.fused_mla.fused_mla

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add this to the existing deepseek CI
https://github.com/pytorch/torchtitan/blob/main/tests/integration_tests/models.py#L100

or if you want to guard it on cudagraph, need to create a new one without EP

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added test with override. I am not too worry about the cudagraph.

"--override.imports torchtitan.overrides.fused_mla.fused_mla,"
"torchtitan.overrides.fused_swiglu.fused_swiglu",
"--parallelism.data_parallel_shard_degree 4",
"--parallelism.expert_parallel_degree 2",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default EP backend won't work with cudagraph which is by default turned on

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to MinimalAsyncEP so it's cudagraphable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah... A100 Ci machine doesn't support MinimalAsyncEP, so I am reverting back and disable cudagraph.

@sanketpurandare sanketpurandare left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Add cudagraphable integration test with minimal async ep instead of default ep backend
  2. tuned configs are great but you can pull all the kernel configs in a dataclass with defaults set to the tuned ones that would be great, would help others using MLA. Also ask the agent to insert a one line comment explaining the config.

@SherlockNoMad SherlockNoMad added the ciflow/h100.8 Trigger H100.8 CI label Aug 18, 2026
@SherlockNoMad
SherlockNoMad merged commit db1ce7f into main Aug 18, 2026
23 of 25 checks passed
@SherlockNoMad
SherlockNoMad deleted the fused-mla-override branch September 2, 2026 23:27
felipemello1 added a commit to felipemello1/torchtitan that referenced this pull request Oct 3, 2026
The stock DeepSeek-V3 attention assembles q and k with eager cats (and
their split-backward cats), which run as non-vectorized
CatArrayBatchedCopy kernels. The opt-in fused_mla override (pytorch#4134)
replaces that glue with Triton kernels and is bitwise equal to the stock
module. Add deepseek_v3_16b_perf and deepseek_v3_671b_perf recipes that
request it, as the RL _perf recipes do for fused_swiglu, so the base
recipes keep the stock path (overrides stay opt-in, OVERRIDE.md). Document
the new recipes in the DeepSeek-V3 README and the override's docstring,
and test that the perf recipes fuse every attention while the base
recipes do not.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants