Skip to content

[DO NOT review, stack on #4656] [Kimi K3] cross PP ranks activation memory LoadBalancing - #4764

Draft
QIU023 wants to merge 6 commits into
pytorch:mainfrom
QIU023:k3_pp_balance
Draft

QIU023 wants to merge 6 commits into
pytorch:mainfrom
QIU023:k3_pp_balance

Conversation

@QIU023

@QIU023 QIU023 commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Add pp_balance to pipeline_kimi_k3: the PP ranks named in PPBalanceKnobs.pp_balance_source_ranks wrap their stages' forwards in saved_tensors_hooks whose pack copies each tensor autograd saves into a pool on pp_balance_dest_rank's GPU through the Mooncake Transfer Engine and frees the local storage, and whose unpack reads 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 through functools.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.
  • A CPU test of the pool allocator.

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 (its MC_FORCE_TCP override 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 without pp_balance never 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)
  • pp2 x vp2 on the debug model, rank 0 parking on rank 1, against the same cell without 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.

@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:13
@pytorch-bot pytorch-bot Bot removed the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 17, 2026
@QIU023 QIU023 changed the title [DO NOT review, stack on K3 text PP PR] [Kimi K3] Pipeline parallelism: the rank store offload its blocks on pinned host memory [DO NOT review, stack on K3 text PP PR] [Kimi K3] PP ranks activation memory LoadBalancing transfer through MoonCake Sep 17, 2026
@QIU023 QIU023 changed the title [DO NOT review, stack on K3 text PP PR] [Kimi K3] PP ranks activation memory LoadBalancing transfer through MoonCake [DO NOT review, stack on K3 text PP PR #4312] [Kimi K3] PP ranks activation memory LoadBalancing transfer through MoonCake Sep 17, 2026
@QIU023 QIU023 changed the title [DO NOT review, stack on K3 text PP PR #4312] [Kimi K3] PP ranks activation memory LoadBalancing transfer through MoonCake [DO NOT review, stack on K3 text PP PR #4312] [Kimi K3] PP ranks activation memory LoadBalancing Sep 17, 2026
@QIU023 QIU023 changed the title [DO NOT review, stack on K3 text PP PR #4312] [Kimi K3] PP ranks activation memory LoadBalancing [DO NOT review, stack on K3 text PP PR #4312] [Kimi K3] cross PP ranks activation memory LoadBalancing Sep 17, 2026
@QIU023 QIU023 mentioned this pull request Sep 17, 2026
14 of 18 tasks
@QIU023
QIU023 force-pushed the k3_pp_balance branch 5 times, most recently from 005cf4a to 4669217 Compare September 25, 2026 05:17
@QIU023
QIU023 force-pushed the k3_pp_balance branch 6 times, most recently from 0a90342 to c4afb61 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.
…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 QIU023 changed the title [DO NOT review, stack on K3 text PP PR #4312] [Kimi K3] cross PP ranks activation memory LoadBalancing [DO NOT review, stack on #4656] [Kimi K3] cross PP ranks activation memory LoadBalancing Oct 9, 2026

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