Skip to content

Replay one CUDA graph per accumulation group - #5088

Merged
fegin merged 4 commits into
mainfrom
gh/fegin/181/head
Oct 7, 2026
Merged

fegin merged 4 commits into
mainfrom
gh/fegin/181/head

Conversation

@fegin

@fegin fegin commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Capture one complete forward-backward group and replay it for each runtime gradient-accumulation group. This supports dynamic group counts when every group has the same input structure, tensor metadata, and gradient-producing parameters.

Each replay performs FSDP gradient reduction and reshard. Reuse existing sharded gradients so the captured reduction accumulates with +=, and copy loss outputs before the next replay. Reject deferred gradient reduction, SDC replay, and GraphTrainer.

fegin added 2 commits October 6, 2026 01:10
[ghstack-poisoned]
[ghstack-poisoned]
@pytorch-bot pytorch-bot Bot added ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 6, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 6, 2026
self.garbage_collector.run(self.num_completed_steps + 1)
self.optim.zero_grad(set_to_none=True)
self.optim.zero_grad(
set_to_none=not self._cuda_graph_per_accumulation_group_enabled

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.

Interesting:

  • when entire outer + inner is captured, we use boyuan's trick to let optimizer access the gradient states
  • now when inner-only is captured, it seems the trick doesn't work because gradient becomes completely external to cudagraph so we have to keep them somewhere

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.

Your reading is correct.

_CUDAGraphGradientState works for whole-step capture because each cuda graph starts with param.grad=None. And cuda graph creates the gradient buffers, and the _CUDAGraphGradientState restores the Python references before the optimizer step.

With this feature, we won't be able to let the cuda graph to create the gradients, because the same cuda graph is executed by the first group and the following groups, which will do "+=" gradient accumulation and assume the gradients exist. Since we only have one cuda graph, this "assuming the gradients exist" will apply to the first group as well. Hence we have to keep gradients outside of the cudagraph.

fegin added 2 commits October 6, 2026 23:29
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Oct 7, 2026
Capture one complete forward-backward group and replay it for each runtime gradient-accumulation group. This supports dynamic group counts when every group has the same input structure, tensor metadata, and gradient-producing parameters.

Each replay performs FSDP gradient reduction and reshard. Reuse existing sharded gradients so the captured reduction accumulates with +=, and copy loss outputs before the next replay. Reject deferred gradient reduction, SDC replay, and GraphTrainer.

ghstack-source-id: 9e9b8ea
Pull-Request: #5088
@fegin
fegin changed the base branch from gh/fegin/181/base to main October 7, 2026 15:44
@fegin
fegin merged commit 7a8f011 into main Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/b200 Trigger B200 CI ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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