Skip to content

[graph_trainer] Accumulate PP gradients in backward graphs - #5208

Merged
IvanKobzarev merged 1 commit into
mainfrom
ivan/graph-trainer-pp-gradient-accumulation-20261007
Oct 15, 2026
Merged

IvanKobzarev merged 1 commit into
mainfrom
ivan/graph-trainer-pp-gradient-accumulation-20261007

Conversation

@IvanKobzarev

@IvanKobzarev IvanKobzarev commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

PP previously returned each microbatch parameter gradient to Python for accumulation.

Use one accumulating graph per PP backward path and stage. The first executed backward for each stage in a step zeroes its persistent accumulators; every full, split-dW, and multiplexed backward updates and returns the same handles; REDUCE_GRAD consumes the final handles once.

With the shared add-chain support from #5119, supported WGrad producers fuse into the accumulator when enabled. Unsupported producers retain explicit add_.

Validation: 90 focused WGrad and GraphPP runtime tests passed.

@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 9, 2026
@IvanKobzarev
IvanKobzarev added this pull request to stack #5209 October 9, 2026 12:08
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 9, 2026
@IvanKobzarev
IvanKobzarev force-pushed the ivan/graph-trainer-pp-gradient-accumulation-20261007 branch 2 times, most recently from b472914 to c8d3f06 Compare October 9, 2026 17:57
Base automatically changed from ivan/graph-trainer-scoped-fsdp-buckets-20261008 to main October 9, 2026 18:01
@IvanKobzarev
IvanKobzarev force-pushed the ivan/graph-trainer-pp-gradient-accumulation-20261007 branch from c8d3f06 to fcc3878 Compare October 9, 2026 18:01
@IvanKobzarev
IvanKobzarev removed this pull request from stack #5209 October 12, 2026 12:04
@IvanKobzarev
IvanKobzarev force-pushed the ivan/graph-trainer-pp-gradient-accumulation-20261007 branch from fcc3878 to 48086f5 Compare October 12, 2026 12:05
@IvanKobzarev
IvanKobzarev changed the base branch from main to ivan/graph-trainer-pp-wgrad-fusion-20261007 October 12, 2026 12:05
@IvanKobzarev
IvanKobzarev added this pull request to stack #5320 October 12, 2026 12:05
Base automatically changed from ivan/graph-trainer-pp-wgrad-fusion-20261007 to main October 12, 2026 12:08
IvanKobzarev added a commit that referenced this pull request Oct 12, 2026
Some WGrad outputs reach the accumulation buffer through a functional
add chain, especially repeated native and MXFP8 contributions.

Validate the complete left-linear chain before rewriting it, fuse
supported producers in eager BF16 leaf order, and leave unsupported
chains unchanged.

The PP gradient-accumulation hookup follows in #5208.

Validation: 24 WGrad tests passed.
@IvanKobzarev
IvanKobzarev force-pushed the ivan/graph-trainer-pp-gradient-accumulation-20261007 branch from 48086f5 to d14347b Compare October 12, 2026 12:08
@IvanKobzarev
IvanKobzarev removed this pull request from stack #5320 October 12, 2026 12:14
@IvanKobzarev
IvanKobzarev added this pull request to stack #5321 October 12, 2026 12:14
Comment on lines +659 to +660
("full_bw_repeat", graphs.modules.full_bw_repeat),
("full_bw_first", graphs.modules.full_bw_first),

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.

This is doubling the number of graph we need to worry about.
and it's for per-stage... every bw stage have 2 versions...

I wonder if the perf gain is really worth it.
The alternative is to just use one graph, and always do gradient accumulation for mb0. My bet is that it would matter less than 1% for E2E.

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.

esp when we have fused gradient accumulation, and the number of microbatch is probably >10.

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.

I will do perf testing to get real numbers.

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.

Tested PP ingraph grad accumulation - this was ~+8.5% of TPS even on local 4 GBs.

@IvanKobzarev
IvanKobzarev force-pushed the ivan/graph-trainer-pp-gradient-accumulation-20261007 branch from d14347b to 1997c4f Compare October 13, 2026 20:54
@IvanKobzarev IvanKobzarev changed the title [graph_trainer] Accumulate PP gradients in graph [graph_trainer] Accumulate PP gradients in backward graphs Oct 13, 2026
IvanKobzarev added a commit that referenced this pull request Oct 14, 2026
PP previously returned each microbatch parameter gradient to the Python runtime for accumulation. That kept the cross-microbatch dataflow outside the captured backward graphs.

Use the same first/repeated lifecycle as SPMD gradient accumulation. The first executed backward returns the initial gradients; repeated full, split-dW, and multiplexed backward graphs accept those handles, update them in place, and return the latest handles. The scheduled REDUCE_GRAD consumes the final handles once.

When WGrad fusion is enabled, replace supported producer-to-add chains with their fused forms; unsupported producers retain the explicit add_.

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

thanks, this is much simpler than the 2 graph variant.

I wonder if we should do the same for spmd with ga case, i.e. make the first mb graph just take gradient buffer as input, and do the redundant accumulate into zero for mb0.

There will be a perf hit, but is simpler.

PP previously returned each microbatch parameter gradient to the Python runtime for accumulation. That kept the cross-microbatch dataflow outside the captured backward graphs.

Give each PP stage persistent accumulator buffers and use one accumulating full, split-dW, or multiplexed backward graph for every microbatch. Before the first executed backward for a stage each step, zero the buffers; subsequent backwards update and return the same handles. The scheduled REDUCE_GRAD consumes the final handles once.

Using one graph avoids separate first/repeated graph construction and compilation for every stage. When WGrad fusion is enabled, replace supported producer-to-add chains with their fused forms; unsupported producers retain the explicit add_.
@IvanKobzarev
IvanKobzarev force-pushed the ivan/graph-trainer-pp-gradient-accumulation-20261007 branch from 1997c4f to 6acf5e9 Compare October 15, 2026 18:13
@IvanKobzarev
IvanKobzarev merged commit 7e21f41 into main Oct 15, 2026
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.

2 participants