Repository navigation
Conversation
QIU023
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 17, 2026 09:12
|
The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:
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
marked this pull request as draft
September 17, 2026 09:13
QIU023
force-pushed
the
k3_pp_balance
branch
5 times, most recently
from
September 25, 2026 05:17
005cf4a to
4669217
Compare
QIU023
force-pushed
the
k3_pp_balance
branch
6 times, most recently
from
September 29, 2026 10:22
0a90342 to
c4afb61
Compare
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.
QIU023
force-pushed
the
k3_pp_balance
branch
from
September 29, 2026 18:53
c4afb61 to
8545ea0
Compare
… 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.
QIU023
force-pushed
the
k3_pp_balance
branch
from
September 29, 2026 20:54
8545ea0 to
43ccdcc
Compare
… 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.
…ionally parking saves on lighter ranks pp_memory.cpu_offload = "planned" runs the first step with every eligible save offloaded to host memory, so a configuration that only fits when it offloads can start. That step records each compute action's peak with the bytes then off the device added back, its duration, and each stage micro-batch's saved bytes. When it returns, the ranks exchange these profiles and derive one plan: the rank with the highest peak moves the largest stage micro-batch whose copy-out and copy-back windows cover that peak, until every rank is under the target, the mean of the profiled peaks by default. The plan marks a moved save MUST_CPU_OFFLOAD and routes it to host memory, or, with pp_memory.balance, to a pool on a lighter pipeline rank reached through mooncake's transfer engine (RemoteBackend: over tcp the pool sits in pinned host memory, over rdma in device memory).
…so over NVLink in a node pp_memory.balance no longer needs cpu_offload="planned". With cpu_offload="none" the profiled step keeps every save on the device and the plan only parks saves in pools on lighter ranks, never in host memory. RemoteBackend takes mooncake's nvlink_intra transport, with the pool and the staging buffer in device memory as over rdma, so the ranks of one node balance their memory across its GPUs.
QIU023
force-pushed
the
k3_pp_balance
branch
from
September 29, 2026 21:47
43ccdcc to
224bdba
Compare
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
Add
pp_balancetopipeline_kimi_k3: the PP ranks named inPPBalanceKnobs.pp_balance_source_rankswrap their stages' forwards insaved_tensors_hookswhosepackcopies each tensor autograd saves into a pool onpp_balance_dest_rank's GPU through the Mooncake Transfer Engine and frees the local storage, and whoseunpackreads it back when backward needs it. Under interleaved 1F1B the resident activation load is uneven across PP ranks; this moves it from the heavy ranks to a light one. Copies are exact.PPBalanceEngine,_PoolAllocator,PPBalanceKnobs,install_pp_balance(kimi_k3/pp_balance.py, new): the engine over the Transfer Engine, the pool's allocator, the knob record, and the installer that wires the hooks onto the schedule's stages.pipeline_kimi_k3(pp_balance=...)(kimi_k3/parallelize.py): the record a recipe passes throughfunctools.partial, like the transport switch; the engine is installed after the split and hangs off the schedule for the schedule's lifetime, since it owns the registered buffers and sessions.Design
Every PP rank constructs the engine, because the address-book exchange is a collective: the destination allocates and registers the pool, the sources register a staging buffer they funnel transfers through, so per-tensor register and unregister calls stay off the hot path. Tensors below
pp_balance_min_tensor_mib, non-contiguous tensors and tensors larger than the staging buffer stay local. The engine picks RDMA where an HCA exists and TCP where one does not (itsMC_FORCE_TCPoverride is honoured for the pool placement too), so the same wiring runs on a workstation and on a cluster.The pool's allocator coalesces its free list: parked tensors are freed in whatever order backward reaches them, not in allocation order, and a bump pointer would run a long step out of pool while most of it is free. Only contiguous tensors are parked: a normalized layout keeps the values but changes the strides the backward kernels tile by.
The transport stays in the model folder, as the AttnRes block transport does; the package is imported at
install_pp_balance, so a run withoutpp_balancenever needs it.Test plan
pytest tests/unit_tests/cpu/test_kimi_k3_pp_balance_pool.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_kimi_k3_stage_swap.py tests/unit_tests/cpu/test_kimi_k3_pp_exact_block_grads.py -q(21 passed)pp_balance, same seed checkpoint and batch, one warm inductor cache; the engine's counters (tensors parked and fetched back, bytes, tensors kept local) and rank 0's peak memory; the table goes here once measured on H100.