Skip to content

Conversation

LucasLLC
Copy link
Contributor

@LucasLLC LucasLLC commented Oct 31, 2023

Adds E2E tests for saving/loading distributed checkpoints. Supported so far are:

  • FSDP
  • HSDP
  • FSDP + TP

Each method is also tested using torch.compile

To run all tests:
python test/distributed/checkpoint/test/distributed/checkpoint/e2e/test_e2e_save_and_load.py

@pytorch-bot pytorch-bot bot added the topic: not user facing topic category label Oct 31, 2023
Copy link

pytorch-bot bot commented Oct 31, 2023

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/112541

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 044fb94 with merge base 6a3922d (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@LucasLLC LucasLLC requested review from fegin and wz337 October 31, 2023 23:37
@LucasLLC LucasLLC self-assigned this Oct 31, 2023

return model, optim

def _equal_state_dict(self, model_0, model_1):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fegin Is your recommendation (https://github.com/pytorch/pytorch/blob/main/test/distributed/tensor/parallel/test_fsdp_2d_parallel.py#L638-L641) necessary here, or do you think this is sufficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming we'll need something similar to compare optimizers?

Copy link
Contributor

@wz337 wz337 Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chien-Chin has some utils to compare state_dict here. See if anything is useful for you, and maybe we can just create a test utils for these so we can re-use these in tests. https://github.com/pytorch/pytorch/blob/main/test/distributed/checkpoint/test_state_dict.py#L56

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe since I can compare DTensor's directly with torch.equal we don't really need this logic, so I left the model state dict comparison the same for now.

I also opted for a direct comparison of the optim state dict, since it was a bit simpler and that seems to work here as well -

self.assertEqual(optim.state_dict(), new_optim.state_dict())
.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are comparing 2 identical state_dicts, this should be enough. However, if you need to compare with non-converted optimizer state_dict, a customized function is required as non-converted optimizer state use parameter id.

@LucasLLC LucasLLC changed the title e2e test for FSDP in Distributed Checkpointing E2E test for FSDP, HSDP, FSDP+TP in Distributed Checkpointing Nov 1, 2023
@LucasLLC LucasLLC marked this pull request as ready for review November 1, 2023 20:22
@wz337 wz337 added ciflow/trunk Trigger trunk jobs on your pull request release notes: distributed (checkpoint) ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR labels Nov 2, 2023
@wz337
Copy link
Contributor

wz337 commented Nov 2, 2023

Adding a few tags. @LucasLLC Just FYI, the multi-gpu tests do not run by default. To enable the multi gpu tests, we need to add CI/periodic to the PR.

return torch.rand(8, 8, device="cuda")


class ModelType(Enum):
Copy link
Contributor

@wz337 wz337 Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noice!! Very clean!!

Copy link
Member

@H-Huang H-Huang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I would also include an example python / pytest command in the PR summary to include as a reference.

Will defer the approval stamp to the DCP experts :)


class TestE2ELoadAndSave(DTensorTestBase):
def _create_model(self, compile, model_type):
dummy_model = TestDummyModel().cuda()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: moving to cuda necessary? I thought FSDP does it

def test_e2e(self, compile, model_type):
# first create and save a checkpoint
model, optim = self._create_model(compile, model_type)
model_state_dict_0, optim_state_dict_0 = get_state_dict(model, optimizers=optim)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest that we use a non-parallelized model and directly call state_dict as the source of truth to compare.

  1. create a non-parallelized model
  2. create a parallelized model
  3. train both models 2 steps
  4. save the parallelized model
  5. create a new parallelized model
  6. load from the trained parallelized model to the new parallelized model
  7. train another step both the new parallelized model and the non-parallelized model and compare the accuracy.

Copy link
Contributor

@fegin fegin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, we can land this first and improve it to compare the result with a non-parallelized model.

Please fix the lint issue and skip_if_lt_x_gpu issue @wz337 mentioned before landing.

Copy link
Contributor

@wz337 wz337 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for having this ready! Super-duper fast!
With this PR, now we can mark our FSDP, HSDP, 2D checkpointing B/E ready on PT-D feature support matrix! Thanks @LucasLLC!

@LucasLLC
Copy link
Contributor Author

LucasLLC commented Nov 2, 2023

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@fegin
Copy link
Contributor

fegin commented Nov 2, 2023

Try lintrunner locally to get the signal of lint and typing errors.

@LucasLLC
Copy link
Contributor Author

LucasLLC commented Nov 2, 2023

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

)
tp_mesh = mesh_2d["tp"]
dp_mesh = mesh_2d["dp"]
model = parallelize_module(dummy_model, tp_mesh, PairwiseParallel())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow up, we should probably switch PairwiseParallel to Colwise + Rowwise as we plan to deprecate the former soon

Copy link
Contributor

@wz337 wz337 Nov 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LucasLLC

For usage of Colwise + Rowwise, you can refer to this slide. https://docs.google.com/presentation/d/1e9TNYu_u_Hz9IpfhS4_R_77de59g5AnSWEf7U-FDphg/edit#slide=id.g28cb00a468a_0_16

So, for this model, we can specify a parallelize_plan for some of the layers, maybe layer 1 and layer 2.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually since we use sequential in the module definition, I am not sure if PairwiseParallel is working here. So using Colwise + Rowwise might be a necessary. Basically, you need to pass in a dictionary as a plan to parallelize_module with its key equals to module's FQN and value is ColwiseParallel or RowwiseParallel.

xuhancn pushed a commit to xuhancn/pytorch that referenced this pull request Nov 7, 2023
…h#112541)

Adds E2E tests for saving/loading distributed checkpoints. Supported so far are:

- FSDP
- HSDP
- FSDP + TP

Each method is also tested using `torch.compile`

To run all tests:
`python test/distributed/checkpoint/test/distributed/checkpoint/e2e/test_e2e_save_and_load.py`

Pull Request resolved: pytorch#112541
Approved by: https://github.com/fegin, https://github.com/wz337
Skylion007 pushed a commit to Skylion007/pytorch that referenced this pull request Nov 14, 2023
…h#112541)

Adds E2E tests for saving/loading distributed checkpoints. Supported so far are:

- FSDP
- HSDP
- FSDP + TP

Each method is also tested using `torch.compile`

To run all tests:
`python test/distributed/checkpoint/test/distributed/checkpoint/e2e/test_e2e_save_and_load.py`

Pull Request resolved: pytorch#112541
Approved by: https://github.com/fegin, https://github.com/wz337
pytorchmergebot pushed a commit that referenced this pull request Nov 14, 2023
Addresses the following comment - #112541 (comment)

Changes the comparison of models in the checkpointing E2E test to compare a non-parallelized model against distribued model after training, saving, & loading.

Pull Request resolved: #113181
Approved by: https://github.com/fegin
pytorchmergebot pushed a commit that referenced this pull request Nov 15, 2023
Addresses the following comment - #112541 (comment)

Changes the comparison of models in the checkpointing E2E test to compare a non-parallelized model against distribued model after training, saving, & loading.

Pull Request resolved: #113181
Approved by: https://github.com/fegin, https://github.com/huydhn, https://github.com/wz337
@github-actions github-actions bot deleted the distributed_checkpointing_e2e_tests branch May 12, 2025 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/periodic Trigger jobs ran periodically on master (periodic.yml) on the PR ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: distributed (checkpoint) topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants