Repository navigation
Conversation
QIU023
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 14, 2026 08:31
QIU023
marked this pull request as draft
September 14, 2026 08:31
QIU023
force-pushed
the
k3_ac_reuse_attention
branch
6 times, most recently
from
September 16, 2026 09:13
7ec30e5 to
37d595a
Compare
4 of 18 tasks
QIU023
force-pushed
the
k3_ac_reuse_attention
branch
2 times, most recently
from
September 17, 2026 08:11
aded475 to
7e9622a
Compare
QIU023
marked this pull request as ready for review
September 17, 2026 08:48
|
|
shuhuayu
reviewed
Sep 25, 2026
QIU023
force-pushed
the
k3_ac_reuse_attention
branch
from
September 28, 2026 08:27
7e9622a to
5d469fd
Compare
QIU023
marked this pull request as draft
September 28, 2026 08:39
shuhuayu
requested changes
Oct 3, 2026
shuhuayu
left a comment
Contributor
There was a problem hiding this comment.
i would suggest keeping the tensor to list of tensor refactor but removing the enforced attention residual recomputation for now.
Inside the model the block residual is a list of [T, D] blocks instead of a [T, N, D] stack. A block opening appends to the list; it used to concatenate a longer stack, and every earlier layer's checkpoint kept the shorter stack it had been called with, so each copy lived until backward. The aggregation stacks the list inside the call, so its arithmetic is unchanged. The model's forward still takes and returns the stack, so the pipeline stage is unchanged: the stack it is given is unbound into the list, and the list is stacked on the way out. The multimodal decoder boundary converts no stack any more: the list starts empty and its first block is layer 0's converted input. The block returns its output and the blocks as one flat tuple, since a function under torch_remat region checkpointing (RegionAC) may return only one hop of tensors.
QIU023
force-pushed
the
k3_ac_reuse_attention
branch
from
October 4, 2026 07:17
5d469fd to
7dea4cb
Compare
Contributor
Author
This branch has not been deployed
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.
Summary
Carry Kimi K3's attention-residual blocks inside the model as a list of [T, D] tensors instead of a [T, N, D] stack, so a block opening appends a reference instead of building a longer copy, and the activation checkpoints of later layers share the same blocks.
kimi_k3/model.py: the blocks are a list;_apply_attention_residualstacks it inside the call, so the arithmetic is unchanged. The model's forward still takes and returns the stack, so the pipeline stage is unchanged.kimi_k3/sharding.py: the layer 0 boundary of the multimodal decoder converts no stack, since the list starts empty.Design
Every block opening used to concatenate a longer stack, and each layer's checkpoint kept the stack it was called with, so under full, selective or region activation checkpointing every shorter copy stayed alive until backward: for 24 layers in blocks of 4 the checkpoints held 21 [T, D] units where the blocks are 6. A checkpoint keeps its inputs by reference, so with a list every checkpoint references the same six blocks.
Results
Memory
This PR
7dea4cbafagainst main838e6962eon one H100 (80 GB):kimi_k3_debugmodel(dim 256, 17 layers, attention residual blocks of 4), AdamW, bf16,seed=42, deterministic, 262144 tokens per step in 65536-token micro-batches, 10 steps; each pair runs on copies of one inductor cache warmed by both trees. Peak memory is the max reserved memory; one [T, D] block is 32 MiB here.8.10837/2.03123.76747/2.56258.10837/2.03123.76747/2.56258.10837/2.03123.76747/2.56258.10837/2.03123.76747/2.5625TP and SP numerics
Verifies numerical correctness under TP and SP: this PR against main
838e6962eon four H100s, the same debug model with AdamW,seed=42, deterministic, 100 steps, each configuration on copies of one inductor cache warmed by both trees. The model memorises the debug set after about step 40, so the table shows steps 1, 10 and 20.8.19951/3.61554/3.294652.5469/2.5156/3.18758.19951/3.61554/3.294652.5469/2.5156/3.18758.21344/3.66909/3.273072.5469/2.5625/3.06258.21337/3.66648/3.253692.5469/2.5625/3.06258.20101/3.51541/3.121352.4062/2.2500/1.7266kimi_k3_debugmodel_mm(dp2 x tp2 x ep2, SP on), type checking off8.20178/3.54356/3.112672.3906/2.3594/1.7969Test plan
pytest tests/unit_tests/cpu/test_kimi_k3_pp_layout.py tests/unit_tests/cpu/test_kimi_k3_pp_stage.py tests/unit_tests/cpu/test_kimi_k3_pp_block_grads.py tests/unit_tests/cpu/test_moe.py tests/unit_tests/cpu/test_transforms.py -q(62 passed; the PP tests need a torch nightly from 2026-10-03 or later)Relation to earlier revisions of this PR
Earlier revisions also recomputed the attention residual in backward under
torch_remat; that part is dropped, as suggested in review.