Skip to content

Fix hf_device_map device index comparison in prepare_model#3895

Merged
SunMarc merged 1 commit intohuggingface:mainfrom
rezaqorbani:fix/prepare-model-hf-device-map-device-compare
Jan 6, 2026
Merged

Fix hf_device_map device index comparison in prepare_model#3895
SunMarc merged 1 commit intohuggingface:mainfrom
rezaqorbani:fix/prepare-model-hf-device-map-device-compare

Conversation

@rezaqorbani
Copy link
Contributor

What does this PR do?

This PR fixes an issue where a TypeError is raised if the base model is loaded without specifying device index (e.g. device_map="auto" or device_map="cuda" instead of device_map="cuda:0") for models loaded in 8-bit/4-bit precision with an hf_device_map. The exception is raised in prepare_model() becausetorch.device(device_index) is called with device_index=None.

The fix constructs the device using torch.device(device_type, device_index) instead, which works even when device_index=None.

Added a test to cover the 8-bit/4-bit + hf_device_map path 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

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

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

@rezaqorbani rezaqorbani force-pushed the fix/prepare-model-hf-device-map-device-compare branch 2 times, most recently from e4bfb37 to 10195a0 Compare December 28, 2025 10:17
@jiqing-feng
Copy link
Contributor

LGTM.

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks a lot, just a nit

@rezaqorbani rezaqorbani force-pushed the fix/prepare-model-hf-device-map-device-compare branch 2 times, most recently from bd6ab6c to 1ae6bc7 Compare December 29, 2025 17:20
Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks !

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

@rezaqorbani rezaqorbani force-pushed the fix/prepare-model-hf-device-map-device-compare branch from 1ae6bc7 to 4111a96 Compare December 30, 2025 19:04
@SunMarc SunMarc merged commit 27ed776 into huggingface:main Jan 6, 2026
22 of 25 checks passed
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.

TypeError: device() received an invalid combination of arguments

4 participants