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

Add methods to PreTrainedModel to use PyTorch's BetterTransformer #21259

Merged

Conversation

fxmarty
Copy link
Contributor

@fxmarty fxmarty commented Jan 23, 2023

As per title.

Should be merged only on the next Optimum release that will include huggingface/optimum#676

Before submitting

Tests are still to be done.

Who can review?

@younesbelkada @sgugger

@fxmarty fxmarty changed the title Add method to PreTrainedModel to use PyTorch's BetterTransformer Add methods to PreTrainedModel to use PyTorch's BetterTransformer Jan 23, 2023
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jan 23, 2023

The documentation is not available anymore as the PR was closed or merged.

@younesbelkada
Copy link
Contributor

as a side note, since in the previous optimum versions the save_pretrained and push_to_hub methods are not blocked, I propose to explicitly block them for transformed models in this PR and/or force users to use a certain version of optimum.

@fxmarty
Copy link
Contributor Author

fxmarty commented Jan 23, 2023

Yes we should probably force the next optimum version.

Copy link
Collaborator

@sgugger sgugger 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 those! I'd also make sure to document the methods and add something in the optimization guides we have :-)

src/transformers/modeling_utils.py Outdated Show resolved Hide resolved
Comment on lines 2967 to 3042
if not is_optimum_available():
raise ImportError("The package `optimum` is required to use BetterTransformer.")

from optimum.bettertransformer import BetterTransformer
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think a version check on optimum with a clear error message would be good? Also can the transform be applied twice? If not there should be a check and a clear error message as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also can the transform be applied twice? If not there should be a check and a clear error message as well.

I think there's currently no check for this @younesbelkada . I will add it on the Optimum side. So as to keep the transformers side as lightweight as possible.

src/transformers/modeling_utils.py Outdated Show resolved Hide resolved
src/transformers/modeling_utils.py Show resolved Hide resolved
@fxmarty fxmarty force-pushed the add-use-better-transformer-option branch from 66366ea to c6dd4b9 Compare February 6, 2023 09:44
@fxmarty fxmarty marked this pull request as ready for review February 6, 2023 10:45
@fxmarty
Copy link
Contributor Author

fxmarty commented Feb 6, 2023

Should be ready @sgugger , the documentation has been extended in https://moon-ci-docs.huggingface.co/docs/transformers/pr_21259/en/perf_infer_gpu_one .

Let me know if I should add a test - in which case optimum should be added in the setup.py, I guess.

@fxmarty fxmarty requested a review from sgugger February 6, 2023 10:47
@younesbelkada
Copy link
Contributor

@fxmarty there should be no need to add optimum in setup.py, we can do something similar than bitsandbytes and add optimum in the Dockerfile of the Docker image that will run the slow tests:

RUN python3 -m pip install --no-cache-dir bitsandbytes

I very much agree that we should add tests, especially to test accelerate compatibility, happy to help you on this, let me know if you need help

@fxmarty
Copy link
Contributor Author

fxmarty commented Feb 6, 2023

Thanks, will do!

especially to test accelerate compatibility

Isn't this already tested on Optimum side?

@younesbelkada
Copy link
Contributor

younesbelkada commented Feb 6, 2023

Isn't this already tested on Optimum side?

Yes but the tests are run on GPU: therefore not run on any of the runners on optimum on a daily basis (but not sue if there are tested somewhere else) - I just asked individually to each contributor to run the accelerate test locally on their GPU before merging (only in case I have serious doubts that the PR breaks anything related to accelerate).
Since in transformers tests are run on GPU on daily basis, we can leverage that and setup a small BetterTransformer testing suite that tests all the tests + accelerate compatibility. Also this enables us to flag anything we need to upstream to accelerate if something breaks BT integration with accelerate

@fxmarty
Copy link
Contributor Author

fxmarty commented Feb 6, 2023

There are tests on the daily basis on GPU in Optimum, for example https://github.com/huggingface/optimum/blob/main/.github/workflows/test_onnxruntime_train.yml and https://github.com/huggingface/optimum/blob/main/.github/workflows/test_onnxruntime_gpu.yml

In my opinion, thorough tests should be added in Optimum, not Transformers. The test I was thinking of in Transformers was only an integration one to check that there's no error.

@younesbelkada
Copy link
Contributor

There is an issue with accelerate loaded models and transform from BT, let's wait until this gets fixed before merging this PR

@github-actions
Copy link

github-actions bot commented Mar 4, 2023

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@fxmarty
Copy link
Contributor Author

fxmarty commented Mar 4, 2023

not stale

@sgugger
Copy link
Collaborator

sgugger commented Mar 6, 2023

If you want this PR included in the next release, you should finish the work and have it merged sooner rather than later :-)
The last I saw was Younes telling we should wait for a fix, was that fix added? Then this needs a rebase on main since it has been a while.

@younesbelkada
Copy link
Contributor

Thanks for the headsup!
Indeed we are working on fixing some bugs on optimum side that was introduced by one of my PRs (the revert-transform PR) before adding the invert_transform method
We can maybe merge this PR by keeping only transform method and blocking the save_pretrained & push_to_hub methods after transforming the model

@fxmarty
Copy link
Contributor Author

fxmarty commented Mar 6, 2023

you should finish the work and have it merged sooner rather than later :-)

There is substantial work left in Optimum before this should be merged. Marking as draft for now!

@fxmarty fxmarty marked this pull request as draft March 6, 2023 14:45
@sgugger
Copy link
Collaborator

sgugger commented Mar 6, 2023

OK, so this won't be in the next release of Transformers (probably this week in preparation for PyTorch 2.0).

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@github-actions github-actions bot closed this Apr 8, 2023
@LysandreJik
Copy link
Member

Hey @fxmarty and @younesbelkada, are there standing PRs in optimum that need to be merged for this to proceed/anything we can help with to have this move forward? Thanks :)

@younesbelkada
Copy link
Contributor

Hey @LysandreJik @sgugger
@fxmarty recently managed to fix all issues related to decoder-based models integration in optimum! I believe that this PR could be re-opened, in my understanding we just need to add few tests and we should be good to go

@younesbelkada younesbelkada reopened this Apr 10, 2023
@fxmarty fxmarty marked this pull request as ready for review April 11, 2023 11:42
@fxmarty fxmarty requested a review from LysandreJik April 12, 2023 10:46
@younesbelkada
Copy link
Contributor

@sgugger @LysandreJik this is now ready for review!

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Thanks! Just have one comment on the is_optimum_available function but the rest looks fine!

src/transformers/__init__.py Outdated Show resolved Hide resolved
src/transformers/integrations.py Outdated Show resolved Hide resolved
src/transformers/modeling_utils.py Outdated Show resolved Hide resolved
src/transformers/testing_utils.py Outdated Show resolved Hide resolved
younesbelkada and others added 2 commits April 25, 2023 15:43
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
@younesbelkada younesbelkada requested a review from sgugger April 25, 2023 13:48
Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

Left a few nits.
LGTM!

docs/source/en/perf_infer_gpu_one.mdx Outdated Show resolved Hide resolved
docs/source/en/perf_train_gpu_one.mdx Outdated Show resolved Hide resolved
Comment on lines +3328 to +3336
if not is_optimum_available():
raise ImportError("The package `optimum` is required to use Better Transformer.")

from optimum.version import __version__ as optimum_version

if version.parse(optimum_version) < version.parse("1.7.0"):
raise ImportError(
f"Please install optimum>=1.7.0 to use Better Transformer. The version {optimum_version} was found."
)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe factor all of this into a is_bettertransformer_available?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm I would say this is too specific, maybe let's keep it as it is

@@ -0,0 +1 @@

Copy link
Member

Choose a reason for hiding this comment

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

Is it wanted?

younesbelkada and others added 2 commits April 26, 2023 19:02
Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com>
@younesbelkada younesbelkada merged commit 3042c63 into huggingface:main Apr 27, 2023
gojiteji pushed a commit to gojiteji/transformers that referenced this pull request Jun 5, 2023
…ggingface#21259)

* fix mess

* better documentation

* typo

* fix doc

* update

* add test

* fix test

* more tests

* Update src/transformers/modeling_utils.py

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>

* move to utils

* Apply suggestions from code review

Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com>

* nit

---------

Co-authored-by: younesbelkada <younesbelkada@gmail.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com>
novice03 pushed a commit to novice03/transformers that referenced this pull request Jun 23, 2023
…ggingface#21259)

* fix mess

* better documentation

* typo

* fix doc

* update

* add test

* fix test

* more tests

* Update src/transformers/modeling_utils.py

Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>

* move to utils

* Apply suggestions from code review

Co-authored-by: Michael Benayoun <mickbenayoun@gmail.com>

* nit

---------

Co-authored-by: younesbelkada <younesbelkada@gmail.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
Co-authored-by: Michael Benayoun <mickbenayoun@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