Skip to content

[DO NOT review, stack on K3 text PP PR #4312] [Kimi K3] PP ranks activation memory offload - #4765

Draft
QIU023 wants to merge 4 commits into
pytorch:mainfrom
QIU023:k3_pp_offload
Draft

QIU023 wants to merge 4 commits into
pytorch:mainfrom
QIU023:k3_pp_offload

Conversation

@QIU023

@QIU023 QIU023 commented Sep 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Add attn_res_cache_offload to pipeline_kimi_k3: with it on, the blocks a rank keeps for its later stages (RankStore) are parked on pinned host memory from the stage that commits them until a later stage on the rank reads them, instead of staying on the device for that whole span.

  • RankStore(offload=...) (kimi_k3/pipeline_stage.py): put copies each block to a pinned host tensor on the current stream, blocks copies it back to the device it saw when a later stage assembles its stack.
  • pipeline_kimi_k3(attn_res_cache_offload=...) (kimi_k3/parallelize.py): the switch, passed the way the transport switch is, functools.partial(pipeline_kimi_k3, attn_res_cache_offload=True) as the pipelining_fn of a recipe.
  • A CPU test of the store's offload path (pass-through on a CPU store; the parked copy pinned and the read-back equal under CUDA).

Design

Every stored block is detached (its gradient travels through the store's deposits), so the round trip changes values nowhere. Both copies are non_blocking on the current stream, so stream order alone serializes the D2H of the commit and the H2D of the read; nothing changes in the stage, _assemble still checks the held set against the routing tables and stacks what blocks() returns. A store on CPU has nowhere to park and stores the tensor as it is.

The switch lives on pipeline_kimi_k3 next to attn_res_cache because it is a property of the rank store the pipelining entry builds, not of the model.

Test plan

  • pytest 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_kimi_k3_stage_swap.py tests/unit_tests/cpu/test_kimi_k3_pp_exact_block_grads.py -q (17 passed)
  • pp2 x vp2 and pp8 x vp4 on the debug model with the switch off and on, same seed checkpoint and batch, one warm inductor cache: the loss is expected identical on every step, since the parked copy is exact; the table goes here once measured on H100.

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

pytorch-bot Bot commented Sep 17, 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 17, 2026 09:17
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 17, 2026
@QIU023 QIU023 mentioned this pull request Sep 17, 2026
14 of 18 tasks
@QIU023
QIU023 force-pushed the k3_pp_offload branch 5 times, most recently from c73e17c to 49117a1 Compare September 25, 2026 05:17
@QIU023
QIU023 force-pushed the k3_pp_offload branch 5 times, most recently from 56f4cd3 to 0194621 Compare September 29, 2026 10:22
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.
Each aggregation runs under a torch_remat checkpoint whenever autograd records
it and no activation-checkpointing policy already wraps its block, so backward
rebuilds the FP32 intermediates instead of keeping them. The output
aggregation sits outside every block and always takes its own.

KimiK3Model.parallelize marks the blocks that selective, full or region AC
wraps, and their residuals run as plain calls inside that checkpoint.
RegionAC's block checkpoint is a torch_remat one, which rejects a nested
checkpoint; under selective and full AC a second one would only recompute the
residual again.
… last reader

Each hop sends one tensor per block, and the model takes and returns its
blocks as a list, so the rank store keeps references to the received and
committed tensors instead of copies, and no stage stacks its blocks into a
fresh leaf. The store drops each block at the backward of the stage that
brought it onto the rank, which is its last reader there.

Under the action-list runtime a pending send keeps its tensor alive, so the
stage waits a forward send at its own backward of that micro-batch and an
input-gradient send at the first forward that proves the peer has used it,
instead of at the end of the step. Receive buffers stay with torch, which
allocates each one right before its receive.

PP_ATTN_RES_CACHE.md and the stage figure's legend describe the blocks as one
tensor each, held until the backward of the stage that brought them.
… ones on host memory

Each tensor a pipeline stage's forward saves gets a torch.utils.checkpoint.CheckpointPolicy
when the forward ends. {MUST,PREFER}_SAVE leaves it with autograd; {MUST,PREFER}_CPU_OFFLOAD
copies it to pinned host memory on a side stream, and backward reads it back
prefetch_n_layers layers ahead. A full host budget keeps the save on the device. The rank
store pins every block it holds, so no policy moves one.

KimiK3Model.Config.pp_memory.cpu_offload = "all" gives MUST_CPU_OFFLOAD to every save outside
the model's last layer, within cpu_offload_budget_gib of host memory; the saves a stage's
backward reads first come back when the rank's previous compute action begins.

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