Repository navigation
Fix Megatron-FSDP checkpoint save failure #2138
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
Changes from 1 commit
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 |
|---|---|---|
|
|
@@ -9,6 +9,8 @@ | |
| import torch | ||
| import torch.distributed.checkpoint | ||
|
|
||
| from megatron.core.distributed import DistributedDataParallelConfig | ||
| from megatron.core.distributed.fsdp.mcore_fsdp_adapter import FullyShardedDataParallel | ||
| from megatron.core.num_microbatches_calculator import ( | ||
| init_num_microbatches_calculator, | ||
| unset_num_microbatches_calculator, | ||
|
|
@@ -111,6 +113,8 @@ def create_args(): | |
| args.retro_add_retriever = False | ||
| args.ckpt_convert_update_legacy_dist_opt_format = False | ||
| args.ckpt_step = None | ||
| args.swiglu = True | ||
| args.num_experts = 1 | ||
|
|
||
| yield args | ||
|
|
||
|
|
@@ -191,7 +195,7 @@ def test_load_base_checkpoint( | |
| assert ckpt_type == expected_ckpt_type | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("ckpt_format", ["torch", "torch_dcp"]) | ||
| @pytest.mark.parametrize("ckpt_format", ["torch", "torch_dcp", "fsdp_dtensor"]) | ||
| def test_save_checkpoint(init_model_parallel, create_args, tmp_path_dist_ckpt, ckpt_format): | ||
| """Test save_checkpoint.""" | ||
| args = create_args | ||
|
|
@@ -202,11 +206,21 @@ def test_save_checkpoint(init_model_parallel, create_args, tmp_path_dist_ckpt, c | |
|
|
||
| args.use_distributed_optimizer = ckpt_format != "torch_dcp" | ||
| args.use_dist_ckpt = ckpt_format != "torch" | ||
| args.use_megatron_fsdp = ckpt_format == "fsdp_dtensor" | ||
|
|
||
| iteration = 123 | ||
| config = TransformerConfig(num_layers=1, kv_channels=1) | ||
| model = MockModel(config) | ||
| optimizer = MockState({"optimizer": "optimizer_state"}) | ||
| if ckpt_format == "fsdp_dtensor": | ||
| model = FullyShardedDataParallel( | ||
| config=config, | ||
| ddp_config=DistributedDataParallelConfig( | ||
| use_distributed_optimizer=True, use_megatron_fsdp=True | ||
| ), | ||
| module=model, | ||
| ) | ||
| optimizer = MockState({"state": {}}) | ||
| opt_param_scheduler = MockState({"opt_param_scheduler": "scheduler_state"}) | ||
| num_floating_point_operations_so_far = 456 | ||
|
|
||
|
|
@@ -228,6 +242,8 @@ def test_save_checkpoint(init_model_parallel, create_args, tmp_path_dist_ckpt, c | |
| expected_ckpt_path = ckpt_dir / "mp_rank_00" / "model_optim_rng.pt" | ||
| elif ckpt_format == "torch_dcp": | ||
| expected_ckpt_path = ckpt_dir / ".metadata" | ||
| elif ckpt_format == "fsdp_dtensor": | ||
|
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. Minor issue, this if condition is running the same code as
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. @BoxiangW If you want to apply this structurally across the codebase, ruff can be used to automatically lint and fix these duplicated if else codepaths.
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. cc @ko3n1g, is there any plan on switching to ruff instead? I know that all other repos are using it already
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. We have some pretty carefully chosen, mostly non-opinionated ruff rules chosen in PyTorch: https://github.com/pytorch/pytorch/blob/a74fe75c450277eb88a95c764e8b0a664a550a86/pyproject.toml#L130 This specific one we did not enable because of breakpoint debugging / traceback reasons, but on a smaller codebase like this more easy to enforce opinionated lints.
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. Do you want to create a separate issue for this? It seems would be very helpful for improving Megatron-Core's formatting
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. More for catching bugs lol, I've found several bugs with ruff that should have not made it past the CI into Megatron. Do you mean issue for this specific rule or enabling ruff more broadly.
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. Got it, current lint is more for formatting and import checks.
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. hey @Skylion007, we had to put the plans to switch to ruff on ice for a bit due to the team impact when handling a code-base of this size. We hope that we can pick those plan up again end of the year. Thanks for the tooling you shared!
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. I enabled most of the rules for most of PyTorch, which is a way larger codebase if you want to discuss how to enable it incrementally @ko3n1g |
||
| expected_ckpt_path = ckpt_dir / ".metadata" | ||
|
|
||
| assert os.path.exists(expected_ckpt_path) | ||
|
|
||
|
|
||
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.
Is this line in use?