Skip to content
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

Introduce shard-merging util for FSDP #2772

Merged
merged 15 commits into from
May 16, 2024
Merged

Introduce shard-merging util for FSDP #2772

merged 15 commits into from
May 16, 2024

Conversation

muellerzr
Copy link
Collaborator

@muellerzr muellerzr commented May 14, 2024

What does this PR do?

This PR brings a shard-merging util from PyTorch into accelerate. The reason for doing so is we can specifically save these weights as safetensors rather than .bin.

Applicable to users who use SHARDED_STATE_DICT during FSDP

Note that this is a cpu-bound process. So you just need enough RAM to load the model into memory you want to merge.

New API

Command Line

accelerate merge-weights {checkpoint_dir} {output_dir}
  • You can specify if you want to remove the checkpoint_dir via --remove_checkpoint_dir
  • By default we save these as safetensors. If you want to disable it, pass in --use_pytorch to save as .bin

Python API

from accelerate.utils import merge_fsdp_weights
output_dir = merge_fsdp_weights(
    checkpoint_dir="my_fsdp_checkpoints",
    output_path="somepath",
    use_safetensors=True,
    remove_checkpoint_dir=True
)

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@pacman100 @SunMarc

TODO:

  • Finish saving logic
  • Write tests
  • Write documentation

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@muellerzr muellerzr changed the title Introduce shard-merging util Introduce shard-merging util for FSDP May 14, 2024
Copy link
Contributor

@pacman100 pacman100 left a comment

Choose a reason for hiding this comment

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

Thank you, @muellerzr, for this nice utility function to merge the sharded FSDP state dicts! It would be great to have an accompanying test for this.

@muellerzr muellerzr requested a review from pacman100 May 15, 2024 15:53
@muellerzr
Copy link
Collaborator Author

@pacman100 hope the tests are to your liking :)

Copy link
Contributor

@pacman100 pacman100 left a comment

Choose a reason for hiding this comment

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

Thank you @muellerzr for iterating! Very useful feature.

Copy link
Member

@SunMarc SunMarc 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 adding this and creating a great test for that ! Left a few minor comments

docs/source/usage_guides/fsdp.md Outdated Show resolved Hide resolved
src/accelerate/commands/merge.py Outdated Show resolved Hide resolved
src/accelerate/commands/merge.py Outdated Show resolved Hide resolved
src/accelerate/utils/fsdp_utils.py Outdated Show resolved Hide resolved
src/accelerate/utils/fsdp_utils.py Outdated Show resolved Hide resolved
src/accelerate/utils/fsdp_utils.py Show resolved Hide resolved
Comment on lines 88 to 96
def test_merge_weights_safetensors(model, path):
# Should now be saved at `path/merged.safetensors`
merge_fsdp_weights(path / "pytorch_model_fsdp_0", path, use_safetensors=True)

safe_state_dict = load_file(path / "merged.safetensors")
safe_loaded_model = TinyModel()
check_weights("diff", model.state_dict(), safe_loaded_model.state_dict())
safe_loaded_model.load_state_dict(safe_state_dict)
check_weights("same", model.state_dict(), safe_loaded_model.state_dict())
Copy link
Member

Choose a reason for hiding this comment

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

nice

@muellerzr muellerzr merged commit 4ba436e into main May 16, 2024
25 checks passed
@muellerzr muellerzr deleted the fsdp-util branch May 16, 2024 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants