Skip to content

kimi_k3: a page for the pipeline cache, with the stage figure and a f… - #4914

Merged
tianyu-l merged 3 commits into
pytorch:mainfrom
QIU023:k3_pp_cache_doc
Sep 29, 2026
Merged

tianyu-l merged 3 commits into
pytorch:mainfrom
QIU023:k3_pp_cache_doc

Conversation

@QIU023

@QIU023 QIU023 commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a page for the Kimi K3 pipeline's attention residual cache, torchtitan/models/kimi_k3/pipeline_parallel/PP_ATTN_RES_CACHE.md, with two figures in the style of models/common/MOE_SHARDING.md, as asked in the review of #4312.

  • assets/images/kimi_k3_pp_attn_res_cache.svg: one virtual stage on one rank with attn_res_cache on and off, forward and backward, with the P2P sends and receives and a legend for every symbol.
  • assets/images/kimi_k3_pp_attn_res_cache_example.svg: a forward-only example, four ranks with two virtual stages each in loop placement, showing the blocks each rank holds when each stage starts, the blocks each hop brings, and the per-hop P2P volume with the cache on and off.

…orward-only example

PP_ATTN_RES_CACHE.md explains the attention residual cache of the pipeline
stage, in the shape of models/common/MOE_SHARDING.md. The first figure is one
virtual stage on one rank with attn_res_cache on and off, forward and
backward, with the P2P boxes and a legend for every symbol. The second is a
forward-only example: four ranks with two virtual stages each in loop
placement, the blocks each rank holds when each stage starts, the blocks
each hop brings, and the per-hop P2P volume with the cache on and off. Both
figures are SVGs under assets/images, where the MoE page keeps its figure.
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Sep 27, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 27, 2026
@pytorch-bot

pytorch-bot Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

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

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

thx for adding the figures, left some comments.

[`stage.py`](stage.py), the rank cache is [`cache.py`](cache.py), and the
tables that decide what each hop carries are [`layout.py`](layout.py).

## Overview

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.

for the backward path with cache=on, the received gradient is correctly added to the current rank's deposit, can you also add one line on the changes of $\nabla \Delta$, which should also adds the gradient from the current stage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added: $\nabla\Delta = \nabla B[\Delta] + \text{deposits}$, this stage's own gradient for the received blocks (including what came back in $\nabla\Delta'$) plus the deposits the rank's later stages left for them.

| on (default) | hidden `[T, D]` and the blocks the receiving rank has not seen, `[T, Nd, D]` | every block its earlier stages committed or received, per micro-batch, in `PPRankLocalCache`, released after its last stage's forward | the same values; the cached blocks' gradients are summed in another order, so not bitwise |
| off | hidden `[T, D]` and the whole stack `[T, N, D]` | nothing between hops | bitwise |

Plain `1F1B` has one stage per rank, so the rank cache never holds anything

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.

it still holds the cache stack temporarily? and both transports carry the whole stack on every hop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, until the end of that stage's forward, and both transports carry the whole stack on every hop. Corrected the doc

- `_assemble_stack`: the received delta and the held blocks become one leaf
`[T, N, D]` in block order, and the stage's model part runs on it.
- `_pack_outgoing_delta`: the columns the next rank lacks, `delta_to_send`,
as views of the model's stack.

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.

the packed tensors are not views even they are selected as views.

@QIU023 QIU023 Sep 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for now torch.stack copies them. Corrected the doc, will have a follow up PR to optimize it to use and store reference, trying to remove any unnecessary copy in followed optimization

…s, no internals

The stage figure drew the forward labels and caption in orange while the page
calls the forward path black; they are black now. The backward side showed
only the deposits collected into the incoming gradient for the blocks the
stage committed. The stage also adds the deposits for the blocks it received
to the gradient it sends back, after the split; the split box, its arrow from
the rank cache and the legend now show that, and the page has one line on it.
The 1F1B note says both transports carry the whole stack and the rank cache
holds the blocks only during the forward. The sections on the stage's
functions and the routing tables are removed, with the figure's note on the
tables; the loop placement the cache needs stays as one line.
_pack_outgoing_delta packs the columns the next rank lacks with torch.stack,
which copies them; its docstring said views.
@QIU023
QIU023 requested a review from shuhuayu September 29, 2026 08:59

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

lgtm!

@tianyu-l
tianyu-l merged commit d6810e4 into pytorch:main Sep 29, 2026
15 checks passed
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.

3 participants