Skip to content

[Kimi K3] Carry the attention-residual blocks as a list - #4656

Open
QIU023 wants to merge 1 commit into
pytorch:mainfrom
QIU023:k3_ac_reuse_attention
Open

QIU023 wants to merge 1 commit into
pytorch:mainfrom
QIU023:k3_ac_reuse_attention

Conversation

@QIU023

@QIU023 QIU023 commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

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_residual stacks 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 7dea4cbaf against main 838e6962e on 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.

activation checkpointing peak memory main peak memory this PR step 1 loss / grad norm step 10 loss / grad norm steps equal to main (loss and grad norm)
none 39.65 GiB 39.46 GiB 8.10837 / 2.0312 3.76747 / 2.5625 10 / 10
selective (the flavor default) 12.30 GiB 12.05 GiB 8.10837 / 2.0312 3.76747 / 2.5625 10 / 10
full 6.16 GiB 5.86 GiB 8.10837 / 2.0312 3.76747 / 2.5625 10 / 10
region 6.16 GiB 5.87 GiB 8.10837 / 2.0312 3.76747 / 2.5625 10 / 10

TP and SP numerics

Verifies numerical correctness under TP and SP: this PR against main 838e6962e on 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.

parallelism GPUs tokens per step activation checkpointing main loss, steps 1 / 10 / 20 main grad norm, steps 1 / 10 / 20 steps equal to main (loss and grad norm)
none 1 256 selective 8.19951 / 3.61554 / 3.29465 2.5469 / 2.5156 / 3.1875 100 / 100
none, main again on a fresh copy of the cache 1 256 selective 8.19951 / 3.61554 / 3.29465 2.5469 / 2.5156 / 3.1875 100 / 100
tp2 x ep2, SP on 2 256 selective 8.21344 / 3.66909 / 3.27307 2.5469 / 2.5625 / 3.0625 100 / 100
tp2 x ep2, SP off 2 256 selective 8.21337 / 3.66648 / 3.25369 2.5469 / 2.5625 / 3.0625 100 / 100
dp2 x tp2 x ep2, SP on 4 512 selective 8.20101 / 3.51541 / 3.12135 2.4062 / 2.2500 / 1.7266 100 / 100
kimi_k3_debugmodel_mm (dp2 x tp2 x ep2, SP on), type checking off 4 4096 none 8.20178 / 3.54356 / 3.11267 2.3906 / 2.3594 / 1.7969 100 / 100

Test 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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 14, 2026
@QIU023
QIU023 marked this pull request as draft September 14, 2026 08:31
@QIU023 QIU023 changed the title [DO NOT review, pending final check] [Kimi K3] Declare torch_remat regions so RegionAC can keep attention and recompute the MoE [DO NOT review, stack on AttnRes AC ReUse] [Kimi K3] Declare torch_remat regions so RegionAC can keep attention and recompute the MoE Sep 14, 2026
@QIU023
QIU023 force-pushed the k3_ac_reuse_attention branch 6 times, most recently from 7ec30e5 to 37d595a Compare September 16, 2026 09:13
@QIU023 QIU023 changed the title [DO NOT review, stack on AttnRes AC ReUse] [Kimi K3] Declare torch_remat regions so RegionAC can keep attention and recompute the MoE [DO NOT review] [Kimi K3] Checkpoint the attention-residual computation Sep 16, 2026
@QIU023
QIU023 force-pushed the k3_ac_reuse_attention branch 2 times, most recently from aded475 to 7e9622a Compare September 17, 2026 08:11
@QIU023 QIU023 changed the title [DO NOT review] [Kimi K3] Checkpoint the attention-residual computation [Kimi K3] Checkpoint the attention-residual computation Sep 17, 2026
@QIU023
QIU023 marked this pull request as ready for review September 17, 2026 08:48
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 17, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

Comment thread torchtitan/models/kimi_k3/model.py Resolved
@QIU023
QIU023 force-pushed the k3_ac_reuse_attention branch from 7e9622a to 5d469fd Compare September 28, 2026 08:27
@QIU023
QIU023 marked this pull request as draft September 28, 2026 08:39
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 28, 2026

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

i would suggest keeping the tensor to list of tensor refactor but removing the enforced attention residual recomputation for now.

Comment thread torchtitan/models/kimi_k3/model.py Resolved
Comment thread torchtitan/models/kimi_k3/model.py Resolved
Comment thread torchtitan/models/kimi_k3/model.py Resolved
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
QIU023 force-pushed the k3_ac_reuse_attention branch from 5d469fd to 7dea4cb Compare October 4, 2026 07:17
@QIU023 QIU023 changed the title [Kimi K3] Checkpoint the attention-residual computation [Kimi K3] Carry the attention-residual blocks as a list Oct 4, 2026
@QIU023
QIU023 requested a review from shuhuayu October 4, 2026 08:22
@QIU023

QIU023 commented Oct 4, 2026

Copy link
Copy Markdown
Contributor Author

Hi Shuhua @shuhuayu , simply dropped the attention residual checkpointing commit and reran the numerical verification comparing with main and everything is good, please check, thanks! once this merged I will rebase #4963 and it should be ready

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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.

2 participants