Repository navigation
[cudagraph] Capture gradient accumulation in one CUDA graph - #4658
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
| self._defer_fsdp_gradient_reduction = ( | ||
| self.parallel_dims.fsdp_enabled | ||
| and self.num_accumulation_steps > 1 | ||
| and not self.config.training.disable_cuda_graphs | ||
| ) |
There was a problem hiding this comment.
not totally convinced that we should defer when the 3 conditions are satisfied -- e.g. when cudagraph is disabled (e.g. on a hardware that doesn't support cudagraph), one still enjoy the benefit of reduced comm overhead, wgrad accumulation, etc.
Maybe we can make it a parallelism.fsdp_xxx flag -- xxx could be defer_gradient_reduction
There was a problem hiding this comment.
The very first version actually includes one and you suggested that we should assume CUDAGraph is always enabled for this use case. If you prefer, I can add it back.
There was a problem hiding this comment.
let me see if I can clarify
- during offline discussion I was saying "if you enable cudagraph capture, the only mode we support is to defer" -- the reason is that cudagraph itself is advanced & has extra memory overhead, and you probably should have good hardware to run
- here I'm saying "if you disable cudagraph, you can choose to defer or not to defer" -- point is that the memory overhead (defer) vs. comm & no-fusion overhead (not defer) is a real tradeoff for anyone not using cudagraph.
There was a problem hiding this comment.
I think when gradient accumulation is used, people are memory hungry cudagraph or not. So the original design doesn't separate the two. But let's do what you propose for now. If there are use cases, we can extend the support, it is not hard.
| if self.sdc_replayer is not None and accumulation_index == 0: | ||
| loss = self.sdc_replayer.run_fwd_bwd( | ||
| forward_backward, step=self.num_completed_steps + 1 | ||
| def _gradient_accumulation_body( |
There was a problem hiding this comment.
given that from outside it makes more sense to call forward_backward as the training_engine interface, should we
- call "gradient_accumulation"
forward_backward - call
forward_backward_microbatch / microbatchesorforward_backward_step-- I prefer the former so "microbatch(es)" is always explicit; otherwise it's all grad accumulation steps.
There was a problem hiding this comment.
Please check if we both agree on the new naming.
[ghstack-poisoned]
tianyu-l
left a comment
There was a problem hiding this comment.
overall looks great -- had a couple of final questions on logic, and some nits on naming & UX
| return loss | ||
|
|
||
| def _pp_forward_backward_body( | ||
| def _pp_forward_backward_microbatches( |
There was a problem hiding this comment.
| def _pp_forward_backward_microbatches( | |
| def _pp_forward_backward_microbatch_group( |
so that we only use microbatch / microbatch_group / microbatch_groups, not microbatches
| if self.config.training.disable_cuda_graphs: | ||
| return | ||
|
|
||
| cuda_graph_forward_backward = partial( |
There was a problem hiding this comment.
| cuda_graph_forward_backward = partial( | |
| eager_forward_backward_fn = partial( |
| defer_fsdp_gradient_reduction = ( | ||
| defer_fsdp_gradient_reduction | ||
| and self.parallel_dims.fsdp_enabled | ||
| and len(microbatch_groups) > 1 |
There was a problem hiding this comment.
don't need this special handling?
| ) | ||
| return arg_mbs, kwarg_mbs, target_mbs | ||
|
|
||
| def _forward_backward_microbatch_groups( |
There was a problem hiding this comment.
| def _forward_backward_microbatch_groups( | |
| def _forward_backward_body( |
which
- more or less matches the public facing
forward_backward() - save some word, as we never needed to define a function
xxx_microbatch_groups - more readable as "_body" is what cudagraph captures
|
|
||
| defer_fsdp_gradient_reduction = ( | ||
| defer_fsdp_gradient_reduction | ||
| and self.parallel_dims.fsdp_enabled |
There was a problem hiding this comment.
somehow feel this is not needed as well, as
- we apply fully_shard even when dp=cp=1
- it should be no op
There was a problem hiding this comment.
uh, right, we always apply fsdp.
| labels=target_mbs, | ||
| loss_kwargs={"global_valid_tokens": global_valid_tokens}, | ||
| ) | ||
| run_microbatch_groups = partial( |
There was a problem hiding this comment.
I would avoid defining this at all.
if sdc_replay:
self.sdc_replayer.run_fwd_bwd(partial(), ...)
else:
result = self._run_forward_backward(preprocessed_microbatch_groups, global_valid_tokens)
|
|
||
| cuda_graph_forward_backward = partial( | ||
| self._forward_backward_microbatch_groups, | ||
| defer_fsdp_gradient_reduction=True, |
There was a problem hiding this comment.
raise value error here if user didn't set it to True
| """Select eager or CUDA graph execution for gradient accumulation.""" | ||
|
|
||
| self._run_gradient_accumulation = self._gradient_accumulation_body | ||
| if self.config.training.disable_cuda_graphs: |
There was a problem hiding this comment.
I think we need that check as well. The point is that
- on a hardware that doesn't support cudagraph, even if by default disable_cudagraph is false, the code could still run without attempting to capture cudagraph
| defer_fsdp_gradient_reduction | ||
| and self.config.parallelism.fsdp_reshard_after_forward != "never" | ||
| ): | ||
| raise ValueError( |
There was a problem hiding this comment.
can happen in post_init of ParallelismConfig, if my earlier suggestion is taken? Not clear why cudagraph makes the difference to error out here.
Actually -- why would cudagraph requires defer + never RAF to be the same? Can I simultaneously
- enable cudagraph
- reshard after forward, and always all-gather again
- but don't reduce-scatter gradient (aka defer)
There was a problem hiding this comment.
Actually -- why would cudagraph requires defer + never RAF to be the same? Can I simultaneously
enable cudagraph
reshard after forward, and always all-gather again
but don't reduce-scatter gradient (aka defer)
This is okay, but I feel that contradict to the argument that with CUDAGraph, we always do defer because that means users have good hardwares. If users have good, why would they ever want to do reshard after forward ?
I don't mind removing the restriction but would like to understand the rationale of why cudagraph enablement makes this special choice.
I would first move the check to post_init but if we later decide to support RAF, I will remove it from the final version.
There was a problem hiding this comment.
a point that maybe minor or not minor:
repeating all-gather (RAF) won't change numerics or lose grad fusion opportunity, but sync gradients across microbatches will -- which may make RAF more favorable when you are really in need for some more memory to trade
| fsdp_root.set_is_last_backward(is_last) | ||
| fsdp_root.set_reshard_after_backward(is_last) | ||
| fsdp_root.set_requires_gradient_sync(is_last) | ||
| elif self.parallel_dims.dp_replicate_enabled: |
There was a problem hiding this comment.
this condition should be outside "if PP else non-PP" -- it matters for PP as well
tianyu-l
left a comment
There was a problem hiding this comment.
looks great! some final comments
| is_last_accumulation_step = accumulation_index == num_accumulation_steps - 1 | ||
|
|
||
| if ( | ||
| not defer_fsdp_gradient_reduction |
There was a problem hiding this comment.
Is this condition saying
- if defer, then we don't need special treatment for HSDP -- as anyway it'll only incur all-reduce at the final accumulation step
I think it's fine, but I also believe it works well if we don't have this line, for simplicity?
There was a problem hiding this comment.
maybe we should change one of them to SAF="never" as it'd be used to give the best perf
[ghstack-poisoned]
Stack from ghstack (oldest at bottom):
Conflict with #4649, need to discuss how to incorporate with #4649
Depend on pytorch/pytorch#196640 and pytorch/pytorch#196641.
parallelism.fsdp_defer_gradient_reductionto reduce gradients after the final backward of a gradient accumulation group.