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

Update unwrap from accelerate #29933

Merged
merged 13 commits into from
Apr 19, 2024
Merged

Update unwrap from accelerate #29933

merged 13 commits into from
Apr 19, 2024

Conversation

SunMarc
Copy link
Member

@SunMarc SunMarc commented Mar 28, 2024

What does this PR do ?

This PR update the unwrap function to use the one in accelerate instead.
Fixes issue from @abhishekkrthakur

@@ -2306,7 +2306,7 @@ def save_pretrained(
files_timestamps = self._get_files_timestamps(save_directory)

# Only save the model itself if we are using distributed training
model_to_save = unwrap_model(self)
model_to_save = unwrap_model(self) if is_accelerate_available() else Accelerator().unwrap_model(self)
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure what is the best case here. I don't think we want to force users to install accelerate to save a model. If they are saving after training through trainer or accelerate, they will have accelerate installed.

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

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

My main concern is #29780, which looks to expand it.

Now that being said, everything here revolves around torch, and transformers with PyTorch requires Accelerate, so I think it's a fair assumption we can assume accelerate must be available in the env. @ArthurZucker let me know if you disagree with this.

If we agree, then I propose fully removing the current implementation, and solely relying on the one in Accelerate. What's in the mentioned PR can then also be offloaded to there, as the behaviors between the two differ, and with that PR they will differ even more.

The other alternative is in test_trainer we include a test that verifies similar behavior between Accelerate's and transformers model unwraps, so we can flag when they are not up to date

@@ -105,7 +105,7 @@
XLA_DOWNCAST_BF16 = os.environ.get("XLA_DOWNCAST_BF16", "0").upper()

if is_accelerate_available():
from accelerate import dispatch_model, infer_auto_device_map, init_empty_weights
from accelerate import Accelerator, dispatch_model, infer_auto_device_map, init_empty_weights
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just use extract_model_from_parallel instead of going through the Accelerator, since that's all it's calling

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I thought about that too. Maybe, we can put that in the unwrap_model so that it is easier to understand. However, we need to add a test to make sure that we have the same behavior.

@muellerzr
Copy link
Contributor

Got word from tf-boi that generally modeling_utils.py is PyTorch only, so any code that calls it requires pytorch, and in-vein with how Accelerate is with transformers requirement wise, accelerate. So I propose that we do move forward with fully ripping it out and relying on accelerate, modifying it's implementation.

@amyeroberts @ArthurZucker what do you say? :)

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Current state looks good. But yes torch requires accelerate, but let's keep unwrap anyway, sounds simpler

@zorrofox
Copy link

Hi @muellerzr @SunMarc , does this PR will merged? We need this.

@SunMarc
Copy link
Member Author

SunMarc commented Apr 18, 2024

I will finish this PR asap @zorrofox ! @ArthurZucker , do you want to switch back to unwrap_model in the trainer files also ? It makes sense to use self.accelerator.unwrap_model(model) since we initialized Accelerator there. I've updated the unwrap_model function, LMK what you think.

src/transformers/modeling_utils.py Outdated Show resolved Hide resolved
src/transformers/modeling_utils.py Outdated Show resolved Hide resolved
Co-authored-by: Zach Mueller <muellerzr@gmail.com>
Copy link
Contributor

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

This looks great to me! (after quality 😉 )

Copy link
Collaborator

@amyeroberts amyeroberts left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

src/transformers/modeling_utils.py Show resolved Hide resolved
@SunMarc SunMarc merged commit b4fd49b into huggingface:main Apr 19, 2024
21 checks passed
ArthurZucker pushed a commit that referenced this pull request Apr 22, 2024
* Use unwrap with the one in accelerate

* oups

* update unwrap

* fix

* wording

* raise error instead

* comment

* doc

* Update src/transformers/modeling_utils.py

Co-authored-by: Zach Mueller <muellerzr@gmail.com>

* style

* put else

---------

Co-authored-by: Zach Mueller <muellerzr@gmail.com>
ydshieh pushed a commit that referenced this pull request Apr 23, 2024
* Use unwrap with the one in accelerate

* oups

* update unwrap

* fix

* wording

* raise error instead

* comment

* doc

* Update src/transformers/modeling_utils.py

Co-authored-by: Zach Mueller <muellerzr@gmail.com>

* style

* put else

---------

Co-authored-by: Zach Mueller <muellerzr@gmail.com>
itazap pushed a commit that referenced this pull request May 14, 2024
* Use unwrap with the one in accelerate

* oups

* update unwrap

* fix

* wording

* raise error instead

* comment

* doc

* Update src/transformers/modeling_utils.py

Co-authored-by: Zach Mueller <muellerzr@gmail.com>

* style

* put else

---------

Co-authored-by: Zach Mueller <muellerzr@gmail.com>
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.

6 participants