Repository navigation
Commit 6d3c1c7
authored
Preserve Python scalar semantics in Vulkan and XNNPACK quantizers (#22804)
### Summary
Preparing a model with the Vulkan or XNNPACK quantizer can change its
results when a Python scalar is materialized using the operation's
output dtype. For example, `torch.tensor([1e-4], dtype=torch.float16) *
100000.0` produces approximately 10 in PyTorch, but casting the scalar
to FP16 first makes the result infinite. Smaller scalars can underflow,
and other dtypes have their own promotion and rounding rules.
Keep the original Python scalar operands for add and multiply operations
whose output is not FP32. FP32 operations retain scalar lifting for
quantization. Integer index arithmetic covered by #22372 and #22065
continues to work.
### Test plan
All 10 focused tests and 208 scalar-promotion subtests pass across
dtypes, add/multiply, scalar/vector inputs, and configured/unconfigured
quantizers:
```bash
OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 PYTHONPATH=src python -m pytest -o addopts= -q \
backends/vulkan/test/quantizer/test_vulkan_quantizer.py \
backends/xnnpack/test/quantizer/test_xnnpack_quantizer.py \
-k 'scalar or int64 or add_mul_long or mul_float32_max or add_and_inplace_add or mul_and_inplace_mul'
```
Existing FP32 scalar quantization, tensor add/multiply, large-scalar
handling, and integer-index regressions are included. `lintrunner
--revision main` and `git diff --check` pass.
Authored with OpenAI Codex.
cc @SS-JIA @manuelcandales @digantdesai @cbilgin1 parent 83d1c53 commit 6d3c1c7
4 files changed
Lines changed: 121 additions & 35 deletions
File tree
- backends
- vulkan
- quantizer
- test/quantizer
- xnnpack
- quantizer
- test/quantizer
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
201 | 201 | | |
202 | 202 | | |
203 | 203 | | |
204 | | - | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
205 | 210 | | |
206 | 211 | | |
207 | 212 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | 21 | | |
24 | 22 | | |
25 | 23 | | |
| |||
30 | 28 | | |
31 | 29 | | |
32 | 30 | | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 31 | | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
| 32 | + | |
46 | 33 | | |
| 34 | + | |
47 | 35 | | |
48 | 36 | | |
49 | 37 | | |
50 | 38 | | |
51 | 39 | | |
52 | 40 | | |
53 | 41 | | |
54 | | - | |
55 | | - | |
56 | | - | |
57 | | - | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
58 | 68 | | |
59 | | - | |
60 | | - | |
61 | | - | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
789 | 789 | | |
790 | 790 | | |
791 | 791 | | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
792 | 795 | | |
793 | 796 | | |
794 | 797 | | |
| |||
845 | 848 | | |
846 | 849 | | |
847 | 850 | | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
848 | 854 | | |
849 | 855 | | |
850 | 856 | | |
| |||
913 | 919 | | |
914 | 920 | | |
915 | 921 | | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
916 | 925 | | |
917 | 926 | | |
918 | 927 | | |
| |||
969 | 978 | | |
970 | 979 | | |
971 | 980 | | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
972 | 984 | | |
973 | 985 | | |
974 | 986 | | |
| |||
1154 | 1166 | | |
1155 | 1167 | | |
1156 | 1168 | | |
1157 | | - | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
1158 | 1175 | | |
1159 | 1176 | | |
1160 | 1177 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
1123 | 1124 | | |
1124 | 1125 | | |
1125 | 1126 | | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | 1127 | | |
1130 | 1128 | | |
1131 | 1129 | | |
| |||
1136 | 1134 | | |
1137 | 1135 | | |
1138 | 1136 | | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | 1137 | | |
1147 | | - | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
| 1154 | + | |
| 1155 | + | |
| 1156 | + | |
| 1157 | + | |
| 1158 | + | |
| 1159 | + | |
| 1160 | + | |
| 1161 | + | |
| 1162 | + | |
| 1163 | + | |
| 1164 | + | |
| 1165 | + | |
| 1166 | + | |
| 1167 | + | |
| 1168 | + | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
| 1186 | + | |
| 1187 | + | |
| 1188 | + | |
| 1189 | + | |
1148 | 1190 | | |
1149 | 1191 | | |
1150 | 1192 | | |
| |||
0 commit comments