Fix hf_device_map device index comparison in prepare_model#3895
Merged
SunMarc merged 1 commit intohuggingface:mainfrom Jan 6, 2026
Merged
Conversation
e4bfb37 to
10195a0
Compare
Contributor
|
LGTM. |
bd6ab6c to
1ae6bc7
Compare
|
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. |
1ae6bc7 to
4111a96
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR fixes an issue where a
TypeErroris raised if the base model is loaded without specifying device index (e.g.device_map="auto"ordevice_map="cuda"instead ofdevice_map="cuda:0") for models loaded in 8-bit/4-bit precision with anhf_device_map. The exception is raised inprepare_model()becausetorch.device(device_index)is called withdevice_index=None.The fix constructs the device using
torch.device(device_type, device_index)instead, which works even whendevice_index=None.Added a test to cover the 8-bit/4-bit +
hf_device_mappath when the model is loaded on CPU (hf_device_map={"": "cpu"}), but the test should also work for other device types as well.Fixes #3758 and #2357
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
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 @jiqing-feng