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
11 changes: 11 additions & 0 deletions megatron/core/optimizer/layer_wise_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,14 @@ def sharded_state_dict(
group['step'] = step

return sharded_state_dict

def save_state_dict_to_file(self, filename: str) -> None:
"""Save the parameter state of the optimizer. For torch format only.
Args:
filename: The filename to save the parameter state.
"""
torch.save(super().state_dict(), filename)

def load_state_dict_from_file(self, filename: str) -> None:
"""Load the parameter state of the optimizer. For torch format only."""
super().load_state_dict(torch.load(filename))
15 changes: 14 additions & 1 deletion megatron/training/checkpointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,14 @@ def save_checkpoint(iteration, model, optimizer, opt_param_scheduler, num_floati
ensure_directory_exists(optim_checkpoint_name)
if not optimizer.is_stub_optimizer:
optimizer.save_parameter_state(optim_checkpoint_name)

# LayerWiseDistributedOptimizer save optimizer state to file on different ranks
if getattr(args, "optimizer", "adam").startswith("dist_") and args.ckpt_format == 'torch':
dp_rank = mpu.get_data_parallel_rank()
optim_checkpoint_name = os.path.join(os.path.dirname(checkpoint_name), f"layer_wise_optimizer_{dp_rank}.pt")
ensure_directory_exists(optim_checkpoint_name)
if not optimizer.is_stub_optimizer:
optimizer.save_state_dict_to_file(optim_checkpoint_name)

async_save_request = None
if args.async_save:
Expand Down Expand Up @@ -1700,7 +1708,12 @@ def load_model_state_dict(module, state_dict, strict: bool):
if not release and not args.finetune and not args.no_load_optim:
try:
# Load state dict.
if not skip_load_to_model_and_opt and optimizer is not None and not optimizer.is_stub_optimizer:
if getattr(args, "optimizer", "adam").startswith("dist_") and args.ckpt_format == 'torch':
# LayerWiseDistributedOptimizer load optimizer state from file on different ranks
dp_rank = mpu.get_data_parallel_rank()
optim_checkpoint_name = os.path.join(os.path.dirname(checkpoint_name), f"layer_wise_optimizer_{dp_rank}.pt")
optimizer.load_state_dict_from_file(optim_checkpoint_name)
elif not skip_load_to_model_and_opt and optimizer is not None and not optimizer.is_stub_optimizer:
optimizer.load_state_dict(state_dict['optimizer'])

# Load distributed optimizer's custom parameter state.
Expand Down
Loading