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

BertForSequenceClassification does not support 'device_map':"auto" yet #25296

Open
2 of 4 tasks
goodaytar opened this issue Aug 3, 2023 · 18 comments
Open
2 of 4 tasks
Labels
Good Second Issue Issues that are more difficult to do than "Good First" issues - give it a try if you want!

Comments

@goodaytar
Copy link

System Info

I have trained a model and am now trying to load and quantise it but getting the error:

BertForSequenceClassification does not support 'device_map':"auto" yet

Code for loading is simply:
model = AutoModelForSequenceClassification.from_pretrained(model_dir, device_map='auto', load_in_8bit=True)

Help would be greatly appreciated!

Thanks,

Lee

Who can help?

No response

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

model = AutoModelForSequenceClassification.from_pretrained(model_dir, device_map='auto', load_in_8bit=True)

Expected behavior

The model would load and be usable.

@amyeroberts
Copy link
Collaborator

Hi @goodaytar, thanks for raising this issue!

Yes, the BERT models don't support the use of device_map=xxx yet. In the full error message, you should have seen:

BertForSequenceClassification not support `device_map="auto"`. To implement support, the model class needs to implement the `_no_split_modules` attribute.

In order to enable this the _no_split_modules attribute needs to be implemented for the model. If you or anyone else in the community would like to open a PR to add this, we'd be very happy to review!

@goodaytar
Copy link
Author

goodaytar commented Aug 4, 2023 via email

@amyeroberts
Copy link
Collaborator

In order to know how to properly place the model onto difference devices, the models need to have _no_split_modules implemented in their PreTrainedModel class e.g. like here for Roberta.

For some modules, it's necessary to place all of the weights on the same device e.g. like Pix2StructVisionLayer for Pix2Struct.

In order to add, it'll be a case of iterating to find the modules that should be split or not. Once implemented, the accelerate tests should be run and pass. This should be tested with 1 and 2 GPUs.

@goodaytar
Copy link
Author

And how do I find the modules that should be split or not?

@amyeroberts
Copy link
Collaborator

@goodaytar You'll need to experiment with the model to find out which modules should be split. I suggest starting with an empty list and looking at similar models to see how they set _no_split_modules.

You can inspect where the layers are allocated by using infer_auto_device_map:

device_map = infer_auto_device_map(model, no_split_module_classes=[])

The modules that can be added will be the layers defined in the modeling file e.g. "BertEmbeddings"

Once set, you can try running the accelerate tests (with GPUs!) to confirm the mapping works. If not, then inspect the device map.

@github-actions
Copy link

github-actions bot commented Sep 3, 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.

@amyeroberts amyeroberts reopened this Sep 12, 2023
@amyeroberts amyeroberts added the Good Second Issue Issues that are more difficult to do than "Good First" issues - give it a try if you want! label Sep 12, 2023
@tanaymeh
Copy link
Contributor

Hi @amyeroberts, I would like to add the 'device_map': "auto" functionality to BERT Models!

@amyeroberts
Copy link
Collaborator

@tanaymeh Great! From next week, I'll be off for a few weeks. Please ping @younesbelkada for review in that time.

@younesbelkada
Copy link
Contributor

@tanaymeh that would be really great, in few words, you just need to make sure to add the module names that contain any skip connection to avoid potential device mismatch issues
Check for instance what has been done for RoBERTa here: https://github.com/huggingface/transformers/blob/main/src/transformers/models/roberta/modeling_roberta.py#L596

@tanaymeh
Copy link
Contributor

@tanaymeh that would be really great, in a few words, you just need to make sure to add the module names that contain any skip connection to avoid potential device mismatch issues Check for instance what has been done for RoBERTa here: main/src/transformers/models/roberta/modeling_roberta.py#L596

That makes sense @younesbelkada! Will create a PR for this.
One question: Will the CI tests on Github also test my implementation of device_map (with 1 and 2 GPUs) every time I push a commit?

@younesbelkada
Copy link
Contributor

Hi @tanaymeh ,
Thanks, will look into it!
The CI will not directly test it, we run "slow" tests every 24h on GPUs that will run those tests

@dragstoll
Copy link

@younesbelkada
Hi Younes
Could you make it work for xlm_roberta_xl too?
Thanks
Regards
Dragan

@Hambaobao
Copy link

@younesbelkada Any updates? We can't wait to use this great feature.

@tanaymeh
Copy link
Contributor

tanaymeh commented Nov 1, 2023

@Hambaobao I am working on the PR for this feature but waiting for a revert from @younesbelkada!

@bp020108
Copy link

any update on this issue? or anyone fixed it?

@Vsareen0
Copy link

Vsareen0 commented Mar 5, 2024

Any update on this issue ?

@lucasjinreal
Copy link

ValueError: SiglipVisionModel does not support device_map='auto'

Same for Siglip?

@amyeroberts
Copy link
Collaborator

@lucasjinreal There are many models which don't yet have this enabled. I've opened a feature request to add this for vision and multimodal models which could have this added: #29786

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good Second Issue Issues that are more difficult to do than "Good First" issues - give it a try if you want!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants