Skip to content

[DO NOT review, stack on #4656][Kimi K3] PP: store each attention-residual block once per rank and free it at its last reader - #4963

Draft
QIU023 wants to merge 3 commits into
pytorch:mainfrom
QIU023:pp_review_optimize
Draft

QIU023 wants to merge 3 commits into
pytorch:mainfrom
QIU023:pp_review_optimize

Conversation

@QIU023

@QIU023 QIU023 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Relation to other PRs

Summary

The Kimi K3 pipeline stage keeps each attention-residual block once per pipeline rank, the memory the Attention Residuals paper states for cross-stage caching ("each block is stored exactly once across all V virtual stages", section 4.1), by passing the blocks as tensors of their own instead of stacking them.

  • kimi_k3/pipeline_parallel/stage.py: _assemble_stack, _pack_outgoing_delta and _split_stack_grad work on per-block lists, so a hop sends one tensor per block and no stage copies its blocks into a fresh leaf; under the action-list runtime, a forward send is waited at the stage's own backward of that micro-batch.
  • kimi_k3/pipeline_parallel/__init__.py: turns the forward send waits on under _PipelineScheduleRuntime.
  • kimi_k3/model.py: the model takes and returns its blocks as a list.
  • kimi_k3/pipeline_parallel/PP_ATTN_RES_CACHE.md and the stage figure's legend: one [T, D] tensor per block.

Design

  • Before this PR (the stage of [Kimi K3] Pipeline Parallelism support (text LLM side): PP Virtual Stage Cache Adapter for Attention Residual #4312), a rank holds each block several times:
    • every stage stacks the blocks it reads into a fresh [T, N, D] leaf that lives until its backward;
    • the outgoing payload is a second stack;
    • the rank cache keeps views that pin whole receive buffers and output stacks.
  • With this PR, one tensor per block:
    • a received block is its receive buffer, and a committed block is the model's own tensor;
    • the rank cache, the stages' inputs and the sends all reference that memory, so a block is freed once the stage that brought it onto the rank has run its backward;
    • the rank cache, the routing tables and the release point are unchanged.
  • Send waits:
    • a pending send keeps its tensor allocated until it is waited, and torch's action-list runtime waits every send at the end of the step;
    • the stage waits a forward send at its own backward of that micro-batch, when the receiver has used it.

Results

4 H100s, the Kimi K3 debug model widened to dim 6144 (17 layers in blocks of 4, 8 experts, every width that scales with dim scaled 24 times), 16 micro-batches of 2048 c4 tokens per step, Interleaved1F1B, FullAC, AdamW, seed 42, deterministic, one warm compile cache per layout. #4656 is the two commits below this PR; both trees ran 100 steps. The layouts with two pipeline ranks hold half the model per GPU and run the model at dim 5120, since at 6144 both trees run out of memory.

layout peak allocated per rank, #4656 (GiB) this PR (GiB) saved (GiB) step time, #4656 / this PR
pp4 x vpp2 55.97 / 48.86 / 46.62 / 32.76 54.68 / 46.61 / 44.75 / 31.40 1.29 / 2.25 / 1.88 / 1.36 3.662 / 3.582 s
pp4 x vpp4 62.75 / 44.14 / 49.05 / 40.38 59.54 / 42.19 / 45.29 / 37.34 3.21 / 1.95 / 3.76 / 3.05 3.821 / 3.744 s
dp2 x pp2 x vpp2, dim 5120 52.51 / 52.51 / 39.28 / 39.28 51.26 / 51.26 / 38.27 / 38.27 1.25 / 1.25 / 1.02 / 1.02 4.834 / 4.816 s
pp2 x vpp2, dim 5120 67.85 / 49.77 66.60 / 48.75 1.25 / 1.02 5.231 / 5.271 s
pp2 x vpp4, dim 5120 67.31 / 52.72 65.69 / 50.65 1.62 / 2.07 5.345 / 5.252 s

Loss / grad norm with and without this PR (without it is the tree of the two #4656 commits below, whose pipeline stage is main's), each pair on one shared warm compile cache with the same seed and data; the last column counts the steps of the 100 whose loss and grad norm are identical.

layout tree step 1 step 10 step 50 step 100 steps identical to without
pp4 x vpp2 without this PR 8.08712 / 24.3750 7.56287 / 1040.0000 2.93439 / 4.9375 2.66764 / 1.5859
pp4 x vpp2 this PR 8.08712 / 24.3750 7.56287 / 1040.0000 2.93439 / 4.9375 2.66764 / 1.5859 100 / 100
pp4 x vpp4 without this PR 8.12927 / 21.0000 7.97795 / 32.5000 2.75488 / 4.9375 2.43219 / 1.2266
pp4 x vpp4 this PR 8.12927 / 21.0000 7.97795 / 32.5000 2.75488 / 4.9375 2.43219 / 1.2266 100 / 100
dp2 x pp2 x vpp2, dim 5120 without this PR 8.06354 / 24.3750 6.34372 / 14.5000 2.74512 / 3.9688 2.25450 / 15.7500
dp2 x pp2 x vpp2, dim 5120 this PR 8.06354 / 24.3750 6.34372 / 14.5000 2.74512 / 3.9688 2.25450 / 15.7500 100 / 100
pp2 x vpp2, dim 5120 without this PR 8.06628 / 23.1250 5.24328 / 22.1250 2.80749 / 4.2188 2.45762 / 1.7578
pp2 x vpp2, dim 5120 this PR 8.06628 / 23.1250 5.24328 / 22.1250 2.80749 / 4.2188 2.45762 / 1.7578 100 / 100
pp2 x vpp4, dim 5120 without this PR 8.11475 / 35.2500 6.92955 / 23.1250 2.77905 / 4.0625 2.40688 / 1.1328
pp2 x vpp4, dim 5120 this PR 8.11475 / 35.2500 6.92955 / 23.1250 2.77905 / 4.0625 2.40688 / 1.1328 100 / 100

Blocks held in step 5 at each rank's peak, in [T, D] blocks (24 MiB at dim 6144, 20 MiB at dim 5120): the rank cache, the stages' saved inputs and outputs and the pending forward sends of blocks, counted by storage. The tight bound frees each block at the backward of the stage that brought it onto the rank; the paper's bound keeps every block of a micro-batch until the rank's last backward of that micro-batch. With this PR the blocks equal the tight bound at every forward and backward of the step, except the last stage's forwards at pp4 x vpp2 and in the pp2 layouts, which sit one block below it because the bound also counts the block that stage completes and uses inside its forward.

layout #4656 this PR tight bound paper bound pending hidden-state sends, #4656 this PR
pp4 x vpp2 44 / 81 / 69 / 62 15 / 18 / 14 / 15 15 / 18 / 14 / 15 22 / 26 / 22 / 23 32 / 32 / 32 / 16 10 / 8 / 6 / 4
pp4 x vpp4 100 / 58 / 143 / 102 19 / 17 / 23 / 21 19 / 17 / 23 / 21 31 / 29 / 35 / 33 64 / 64 / 64 / 48 18 / 16 / 14 / 12
pp2 x vpp2, dim 5120, with or without dp2 38 / 43 9 / 9 9 / 9 13 / 13 32 / 16 4 / 2
pp2 x vpp4, dim 5120 34 / 81 9 / 11 9 / 11 15 / 17 64 / 48 8 / 6

A hidden-state send carries a stage's output, one [T, D] that is not a block, so it is counted apart (one per forward of a stage other than the last, taken out of the step 5 records); its memory is real and is part of the peaks above. With this PR it is waited at the sender's own backward of that micro-batch; on #4656 it stays until the end of the step.

Test plan

  • pytest tests/unit_tests/cpu/test_kimi_k3_pp_block_grads.py tests/unit_tests/cpu/test_kimi_k3_pp_stage.py tests/unit_tests/cpu/test_kimi_k3_pp_layout.py tests/unit_tests/cpu/test_pipeline_parallel.py tests/unit_tests/cpu/test_kimi_k3_attention_residual_recompute.py -q (70 passed): the four-rank gloo pipelines of [Kimi K3] Pipeline Parallelism support (text LLM side): PP Virtual Stage Cache Adapter for Attention Residual #4312, now with one tensor per block and the forward send waits, under Interleaved1F1B with the cache on and off, every block gradient bitwise with one device, and eval between steps.

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 30, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 30, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 30, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/fake-pg

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@QIU023
QIU023 marked this pull request as draft September 30, 2026 07:02
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 30, 2026
@QIU023
QIU023 force-pushed the pp_review_optimize branch 2 times, most recently from 345e9e0 to 312bc81 Compare October 1, 2026 06:39
@QIU023 QIU023 mentioned this pull request Oct 12, 2026
14 of 18 tasks
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 takes the list of sources it attends over (the blocks, then
the partial block), the way FLA's fused_attnres takes its residuals, and
stacks it inside the compiled region. Dynamo specializes a list on its length,
so the region compiles one graph per source count (5 for the debug flavor, 8
for Kimi-K3) and raises its recompile limit to 32: a fullgraph region fails at
the default limit of 8 instead of falling back. The Triton override takes the
same list, stacks it for its kernels and keeps the sources, not the stack,
for backward.

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.
…per rank

The stage hands the model its blocks as a list instead of stacking them
into a fresh leaf, and a hop sends one tensor per block. A received block
is its own receive buffer and a committed block is the model's own tensor,
so the rank cache, the stages' inputs and the sends reference the same
memory, and each block lives once on the rank until the stage that brought
it has run its backward. _assemble_stack, _pack_outgoing_delta and
_split_stack_grad keep their roles on per-block lists; the rank cache, the
routing tables and the release point are unchanged. The model takes and
returns its blocks as a list.

Under the action-list runtime a pending send keeps its tensor allocated, so
a forward send is waited at the stage's own backward of that micro-batch
instead of at the end of the step.
@QIU023
QIU023 force-pushed the pp_review_optimize branch from 312bc81 to 57790e1 Compare October 12, 2026 17:53
When nothing upstream of a stage is trainable, as when LoRA adapts only some
projections and the first stages hold none of them, the receive metadata marks
every input as not needing a gradient and backward produces none. The stage
raised "backward produced no gradient for any input" in that case; it now sends
nothing upstream, as it already does for its own blocks. The error stays for a
stage with an input that needs a gradient. The new test freezes block 0 in the
pp4 x vpp2 split and matches the single-device reference bitwise.

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

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.

1 participant