Skip to content

[kimi k3] Support sample packing in KDA - #4347

Merged
tianyu-l merged 5 commits into
pytorch:mainfrom
JavaZeroo:kimi-k3-sample-packing
Sep 10, 2026
Merged

tianyu-l merged 5 commits into
pytorch:mainfrom
JavaZeroo:kimi-k3-sample-packing

Conversation

@JavaZeroo

@JavaZeroo JavaZeroo commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

What changes

  • KimiK3Model.get_attention_masks returns a mask dict, like Qwen3.5. MLA layers read "quadratic_attention" and KDA layers read "kda". The "kda" entry is the VarlenMetadata that Attention Gym's causal_conv1d and chunk_kda use to reset the conv window and the recurrent state at each document start. Each block picks its own entry by attn_mask_key. The signature is the same as Decoder.get_attention_masks after enable cudagraph for varlen pretrain #4156.
  • The multimodal packer numbers its padding positions from the first padded token, modulo max_context_length.
  • kimi_k3_debugmodel takes seq_len like every other debug config and sizes its rows from the model spec, so it runs 2048-token rows instead of a fixed 256.

The text path and the helper itself landed in #4156. This PR only touches the multimodal packer, the multimodal collator and Kimi K3.

Verification

Packing on the debug config. kimi_k3_debugmodel rows are 2048 tokens and one cc12m sample is about 210 tokens, so without the packer a row holds one document and the rest is padding. With MMSamplePackingConfig wrapped around the dataset:

Documents per row Padding tokens per row
No packer 1 1837
Packer 16 3

End to end on B200. kimi_k3_debugmodel, 10 steps, --debug.seed 42 --debug.deterministic. With the packer, loss went from 12.50 to 3.26 over 10 steps and grad_norm from 15.1 to 2.2. Without the packer, loss went from 12.45 to 4.50. These numbers come from the previous revision of this branch; the K3 wiring is unchanged. TODO: re-run on this revision.

Kimi K3 does not match main bitwise on rows with padding, and should not: on main the KDA layers receive no offsets at all.

TODO

  • get_attention_masks in Kimi K3 and Qwen3.5 are near-identical and should be shared; needs a design.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 27, 2026
@JavaZeroo
JavaZeroo marked this pull request as draft August 27, 2026 05:33
Comment thread torchtitan/models/common/attention.py Resolved
@JavaZeroo

Copy link
Copy Markdown
Contributor Author

@tianyu-l @shuhuayu All the review comments are addressed, and I verified the KDA varlen path end-to-end on a B200. Ready for another look whenever you have time.

@JavaZeroo
JavaZeroo force-pushed the kimi-k3-sample-packing branch from cf63eb6 to c340951 Compare September 3, 2026 03:34
Comment thread torchtitan/models/qwen3_5/model.py Outdated
sequence_positions,
positions,
padding_mask=padding_mask,
include_host_offsets=True,

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.

this field is removed, please rebase

def get_attention_masks(
self,
positions: torch.Tensor,
padding_mask: torch.Tensor | None = None,

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.

please make it a kwarg, instead of positional (we probably should do the same for positions but the scope would be too much, can do it later)

Comment thread torchtitan/models/qwen3_5/gdn.py Outdated
key_head_dim: int,
value_head_dim: int,
cu_seqlens_host: tuple[int, ...] | None = None,
cu_seqlens_cpu: torch.Tensor | None = None,

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.

similar

Comment thread torchtitan/models/kimi_k2_7/model.py Outdated

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.

why not feeding padding_mask here?

return _compiled_create_block_mask(*args, **kwargs)


def create_varlen_metadata_for_document(

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 take a look at #4156 and see what's the difference?

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.

Looked at #4156. It already implements the text-path padding_mask, and it keeps every segment within max_context_length, which mine doesn't. I think I'll wait for it to merge and then rebase this PR on top of it, keeping only the multimodal packer/collator and the Kimi K3 mask wiring.

@QIU023 QIU023 mentioned this pull request Sep 8, 2026
14 of 18 tasks
@shuhuayu

shuhuayu commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Hi @JavaZeroo, #4156 has merged, can you rebase?

@JavaZeroo

Copy link
Copy Markdown
Contributor Author

Hi @JavaZeroo, #4156 has merged, can you rebase?

Sure, im currently working on it.

@shuhuayu shuhuayu 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.

lgtm, some nit comments.

Comment thread torchtitan/models/kimi_k3/model.py Outdated
Comment on lines +48 to +49
# MLA layers read "quadratic_attention" (a BlockMask under flex, shared offsets
# under varlen); KDA layers read "kda" (document offsets).

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.

nit: remove.

Comment on lines +89 to +90
# Sample packing pads inside each row, so carry the packer's own mask
# through and extend it with this batch's tail padding.

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.

nit: remove.

Grain pads a packed multimodal row with position 0 on every padded token,
which the position-based boundary rules read as one document start per
padded token. Number the padding from its first token modulo
max_context_length, as the multimodal collator already does for the batch
tail, so a padded run forms segments of at most one context window. The
packer and collator also emit the padding_mask that
create_varlen_metadata_for_document takes.
get_attention_masks returns a mask dict: MLA layers read
"quadratic_attention" (a BlockMask under flex, the shared offsets under
varlen) and KDA layers read "kda", the document offsets that reset the
Q/K/V causal-convolution window and the recurrent state at document
boundaries. Drop the ValueError that rejected sample packing.
kimi_k3_debugmodel takes seq_len like every other debug config and derives
the training row length from the model spec instead of a fixed 256, so the
debug rows match the shared DEFAULT_DEBUG_MODEL_SEQ_LEN.

The dataloader helper drops two settings that did nothing: min_pixels and
max_pixels are ignored by resize_to_navit_patch_grid, which only reads
max_patches and max_patches_per_side, and max_images_per_batch=8 was sized
for 256-token rows and would reject a packed 2048-token row; the collator
default of 128 applies instead.
@JavaZeroo
JavaZeroo force-pushed the kimi-k3-sample-packing branch from f05a359 to 431b922 Compare September 10, 2026 12:12
@tianyu-l
tianyu-l merged commit 62b423a into pytorch:main Sep 10, 2026
14 checks passed
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.

4 participants