Commit a776b1f
authored
Relax int4wo device mismatch error (pytorch#2254)
**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:
pytorch#1117.
**Test Plan:**
python test/quantization/test_quant_api.py -k test_int4wo_cuda_serialization1 parent 446f07d commit a776b1f
File tree
2 files changed
+21
-1
lines changed- test/quantization
- torchao/dtypes/uintx
2 files changed
+21
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1017 | 1017 | | |
1018 | 1018 | | |
1019 | 1019 | | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
| 1038 | + | |
1020 | 1039 | | |
1021 | 1040 | | |
1022 | 1041 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
318 | 319 | | |
319 | 320 | | |
320 | 321 | | |
321 | | - | |
| 322 | + | |
322 | 323 | | |
323 | 324 | | |
324 | 325 | | |
| |||
0 commit comments