Relax int4wo device mismatch error#2254
Merged
Merged
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2254
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit c31eb1d with merge base 4c6188f ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
**Summary:** We have an guard preventing users from using a cuda quantized on cpu and vice versa. However, this also prevents users who load their checkpoints on cpu first and then move them to cuda later, which is what torchtune does: ``` quantize_(model.cuda(), Int4WeightOnlyConfig()) # save checkpoint in cuda torch.save(model.state_dict(), "my_checkpoint.pt") # load checkpoint on cpu # This is what torchtune does: https://github.com/pytorch/torchtune/blob/v0.6.1/torchtune/training/checkpointing/_utils.py#L253 sd = torch.load("my_checkpoint.pt", weights_only=False, map_location="cpu") # move checkpoint to cuda for k, v in sd.items(): sd[k] = v.to("cuda") # load state_dict in cuda model.load_state_dict(sd, assign=True) ``` This use case is safe in that the model was quantized in cuda and ultimately used on cuda. This commit relaxes the error to allow the above use case. More details here: #1117. **Test Plan:** python test/quantization/test_quant_api.py -k test_int4wo_cuda_serialization
99df5c1 to
c31eb1d
Compare
jerryzh168
approved these changes
May 23, 2025
jerryzh168
left a comment
Contributor
There was a problem hiding this comment.
Thanks!
I think we also want to support quantizing in CPU directly, but this is a good first step
jerryzh168
reviewed
May 27, 2025
| # cpu and cuda device: https://github.com/pytorch/ao/issues/1117 | ||
| if not is_device(torch.device(self.device).type, device): | ||
| raise ValueError( | ||
| logging.warning( |
Contributor
There was a problem hiding this comment.
@andrewor14 currently it will print a lot of warning
maybe we can just remove this? since it's supported now.
or we can change it to something like warn_once
liangel-02
pushed a commit
that referenced
this pull request
Aug 25, 2025
**Summary:** We have an guard preventing users from using a cuda quantized on cpu and vice versa. However, this also prevents users who load their checkpoints on cpu first and then move them to cuda later, which is what torchtune does: ``` quantize_(model.cuda(), Int4WeightOnlyConfig()) # save checkpoint in cuda torch.save(model.state_dict(), "my_checkpoint.pt") # load checkpoint on cpu # This is what torchtune does: https://github.com/pytorch/torchtune/blob/v0.6.1/torchtune/training/checkpointing/_utils.py#L253 sd = torch.load("my_checkpoint.pt", weights_only=False, map_location="cpu") # move checkpoint to cuda for k, v in sd.items(): sd[k] = v.to("cuda") # load state_dict in cuda model.load_state_dict(sd, assign=True) ``` This use case is safe in that the model was quantized in cuda and ultimately used on cuda. This commit relaxes the error to allow the above use case. More details here: #1117. **Test Plan:** python test/quantization/test_quant_api.py -k test_int4wo_cuda_serialization
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.
Summary: We have an guard preventing users from using a cuda quantized on cpu and vice versa. However, this also prevents users who load their checkpoints on cpu first and then move them to cuda later, which is what torchtune does:
This use case is safe in that the model was quantized in cuda and ultimately used on cuda. This commit relaxes the error to allow the above use case. More details here: #1117.
Test Plan:
python test/quantization/test_quant_api.py -k test_int4wo_cuda_serialization