Repository navigation
Replay one CUDA graph per accumulation group - #5088
Conversation
[ghstack-poisoned]
| 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
[ghstack-poisoned]
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
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.