Repository navigation
Thread process groups through training checkpoint paths #5486
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
Merged
yashaswikarnati
merged 13 commits into
NVIDIA:main
from
yashaswikarnati:codex/pr5377-pg-threading
Jun 25, 2026
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
52348a0
Thread process groups through training checkpoint paths
yashaswikarnati c8f7199
Remove redundant MIMO optimizer comment
yashaswikarnati b45f91b
Address process group review comments
yashaswikarnati 51c4ed9
Thread RNG checkpoint and seed groups
yashaswikarnati 6874d28
Move RNG key prefix after checkpoint groups
yashaswikarnati 8f67657
Use DP CP fallback for checkpoint RNG state
yashaswikarnati 5e414fb
Use rank-local TP size for JIT warmup
yashaswikarnati 307af54
Thread expert DP group into checkpoint save
yashaswikarnati 04098c7
Use DP group for checkpoint RNG list indexing
yashaswikarnati 7e188cc
Thread seed groups through initialize
yashaswikarnati 6e3704f
Use threaded ranks for legacy checkpoint save gate
yashaswikarnati 1bdd7a9
Remove redundant training PGC comment
yashaswikarnati 2b23843
Add MIMO optimizer method docstrings
yashaswikarnati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -368,8 +368,18 @@ def read_metadata(tracker_filename): | |
| return max_iter, release | ||
|
|
||
|
|
||
| def get_rng_state(ckpt_format: str, tp_group: torch.distributed.ProcessGroup, pp_group: torch.distributed.ProcessGroup) -> Union[List[Dict[str, Any]], ShardedObject]: | ||
| """Collect rng state across data parallel ranks.""" | ||
| def get_rng_state( | ||
| ckpt_format: str, | ||
| tp_group: torch.distributed.ProcessGroup, | ||
| pp_group: torch.distributed.ProcessGroup, | ||
| dp_cp_group: Optional[torch.distributed.ProcessGroup] = None, | ||
| key_prefix: str = '', | ||
| ) -> Union[List[Dict[str, Any]], ShardedObject]: | ||
| """Collect rng state across data parallel ranks. | ||
|
|
||
| dp_cp_group threads the data-parallel (with context-parallel) group; None falls back to the mpu API. | ||
| key_prefix namespaces the rng ShardedObject key so disjoint grids avoid a key collision (default ''). | ||
| """ | ||
| args = get_args() | ||
| rng_state = { | ||
| 'random_rng_state': random.getstate(), | ||
|
|
@@ -378,28 +388,39 @@ def get_rng_state(ckpt_format: str, tp_group: torch.distributed.ProcessGroup, pp | |
| 'cuda_rng_state': torch.cuda.get_rng_state(), | ||
| 'rng_tracker_states': tensor_parallel.get_cuda_rng_tracker().get_states()} | ||
|
|
||
| dp_cp_world_size = ( | ||
| get_pg_size(dp_cp_group) | ||
| if dp_cp_group is not None | ||
| else mpu.get_data_parallel_world_size(with_context_parallel=True) | ||
| ) | ||
| rng_state_list = None | ||
| if args.data_parallel_random_init and torch.distributed.is_initialized() and \ | ||
| mpu.get_data_parallel_world_size() > 1: | ||
| dp_cp_world_size > 1: | ||
| rng_state_list = \ | ||
| [None for i in range(mpu.get_data_parallel_world_size())] | ||
| [None for i in range(dp_cp_world_size)] | ||
| torch.distributed.all_gather_object( | ||
| rng_state_list, | ||
| rng_state, | ||
| group=mpu.get_data_parallel_group()) | ||
| group=( | ||
| dp_cp_group | ||
| if dp_cp_group is not None | ||
| else mpu.get_data_parallel_group(with_context_parallel=True) | ||
| ), | ||
| ) | ||
| else: | ||
| rng_state_list = [rng_state] | ||
|
|
||
| dp_cp_rank = get_pg_rank(dp_cp_group) if dp_cp_group is not None else mpu.get_data_parallel_rank(with_context_parallel=True) | ||
| if ckpt_format == "torch_dist": | ||
| pp_rank = get_pg_rank(pp_group) | ||
| pp_size = get_pg_size(pp_group) | ||
| tp_rank = get_pg_rank(tp_group) | ||
| tp_size = get_pg_size(tp_group) | ||
| rng_state_list = ShardedObject('rng_state', rng_state_list, (pp_size, tp_size), (pp_rank, tp_rank), | ||
| replica_id=mpu.get_data_parallel_rank(with_context_parallel=True)) | ||
| rng_state_list = ShardedObject(f'{key_prefix}rng_state', rng_state_list, (pp_size, tp_size), (pp_rank, tp_rank), | ||
| replica_id=dp_cp_rank) | ||
| elif ckpt_format == "fsdp_dtensor": | ||
| pp_rank = mpu.get_pipeline_model_parallel_rank() | ||
| tp_rank = mpu.get_tensor_model_parallel_rank() | ||
| pp_rank = get_pg_rank(pp_group) | ||
| tp_rank = get_pg_rank(tp_group) | ||
| rng_state_list = { | ||
| f"({pp_rank}, {tp_rank})": rng_state_list | ||
| } | ||
|
|
@@ -494,7 +515,7 @@ def save_grads(save_dir, state_dict, iteration, grad_label): | |
|
|
||
| def save_checkpoint(iteration, model, optimizer, opt_param_scheduler, num_floating_point_operations_so_far, | ||
| checkpointing_context=None, pipeline_rank=None, expert_rank=None, tensor_rank=None, pipeline_parallel=None, expert_parallel=None, non_persistent_ckpt=False, | ||
| train_data_iterator=None, preprocess_common_state_dict_fn = None, release=False, tp_group: Optional[torch.distributed.ProcessGroup] = None, pp_group: Optional[torch.distributed.ProcessGroup] = None, dp_cp_group: Optional[torch.distributed.ProcessGroup] = None): | ||
| train_data_iterator=None, preprocess_common_state_dict_fn = None, release=False, tp_group: Optional[torch.distributed.ProcessGroup] = None, pp_group: Optional[torch.distributed.ProcessGroup] = None, dp_cp_group: Optional[torch.distributed.ProcessGroup] = None, rng_state_key_prefix: str = ''): | ||
| """Save a model, optimizer and optionally dataloader checkpoint. | ||
|
|
||
| Checkpointing context is used to persist some checkpointing state | ||
|
|
@@ -557,7 +578,9 @@ def save_checkpoint(iteration, model, optimizer, opt_param_scheduler, num_floati | |
| if tp_group is None and pp_group is None: | ||
| tp_group = mpu.get_tensor_model_parallel_group() | ||
| pp_group = mpu.get_pipeline_model_parallel_group() | ||
| rng_state = get_rng_state(args.ckpt_format, tp_group, pp_group) | ||
| rng_state = get_rng_state(args.ckpt_format, tp_group, pp_group, | ||
| dp_cp_group=dp_cp_group, | ||
| key_prefix=rng_state_key_prefix) | ||
|
|
||
| # Collect rerun state across all ranks | ||
| rerun_state_machine = get_rerun_state_machine() | ||
|
|
@@ -609,9 +632,9 @@ def save_checkpoint(iteration, model, optimizer, opt_param_scheduler, num_floati | |
| # the dense and expert parallelism layouts disagree (e.g. TP > EP*ETP); the union | ||
| # does, with at most one rank per (tp_rank, ep_rank) inside any DP group. | ||
| if not torch.distributed.is_initialized() \ | ||
| or ckpt_type != CheckpointType.LEGACY \ | ||
| or mpu.get_data_parallel_rank() == 0 \ | ||
| or mpu.get_expert_data_parallel_rank() == 0 \ | ||
| or ckpt_type != CheckpointType.LEGACY: | ||
| or mpu.get_expert_data_parallel_rank() == 0: | ||
| if ckpt_type != CheckpointType.LEGACY: | ||
| sharded_sd_metadata = _build_sharded_state_dict_metadata(args, dp_cp_group=dp_cp_group) | ||
| if args.use_distributed_optimizer: | ||
|
|
@@ -664,8 +687,9 @@ def save_checkpoint(iteration, model, optimizer, opt_param_scheduler, num_floati | |
|
|
||
| if args.ckpt_fully_parallel_save: | ||
| if args.ckpt_fully_parallel_save_process_group == 'dp': | ||
| process_group = mpu.get_data_parallel_group(with_context_parallel=True) | ||
| process_group = dp_cp_group if dp_cp_group is not None else mpu.get_data_parallel_group(with_context_parallel=True) | ||
| elif args.ckpt_fully_parallel_save_process_group == 'ep_dp': | ||
| # NOTE: expert-DP save group not threaded yet; falls back to mpu (not on the disjoint-grid path). | ||
|
Contributor
Author
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. why # NOTE: expert-DP save group not threaded yet; falls back to mpu (not on the disjoint-grid path). ? |
||
| process_group = mpu.get_expert_data_parallel_group() | ||
| save_strategy = FullyParallelSaveStrategyWrapper(save_strategy, process_group, | ||
| args.ckpt_assume_constant_structure) | ||
|
|
@@ -1631,7 +1655,7 @@ def _set_arg(arg_name, old_arg_name=None, force=False): | |
|
|
||
|
|
||
| def load_checkpoint(ddp_model, optimizer, opt_param_scheduler, load_arg='load', strict=True, | ||
| checkpointing_context=None, skip_load_to_model_and_opt=False, tp_group: Optional[torch.distributed.ProcessGroup] = None, pp_group: Optional[torch.distributed.ProcessGroup] = None, dp_cp_group: Optional[torch.distributed.ProcessGroup] = None): | ||
| checkpointing_context=None, skip_load_to_model_and_opt=False, tp_group: Optional[torch.distributed.ProcessGroup] = None, pp_group: Optional[torch.distributed.ProcessGroup] = None, dp_cp_group: Optional[torch.distributed.ProcessGroup] = None, rng_state_key_prefix: str = ''): | ||
| """Load a model checkpoint and return the iteration. | ||
| strict (bool): whether to strictly enforce that the keys in | ||
| :attr:`state_dict` of the checkpoint match the names of | ||
|
|
@@ -1716,15 +1740,17 @@ def load_checkpoint(ddp_model, optimizer, opt_param_scheduler, load_arg='load', | |
| if tp_group is None and pp_group is None: | ||
| tp_group = mpu.get_tensor_model_parallel_group() | ||
| pp_group = mpu.get_pipeline_model_parallel_group() | ||
| gen_sd_rng_state = get_rng_state(args.ckpt_format, tp_group, pp_group) # we can load the rng state | ||
| gen_sd_rng_state = get_rng_state(args.ckpt_format, tp_group, pp_group, | ||
| dp_cp_group=dp_cp_group, | ||
| key_prefix=rng_state_key_prefix) # we can load the rng state | ||
| else: | ||
| ignore_rng_state = True | ||
| gen_sd_rng_state = None | ||
| if ckpt_tp_pp != run_tp_pp: | ||
| print_rank_0("{}: RNG state will be ignored".format(mismatch_msg)) | ||
|
|
||
| if ckpt_type == CheckpointType.LOCAL: | ||
| sharded_sd_metadata = _build_sharded_state_dict_metadata(args) | ||
| sharded_sd_metadata = _build_sharded_state_dict_metadata(args, dp_cp_group=dp_cp_group) | ||
| else: | ||
| sharded_sd_metadata = dist_checkpointing.load_content_metadata(preloaded_state_dict=state_dict) | ||
| print_rank_0(f'sharded_state_dict metadata loaded from the checkpoint: {sharded_sd_metadata}') | ||
|
|
@@ -1820,7 +1846,7 @@ def load_checkpoint(ddp_model, optimizer, opt_param_scheduler, load_arg='load', | |
| "optimizer": optimizer_sd, | ||
| "args": None, | ||
| "iteration": 1, | ||
| "rng_state": get_rng_state(args.ckpt_format, tp_group, pp_group), | ||
| "rng_state": get_rng_state(args.ckpt_format, tp_group, pp_group, dp_cp_group=dp_cp_group), | ||
| "checkpoint_version": None, | ||
| "opt_param_scheduler": opt_param_scheduler.state_dict(), | ||
| "num_floating_point_operations_so_far": 0, | ||
|
|
@@ -1843,12 +1869,17 @@ def load_checkpoint(ddp_model, optimizer, opt_param_scheduler, load_arg='load', | |
| data_iterator=None, ckpt_format=ckpt_format, force=True, | ||
| ) | ||
| if not args.no_load_rng: | ||
| gen_sd_rng_state = get_rng_state(args.ckpt_format, tp_group, pp_group) | ||
| gen_sd_rng_state = get_rng_state( | ||
| args.ckpt_format, tp_group, pp_group, dp_cp_group=dp_cp_group | ||
| ) | ||
| if not args.no_load_optim: | ||
| gen_sd_optim = optimizer | ||
| gen_sd_opt_param_scheduler = opt_param_scheduler | ||
|
|
||
| optim_sd_kwargs = dict(metadata=_build_sharded_state_dict_metadata(args), is_loading=True) | ||
| optim_sd_kwargs = dict( | ||
| metadata=_build_sharded_state_dict_metadata(args, dp_cp_group=dp_cp_group), | ||
| is_loading=True, | ||
| ) | ||
|
|
||
| state_dict = generate_state_dict( | ||
| args, | ||
|
|
@@ -2021,8 +2052,8 @@ def load_model_state_dict(module, state_dict, strict: bool): | |
| if 'rng_state' in state_dict: | ||
| if args.ckpt_format == "fsdp_dtensor": | ||
| # FSDP DTensor checkpoints store rng_state in a different format. | ||
| tp_rank = mpu.get_tensor_model_parallel_rank() | ||
| pp_rank = mpu.get_pipeline_model_parallel_rank() | ||
| tp_rank = get_pg_rank(tp_group) if tp_group is not None else mpu.get_tensor_model_parallel_rank() | ||
| pp_rank = get_pg_rank(pp_group) if pp_group is not None else mpu.get_pipeline_model_parallel_rank() | ||
| if f"({pp_rank}, {tp_rank})" in state_dict['rng_state']: | ||
| rng_state = state_dict['rng_state'][f"({pp_rank}, {tp_rank})"] | ||
| else: | ||
|
|
@@ -2033,7 +2064,12 @@ def load_model_state_dict(module, state_dict, strict: bool): | |
|
|
||
| # access rng_state for data parallel rank | ||
| if args.data_parallel_random_init: | ||
| rng_state = rng_state[mpu.get_data_parallel_rank()] | ||
| dp_cp_rank = ( | ||
|
Contributor
Author
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 should be dp rank? not dp cp ? |
||
| get_pg_rank(dp_cp_group) | ||
| if dp_cp_group is not None | ||
| else mpu.get_data_parallel_rank(with_context_parallel=True) | ||
| ) | ||
| rng_state = rng_state[dp_cp_rank] | ||
| else: | ||
| rng_state = rng_state[0] | ||
| random.setstate(rng_state['random_rng_state']) | ||
|
|
@@ -2071,9 +2107,13 @@ def load_model_state_dict(module, state_dict, strict: bool): | |
| if torch.distributed.is_initialized(): | ||
| torch.distributed.barrier() | ||
|
|
||
| _tp_r = get_pg_rank(tp_group) if tp_group is not None else mpu.get_tensor_model_parallel_rank() | ||
| _tp_w = get_pg_size(tp_group) if tp_group is not None else mpu.get_tensor_model_parallel_world_size() | ||
| _pp_r = get_pg_rank(pp_group) if pp_group is not None else mpu.get_pipeline_model_parallel_rank() | ||
| _pp_w = get_pg_size(pp_group) if pp_group is not None else mpu.get_pipeline_model_parallel_world_size() | ||
| print_rank_0(f' successfully loaded checkpoint from {load_dir} ' | ||
| f'[ t {mpu.get_tensor_model_parallel_rank() + 1}/{mpu.get_tensor_model_parallel_world_size()}, ' | ||
| f'p {mpu.get_pipeline_model_parallel_rank() + 1}/{mpu.get_pipeline_model_parallel_world_size()} ] ' | ||
| f'[ t {_tp_r + 1}/{_tp_w}, ' | ||
| f'p {_pp_r + 1}/{_pp_w} ] ' | ||
| f'at iteration {iteration}') | ||
|
|
||
| # Additional callback for wandb (last rank) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
whats the change at this line? why are we still using mpu groups here ?