Repository navigation
[kimi k3] Support sample packing in KDA - #4347
Conversation
d12ac5f to
b81093f
Compare
cf63eb6 to
c340951
Compare
| sequence_positions, | ||
| positions, | ||
| padding_mask=padding_mask, | ||
| include_host_offsets=True, |
There was a problem hiding this comment.
this field is removed, please rebase
| def get_attention_masks( | ||
| self, | ||
| positions: torch.Tensor, | ||
| padding_mask: torch.Tensor | None = None, |
There was a problem hiding this comment.
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)
| key_head_dim: int, | ||
| value_head_dim: int, | ||
| cu_seqlens_host: tuple[int, ...] | None = None, | ||
| cu_seqlens_cpu: torch.Tensor | None = None, |
There was a problem hiding this comment.
why not feeding padding_mask here?
| return _compiled_create_block_mask(*args, **kwargs) | ||
|
|
||
|
|
||
| def create_varlen_metadata_for_document( |
There was a problem hiding this comment.
could you take a look at #4156 and see what's the difference?
There was a problem hiding this comment.
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.
|
Hi @JavaZeroo, #4156 has merged, can you rebase? |
Sure, im currently working on it. |
c340951 to
3a43019
Compare
shuhuayu
left a comment
There was a problem hiding this comment.
lgtm, some nit comments.
| # MLA layers read "quadratic_attention" (a BlockMask under flex, shared offsets | ||
| # under varlen); KDA layers read "kda" (document offsets). |
| # Sample packing pads inside each row, so carry the packer's own mask | ||
| # through and extend it with this batch's tail padding. |
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.
f05a359 to
431b922
Compare
What changes
KimiK3Model.get_attention_masksreturns 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 byattn_mask_key. The signature is the same asDecoder.get_attention_masksafter enable cudagraph for varlen pretrain #4156.kimi_k3_debugmodeltakesseq_lenlike 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_debugmodelrows 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. WithMMSamplePackingConfigwrapped around the dataset: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_masksin Kimi K3 and Qwen3.5 are near-identical and should be shared; needs a design.