Skip to content

kda in torchtitan (training) - #4351

Merged
liangel-02 merged 2 commits into
mainfrom
gh/liangel-02/3/head
Aug 27, 2026
Merged

liangel-02 merged 2 commits into
mainfrom
gh/liangel-02/3/head

Conversation

@liangel-02

@liangel-02 liangel-02 commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

(remaking bc i accideltny merged #4164 into my ghstack instead of main)

this PR adds attention module for KDA (relying on attention gym KDA kernels and user facing APIs)

correctness

tested with c4 dataset:
Screenshot 2026-08-24 at 3 04 05 PM

torchtitan attention gym vs hugging face (natural routing)

Full multimodal last-token logit parity (TorchTitan vs HuggingFace)
KL=7.0921e-02 cos=0.927007 max_diff=1.6885e+00 top1=N top5=20%

^i compared hugigng face and torchtitan layer by layer using identical weights and inputs, first divergence appears at block 2 where expert choice agreement was 94.5% and max router score diff was 0.0065. with nearly tied experts, small numerical differences led to different experts being in the top k which led to model executing different expert weights.

if we force expert routing to be the same, we see more alignment. i did this by first running hugging face and recording the top k experts for every token at every layer and then replacing torchtitan with those choices.

torchtitan attention gym vs hugging face (forced expert routing)

Full multimodal last-token logit parity (TorchTitan vs HuggingFace)
KL=2.1806e-04 cos=0.999769 max_diff=9.6069e-02 top1=Y top5=80%

torchtitan fla vs hugging face

Full multimodal last-token logit parity (TorchTitan vs HuggingFace)
KL=2.3385e-04 cos=0.999754 max_diff=9.7656e-02 top1=Y top5=80%

performance

Screenshot 2026-08-24 at 6 35 10 PM

^attention gym is 1.38x and uses 26% less memory

attention gym trace:
Screenshot 2026-08-24 at 6 41 17 PM

fla trace:
Screenshot 2026-08-24 at 6 41 49 PM

Stack from ghstack (oldest at bottom):

[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 27, 2026
@liangel-02
liangel-02 changed the base branch from gh/liangel-02/3/base to main August 27, 2026 17:17
@liangel-02
liangel-02 merged commit d208df8 into main Aug 27, 2026
11 checks passed
ngpu: int = 4
disabled: bool = False
skip_rocm_test: bool = False
required_cuda_capabilities: Sequence[tuple[int, int]] = ()

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.

We don't have blackwell for titan CI, so Kimi-K3 tests are going to be skipped anyway

@QIU023 QIU023 Aug 29, 2026 •

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.

Hi @wwwjn ! I have published a PR: #4374 to allow Attention Gym KDA reference impl to be called when the devices are not having SM100/103 blackwell GPUs, so the Kimi K3 titan CI can be re-activated, please let me know if it helps, thanks a lot!

JavaZeroo added a commit to JavaZeroo/torchtitan that referenced this pull request Aug 29, 2026
KDA already takes document offsets after pytorch#4351, but Kimi K3 still
rejected MMSamplePackingConfig and handed its KDA layers a hard-coded
None, so the offsets never reached them.

get_attention_masks now returns a mask dict, mirroring Qwen3.5's hybrid
layout: MLA layers read "quadratic_attention" (a BlockMask under flex,
shared offsets under varlen) and KDA layers read "kda". The block routes
each layer to its own entry, and the packing guard is gone.

The offsets test uses Attention Gym's eager reference implementation so
it runs on any CUDA device -- the fused kernel it ships alongside is
compiled for sm_100 only, which is why the kernel test beside it is
skipped here. What the test covers is the segmentation the offsets
encode, which is implementation independent: a packed row reproduces the
per-document runs bitwise, and the same row without offsets does not.
JavaZeroo added a commit to JavaZeroo/torchtitan that referenced this pull request Sep 3, 2026
KDA already takes document offsets after pytorch#4351, but Kimi K3 still
rejected MMSamplePackingConfig and handed its KDA layers a hard-coded
None, so the offsets never reached them.

get_attention_masks now returns a mask dict, mirroring Qwen3.5's hybrid
layout: MLA layers read "quadratic_attention" (a BlockMask under flex,
shared offsets under varlen) and KDA layers read "kda". The block routes
each layer to its own entry, and the packing guard is gone.

The offsets test uses Attention Gym's eager reference implementation so
it runs on any CUDA device -- the fused kernel it ships alongside is
compiled for sm_100 only, which is why the kernel test beside it is
skipped here. What the test covers is the segmentation the offsets
encode, which is implementation independent: a packed row reproduces the
per-document runs bitwise, and the same row without offsets does not.
JavaZeroo added a commit to JavaZeroo/torchtitan that referenced this pull request Sep 24, 2026
KDAKernel only accepted SM100/SM103. That gate matched attn-gym 0.0.5,
pinned when pytorch#4351 moved KDA onto Attention Gym. Since 0.0.7, chunk_kda
runs CuTe kernels on SM100/SM103 and Triton kernels on other NVIDIA
GPUs, and the pinned 0.0.10 keeps that dispatch. The remaining hardware
floor is the fused bounded gate, which needs TMA (SM90+).

- Relax the KDAKernel gate and the two KDA GPU test skips to >= (9, 0).
- Document the hardware requirement in the Kimi K3 README.
JavaZeroo added a commit to JavaZeroo/torchtitan that referenced this pull request Sep 24, 2026
KDAKernel only accepted SM100/SM103. That gate matched attn-gym 0.0.5,
pinned when pytorch#4351 moved KDA onto Attention Gym. Since 0.0.7, chunk_kda
runs CuTe kernels on SM100/SM103 and Triton kernels on other NVIDIA
GPUs, and the pinned 0.0.13 keeps that dispatch. The remaining hardware
floor is the fused bounded gate, which needs TMA (SM90+).

- Relax the KDAKernel gate and the two KDA GPU test skips to >= (9, 0).
- Document the hardware requirement in the Kimi K3 README.
tianyu-l pushed a commit that referenced this pull request Sep 25, 2026
Currently, `KDAKernel` only accept SM100/SM103, so Kimi K3 and its KDA
tests only run on B200-class GPUs. This gate is stale:

- #4351 added it with the `attn-gym==0.0.5` pin, when the KDA kernels
were Blackwell-only.
- Since attn-gym 0.0.7, `chunk_kda` uses CuTe kernels on SM100/SM103 and
Triton kernels on other NVIDIA GPUs. #4374 was closed for this reason.

K3's real hardware floor is the fused bounded gate
(`bound_gate(impl="fused")`), which needs TMA, i.e. CUDA capability
9.0+.

### Changes

- `KDAKernel`: require CUDA capability >= 9.0 instead of exactly
SM100/SM103.
- `test_kda_attention.py`, `test_kimi_k3.py`: relax the KDA skip
conditions to match.
- Kimi K3 README: document the hardware requirement.

SM100/SM103 behavior is unchanged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants