Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions megatron/core/distributed/fsdp/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,22 @@ Megatron-FSDP's `fully_shard_*` API has a comprehensive set of arguments for fin
- **Only effective when using with Megatron-Core.**
- Defaults to `False`.
- By default we try to use NCCL window (symmetric) registration if it is available. If not it falls back to conventional local registration.
- `fsdp_manual_registration` will manually register the FSDP communication buffers with the NCCL user buffer. For symmetric registration with large models, the registration itself can take a significant amount of time. This option minimizes the number of registration calls to reduce the registration time. However, with this option enabled, you need to manually call the `ParamAndGradBuffer.manual_buffer_registration()` function after the first iteration. This is already implemented in the Megatron-LM training loop. In other use cases, users are expected to call this function themselves.
- `fsdp_manual_registration` will manually register the FSDP communication buffers with the NCCL user buffer. For symmetric registration with large models, the registration itself can take a significant amount of time. This option minimizes the number of registration calls to reduce the registration time. However, with this option enabled, you need to manually call the `ParamAndGradBuffer.manual_buffer_registration()` function after the first iteration. This is already implemented in the Megatron-LM training loop. In other use cases, users are expected to call this function themselves.
- This is an example of required modification in the training loop.
```python
def train(...):
...
# After the first iteration, user need to call the
# ParamAndGradBuffer.manual_buffer_registration() function in the training loop
if (iteration == start_iteration + 1):
if isinstance(model, megatron_FSDP) and model.ddp_config.fsdp_manual_registration:
param_and_grad_buffer = getattr(model, "param_and_grad_buffer", None)
if param_and_grad_buffer is not None:
param_and_grad_buffer.manual_buffer_registration()
```
- **Only effective when using with Megatron-Core.**
- This option is only effective when `nccl_ub` is enabled.
- Defaults to `False`.
- Defaults to `False`, but will be automatically enabled in Megatron-LM.
- `disable_symmetric_registration` will disable NCCL window (i.e. symmetric) registration when using `nccl_ub`.
- Defaults to `False`.
- `fsdp_double_buffer` will use persistently allocated double buffers for temporarily-defined memory needed in `MegatronFSDP` communications. Having persistent double buffers may increase peak VRAM utilization, but is required to register NCCL user buffers (`nccl_ub=True`) for `MegatronFSDP`. Currently, this is only supported for simple repetitive model structures such as GPT.
Expand Down
8 changes: 7 additions & 1 deletion megatron/training/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,13 @@ def validate_args(args, defaults={}):

assert args.ckpt_format == "fsdp_dtensor", \
"Megatron-FSDP requires the `fsdp_dtensor` checkpointing format."


if args.nccl_ub and args.use_megatron_fsdp:
# In Megatron-LM, required implementation for manual registration is already provided.
# So we enable the manual registration by default when nccl-ub and use_megatron_fsdp is set.
args.fsdp_manual_registration = True
warn_rank_0('FSDP manual registration is enabled by default when nccl-ub is enabled')

if args.fsdp_manual_registration:
assert args.use_megatron_fsdp, "FSDP manual registration is only supported with Megatron FSDP."
assert args.nccl_ub, "FSDP manual registration is only supported with --nccl-ub argument."
Expand Down
7 changes: 4 additions & 3 deletions megatron/training/training.py
Original file line number Diff line number Diff line change
Expand Up @@ -3122,16 +3122,17 @@ def trace_handler(p):
# If requested, manually register FSDP communication buffers after a short warmup.
if (
getattr(args, "fsdp_manual_registration", False)
and getattr(args, "nccl_ub", False)
and getattr(args, "use_megatron_fsdp", False)
and iteration == start_iteration + 1
):
for model_chunk in model:
if isinstance(model_chunk, megatron_FSDP) and getattr(
model_chunk.ddp_config, "fsdp_manual_registration", False
):
pad_buf = getattr(model_chunk, "param_and_grad_buffer", None)
if pad_buf is not None:
pad_buf.manual_buffer_registration()
param_and_grad_buffer = getattr(model_chunk, "param_and_grad_buffer", None)
if param_and_grad_buffer is not None:
param_and_grad_buffer.manual_buffer_registration()

if args.perform_rl_step and args.rl_use_sequence_packing:
iteration_sequences = rl_utils.get_iteration_sequence_count(args)
Expand Down
Loading