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

Allow for force unwrapping #2595

Merged
merged 7 commits into from
Apr 2, 2024
Merged

Allow for force unwrapping #2595

merged 7 commits into from
Apr 2, 2024

Conversation

muellerzr
Copy link
Collaborator

What does this PR do?

There are cases when we need to unwrap far more than just what is done natively in Accelerate, such as when we have TPUs/SPMD.

This PR expands extract_model_from_parallel to support situations such as using XLA's FSDPv2.

Alternative to huggingface/transformers#29780, which we can upstream to transformers by just using recursive=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.

@SunMarc @shub-kris

We can't test it on TPUs since we don't have our runners, but @shub-kris tested this himself :)

@@ -193,6 +206,31 @@ def test_extract_model(self):

assert model == model_unwrapped

@require_tpu
@require_huggingface_suite
def test_extract_model_recursive_fsdpv2(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

While we don't have runners for this test, @shub-kris verified it works

One more thing that needs to be in the test which I missed is: One has to make sure that you set this two environment variables:
export PJRT_DEVICE=TPU XLA_USE_SPMD=1
I encountered the error when I didn't set XLA_USE_SPMD=1
 Please enable SPMD via `torch_xla.runtime.use_spmd()`

Copy link
Contributor

Choose a reason for hiding this comment

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

I will do a test-run on this huggingface/transformers#29659 to make sure training and loading of model works properly.

Copy link
Contributor

Choose a reason for hiding this comment

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

@muellerzr shouldn't we add that in the test function that it should be ran with

export PJRT_DEVICE=TPU XLA_USE_SPMD=1

Copy link
Contributor

Choose a reason for hiding this comment

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

I will do a test-run on this huggingface/transformers#29659 to make sure training and loading of model works properly.

Ahh I can't do as the changes aren't there yet in the transformers library.

@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.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

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

Overall LGTM to me thanks. I only think it could be a bit better commented for future clarity.

Regarding the testing, good to have confirmation that this fixes the issue. I guess we could come up with an artificial test case that has module.module and works without TPU. Not sure how useful that would be.

tests/test_utils.py Show resolved Hide resolved
src/accelerate/utils/other.py Show resolved Hide resolved
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.

LGTM !

Comment on lines +92 to +93
if recursive:
# This is needed in cases such as using FSDPv2 on XLA
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to detect that we are using fsdpv2 or xla , so that we don't have to set recursive. cc @shub-kris

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are, but accelerate doesn’t support it yet and it’s experimental. So for now a generic approach, as it’s a single location in the Trainer

Copy link
Contributor

@shub-kris shub-kris 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 the nice PR @muellerzr. Great work.
I will approve the PR once I do a small training to verify everything works end-to-end.

@muellerzr muellerzr merged commit bbecad4 into main Apr 2, 2024
25 checks passed
@muellerzr muellerzr deleted the expound-unwrap branch April 2, 2024 13:59
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.

5 participants