Commit 91c0d0c
Fix xnnpack quantization discrepancy for non-fp32 (#8488)
Summary:
Perform quantization on the weights expressed in their original dtype (from the checkpoint) by performing source transformations before dtype cast. Previously the model was being converted to the `dtype_override` arg's dtype and then quantized. This eliminates supposedly eliminates quantization noise.
Note - no need to worry about https://github.com/pytorch/ao/blob/main/torchao/quantization/GPTQ.py#L1168, precision is passed in with the checkpoint dtype
### Comparison of arbitrary q_proj tensor from sample Llama checkpoint:
Before:
```
Mismatched elements: 3260378 / 4194304 (77.7%)
Greatest absolute difference: 0.08802086114883423 at index (1129, 604) (up to 1e-05 allowed)
Greatest relative difference: 1.0 at index (0, 1350) (up to 1.3e-06 allowed)
Signal-to-noise: 32.8974 dB
```
After: no difference
Test Plan:
### Manual testing
```
python -m examples.models.llama.export_llama \
-v -c xl_consolidated/consolidated_renamed.pth \
-p xl_consolidated/et_params.json -kv -d fp32 \
-qmode 8da4w --group_size 32 -X \
--use_sdpa_with_kv_cache \
--output_name quantized_baseline.pte \
--max_context_length 4096 -E 4,32
```
With the following inserted after the quantization:
```
edge_manager.model(
torch.tensor([[2, 3, 4]], dtype=torch.long),
{"input_pos": torch.tensor([0], dtype=torch.long)},
)
```
And the following modifications to GPTQ.py in torchao: pytorch/ao#1756 for testing.
### Automated testing
+ existing CI tests
### Regression testing
TBD
Differential Revision: D70184325
Pulled By: jackzhxng1 parent dedfdaf commit 91c0d0c
File tree
6 files changed
+147
-84
lines changed- examples/models
- llama
- source_transformation
- exir/tests
- extension/llm/export
6 files changed
+147
-84
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
55 | | - | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
| 59 | + | |
58 | 60 | | |
59 | 61 | | |
60 | 62 | | |
| |||
563 | 565 | | |
564 | 566 | | |
565 | 567 | | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
570 | | - | |
571 | | - | |
572 | | - | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
573 | 603 | | |
574 | | - | |
575 | | - | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
576 | 609 | | |
577 | | - | |
578 | | - | |
579 | | - | |
580 | | - | |
581 | | - | |
582 | | - | |
583 | | - | |
584 | | - | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | | - | |
589 | | - | |
590 | | - | |
591 | | - | |
592 | | - | |
593 | | - | |
594 | | - | |
595 | | - | |
596 | | - | |
597 | | - | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
598 | 613 | | |
599 | | - | |
600 | | - | |
601 | 614 | | |
602 | 615 | | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
603 | 625 | | |
604 | 626 | | |
605 | 627 | | |
| |||
783 | 805 | | |
784 | 806 | | |
785 | 807 | | |
786 | | - | |
787 | | - | |
788 | 808 | | |
789 | 809 | | |
790 | 810 | | |
| |||
1004 | 1024 | | |
1005 | 1025 | | |
1006 | 1026 | | |
| 1027 | + | |
| 1028 | + | |
1007 | 1029 | | |
1008 | 1030 | | |
1009 | 1031 | | |
| |||
1020 | 1042 | | |
1021 | 1043 | | |
1022 | 1044 | | |
| 1045 | + | |
1023 | 1046 | | |
1024 | 1047 | | |
1025 | 1048 | | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
1031 | | - | |
1032 | | - | |
1033 | | - | |
1034 | | - | |
1035 | | - | |
1036 | | - | |
1037 | | - | |
1038 | | - | |
1039 | | - | |
1040 | | - | |
1041 | | - | |
1042 | | - | |
1043 | | - | |
1044 | | - | |
1045 | | - | |
1046 | | - | |
1047 | | - | |
1048 | | - | |
1049 | | - | |
1050 | | - | |
1051 | | - | |
1052 | 1049 | | |
1053 | 1050 | | |
1054 | 1051 | | |
1055 | 1052 | | |
1056 | 1053 | | |
1057 | | - | |
| 1054 | + | |
1058 | 1055 | | |
1059 | 1056 | | |
1060 | 1057 | | |
| |||
1091 | 1088 | | |
1092 | 1089 | | |
1093 | 1090 | | |
1094 | | - | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
1095 | 1095 | | |
1096 | 1096 | | |
1097 | 1097 | | |
| |||
1125 | 1125 | | |
1126 | 1126 | | |
1127 | 1127 | | |
1128 | | - | |
| 1128 | + | |
1129 | 1129 | | |
1130 | 1130 | | |
1131 | 1131 | | |
| |||
1139 | 1139 | | |
1140 | 1140 | | |
1141 | 1141 | | |
1142 | | - | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
1143 | 1150 | | |
1144 | 1151 | | |
1145 | 1152 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
122 | 122 | | |
123 | 123 | | |
124 | 124 | | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | 125 | | |
129 | 126 | | |
130 | 127 | | |
| |||
171 | 168 | | |
172 | 169 | | |
173 | 170 | | |
| 171 | + | |
174 | 172 | | |
| 173 | + | |
175 | 174 | | |
176 | 175 | | |
177 | 176 | | |
| |||
241 | 240 | | |
242 | 241 | | |
243 | 242 | | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
244 | 247 | | |
245 | 248 | | |
246 | 249 | | |
| |||
277 | 280 | | |
278 | 281 | | |
279 | 282 | | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
| 283 | + | |
288 | 284 | | |
289 | 285 | | |
290 | 286 | | |
| |||
0 commit comments