-
Notifications
You must be signed in to change notification settings - Fork 27.6k
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
Support for SDPA for SAM models #34110
Conversation
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! 🤗
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding, I think this should be automatically tests, but make sure to run the slow tests
|
Doing run slow I also get the following failed tests:
I haven't found the root cause of I'll work on a fix for those I think I'll be able to solve. |
15192f3
to
5a16359
Compare
- Updated tests to skip cases flash and dynamic compile. - Minor adjustment to ensure correct loading of model with sdpa for dispatch test.
8200114
to
7ee64dc
Compare
Apologies for the commit history mess. I had to rebase to clean things up, so the branch should now be up to date with the main branch and tests pass. |
Hi @MagnusS0, thanks for the update, can you please push an empty commit with the message |
Sorry, it should be the last commit, in that case I can approve the run 🙂 |
Ahh no problem, will commit again. Just fyi there are 4 slow tests I expect will fail, as they also do that on main. |
It looks like everything is fine in CI 🤗 Let's wait for a comment from @zucchini-nlp regarding setting attention implementation in config and I suppose it's ready to be merged |
Awesome 🤗 Probably just some small differences in my local setup causing the test to fail then. |
…SAM model tests - Added attributes for sub_configs as per PR huggingface#34410. - Enabled tests for configs, ensuring the composite model (SAM) has several sub-configs in the main config. - Added class attribute _is_composite=True to the tester class - test_sdpa_can_dispatch_composite_models added
@ArthurZucker Could you please review this PR when you have a moment? It should be ready to merge 🤗 Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the addition, sorry for the delay.
We are going to refactor a bit afterwards in #35235, but let's merge for now! 🤗
What does this PR do?
This PR adds support for SDPA to the SAM (Segment Anything Model), following the implementation done by the PyTorch team in segment-anything-fast.
I add two new classes:
SamSdpaAttention(SamAttention)
: For the decoder.SamSdpaVisionAttention(SamVisionAttention)
: For the encoder.Performance Improvements (Updated 03.11.2024)
Time Reduction between Eager and SDPA modes for SAM-B and SlimSAM averaged across 10 runs in ms:
Peak Memory Usage During Forward Pass:
On the TrashNet 1.0 validation set I got a reduction around 30-35%, with no decrease in accuracy.
I also ran training on TrashNet 1.0 with no issues and about 1.7x speedup per epoch with SlimSAM. Also allowed gradient check-pointing that seemed to be missing from the model deceleration, worked without any changes.
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
Who can review?
@amyeroberts
@qubvel
I'll also tag @ArthurZucker since we just worked on this model last weekend.
Some TODO
Should probably add some documentation for this, but let me know where is best place to add this.