diff --git a/megatron/core/distributed/fsdp/src/README.md b/megatron/core/distributed/fsdp/src/README.md index 3603fc105a2..98c01a759eb 100644 --- a/megatron/core/distributed/fsdp/src/README.md +++ b/megatron/core/distributed/fsdp/src/README.md @@ -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. diff --git a/megatron/training/arguments.py b/megatron/training/arguments.py index 4e8de13bd54..ba4099c3b71 100644 --- a/megatron/training/arguments.py +++ b/megatron/training/arguments.py @@ -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." diff --git a/megatron/training/training.py b/megatron/training/training.py index 156fff6fb58..d8825a7b52f 100644 --- a/megatron/training/training.py +++ b/megatron/training/training.py @@ -3122,6 +3122,7 @@ 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 ): @@ -3129,9 +3130,9 @@ def trace_handler(p): 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)