Repository navigation
Adding code for Flextron #4429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding code for Flextron #4429
Changes from 17 commits
fa17762
cb56a0f
4c18286
c38d1f1
3b8f6ff
892ca2f
8b9ffae
044965d
aab70ea
c03c840
93608a8
3c864d3
b53eba1
e8c12da
e05503d
fe19e6a
115f5b5
7f2b92f
0ae4d4d
39b93a3
6b742b6
77bd779
0f0ac6f
f871331
32c7d09
160336b
e975d5e
33b5c9e
96c8e5c
1570c80
b8bdfd6
9311c95
0894912
41e8976
c2f58f9
f68b0e6
3292cf7
7e4c52e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| # Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
|
|
||
| from functools import partial | ||
| from typing import Callable, List, Optional, Union | ||
| from typing import Callable, Dict, List, Optional, Union | ||
|
|
||
| import torch | ||
| from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors | ||
|
|
@@ -275,6 +275,44 @@ def _allreduce_position_embedding_grads( | |
| ) | ||
|
|
||
|
|
||
| def _allreduce_router_grads(model: List[torch.nn.Module], config: TransformerConfig): | ||
| """ | ||
| All-reduce router grads. | ||
|
|
||
| Reduce grads across all the pp stages to ensure that parameters of the router stay in sync. | ||
| """ | ||
|
|
||
| if parallel_state.get_pipeline_model_parallel_world_size() > 1: | ||
| grads_dict: Dict[str, List[torch.Tensor]] = {} | ||
| for model_chunk in model: | ||
| for name, param in get_attr_wrapped_model(model_chunk, 'named_parameters')(): | ||
| if param.requires_grad and getattr(param, 'pipeline_parallel', False): | ||
| grad = param.main_grad | ||
| if name in grads_dict: | ||
| # Add all the virtual PP rank's gradients to | ||
| # the first local virtual PP rank. | ||
| grads_dict[name][0].add_(grad) | ||
| # Append to the end for later update after cross-rank reduce. | ||
| grads_dict[name].append(grad) | ||
| else: | ||
| grads_dict[name] = [grad] | ||
|
|
||
| if grads_dict: | ||
| # All-reduce the gradient on the first VPP rank. | ||
| grads = [param_grad[0] for _, param_grad in grads_dict.items()] | ||
| coalesced = _flatten_dense_tensors(grads) | ||
| torch.distributed.all_reduce( | ||
| coalesced, group=parallel_state.get_pipeline_model_parallel_group() | ||
| ) | ||
| for buf, synced in zip(grads, _unflatten_dense_tensors(coalesced, grads)): | ||
| buf.copy_(synced) | ||
|
|
||
| # Update the gradients on other VPP ranks. | ||
| for grads in grads_dict.values(): | ||
| for grad in grads[1:]: | ||
| grad.copy_(grads[0]) | ||
|
|
||
|
Comment on lines
+278
to
+314
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new function modifies a core distributed file but has no unit test coverage. A test verifying the all-reduce behavior (especially the VPP gradient aggregation logic in lines 291-313) would help prevent regressions, since bugs here would silently produce incorrect router gradients across pipeline stages. |
||
|
|
||
| def reset_model_temporary_tensors(config: TransformerConfig, model: List[torch.nn.Module]): | ||
| """ | ||
| Reset the temporary tensors of the model. | ||
|
|
@@ -457,6 +495,9 @@ def finalize_model_grads( | |
| if config.timers is not None: | ||
| config.timers('conditional-embedder-grads-all-reduce').stop() | ||
|
|
||
| if getattr(config, 'flextron', False): | ||
| _allreduce_router_grads(model, config) | ||
|
|
||
| # All-reduce layer-norm grads (for sequence parallelism) and non-tensor parallel modules. | ||
| if config.timers is not None: | ||
| config.timers('non-tensor-parallel-grads-all-reduce', log_level=1).start( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we improve the attribute name? IIUC,
pipeline_parallelmeans the parameters need to stay in sync across PP stages? If so, I think this attribute name needs to be a bit more descriptive.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to "flextron_router_pp_sync" in latest commit