|
28 | 28 | QuantizationSpec, |
29 | 29 | ) |
30 | 30 |
|
| 31 | +DEFAULT_EPS_8BIT = 0.0001 / 255 |
| 32 | +DEFAULT_EPS_16BIT = 0.0001 / 65535 |
| 33 | + |
31 | 34 |
|
32 | 35 | @dataclass(eq=True) |
33 | 36 | class QuantizationConfig: |
@@ -104,10 +107,12 @@ def _derive_bias_qparams_fn( |
104 | 107 |
|
105 | 108 |
|
106 | 109 | def get_8a8w_qnn_ptq_config( |
107 | | - act_symmetric: bool = False, act_observer=MovingAverageMinMaxObserver |
| 110 | + act_symmetric: bool = False, |
| 111 | + act_observer=MovingAverageMinMaxObserver, |
| 112 | + eps: float = None, |
108 | 113 | ) -> QuantizationConfig: |
109 | | - # the smallest scale: 0.0001 / 255 |
110 | | - extra_args: Dict[str, Any] = {"eps": 2**-21} |
| 114 | + # the smallest scale defaults to DEFAULT_EPS_8BIT |
| 115 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_8BIT} |
111 | 116 |
|
112 | 117 | act_quantization_spec = QuantizationSpec( |
113 | 118 | dtype=torch.uint8, |
@@ -146,10 +151,12 @@ def get_8a8w_qnn_ptq_config( |
146 | 151 |
|
147 | 152 |
|
148 | 153 | def get_8a4w_qnn_ptq_config( |
149 | | - act_symmetric: bool = True, act_observer=MovingAverageMinMaxObserver |
| 154 | + act_symmetric: bool = True, |
| 155 | + act_observer=MovingAverageMinMaxObserver, |
| 156 | + eps: float = None, |
150 | 157 | ) -> QuantizationConfig: |
151 | | - # the smallest scale: 0.0001 / 255 |
152 | | - extra_args: Dict[str, Any] = {"eps": 2**-21} |
| 158 | + # the smallest defaults to DEFAULT_EPS_8BIT |
| 159 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_8BIT} |
153 | 160 |
|
154 | 161 | if act_symmetric: |
155 | 162 | # If zero_point is 128, htp can do optimizations. |
@@ -203,10 +210,10 @@ def get_8a4w_qnn_ptq_config( |
203 | 210 |
|
204 | 211 | # 4 bits quantization only supports specific ops. |
205 | 212 | def get_16a4w_qnn_ptq_config( |
206 | | - act_observer=MovingAverageMinMaxObserver, |
| 213 | + act_observer=MovingAverageMinMaxObserver, eps: float = None |
207 | 214 | ) -> QuantizationConfig: |
208 | | - # the smallest scale: 0.0001 / 65535 |
209 | | - extra_args: Dict[str, Any] = {"eps": 2**-29} |
| 215 | + # the smallest defaults to DEFAULT_EPS_16BIT |
| 216 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_16BIT} |
210 | 217 | act_quantization_spec = QuantizationSpec( |
211 | 218 | dtype=torch.int32, |
212 | 219 | quant_min=torch.iinfo(torch.uint16).min, |
@@ -243,10 +250,10 @@ def get_16a4w_qnn_ptq_config( |
243 | 250 |
|
244 | 251 |
|
245 | 252 | def get_16a8w_qnn_ptq_config( |
246 | | - act_observer=MovingAverageMinMaxObserver, |
| 253 | + act_observer=MovingAverageMinMaxObserver, eps: float = None |
247 | 254 | ) -> QuantizationConfig: |
248 | | - # the smallest scale: 0.0001 / 65535 |
249 | | - extra_args: Dict[str, Any] = {"eps": 2**-29} |
| 255 | + # the smallest defaults to DEFAULT_EPS_16BIT |
| 256 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_16BIT} |
250 | 257 | act_quantization_spec = QuantizationSpec( |
251 | 258 | dtype=torch.int32, |
252 | 259 | quant_min=torch.iinfo(torch.uint16).min, |
@@ -281,10 +288,10 @@ def get_16a8w_qnn_ptq_config( |
281 | 288 |
|
282 | 289 |
|
283 | 290 | def get_16a8w_qnn_qat_config( |
284 | | - act_observer=MovingAverageMinMaxObserver, |
| 291 | + act_observer=MovingAverageMinMaxObserver, eps: float = None |
285 | 292 | ) -> QuantizationConfig: |
286 | | - # the smallest scale: 0.0001 / 65535 |
287 | | - extra_args: Dict[str, Any] = {"eps": 2**-29} |
| 293 | + # the smallest defaults to DEFAULT_EPS_16BIT |
| 294 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_16BIT} |
288 | 295 | act_fake_quant_ctr = FusedMovingAvgObsFakeQuantize.with_args( |
289 | 296 | dtype=torch.int32, |
290 | 297 | quant_min=torch.iinfo(torch.uint16).min, |
@@ -339,10 +346,10 @@ def get_16a8w_qnn_qat_config( |
339 | 346 |
|
340 | 347 |
|
341 | 348 | def get_16a16w_qnn_ptq_config( |
342 | | - act_observer=MovingAverageMinMaxObserver, |
| 349 | + act_observer=MovingAverageMinMaxObserver, eps: float = None |
343 | 350 | ) -> QuantizationConfig: |
344 | | - # the smallest scale: 0.0001 / 65535 |
345 | | - extra_args: Dict[str, Any] = {"eps": 2**-29} |
| 351 | + # the smallest defaults to DEFAULT_EPS_16BIT |
| 352 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_16BIT} |
346 | 353 | act_quantization_spec = QuantizationSpec( |
347 | 354 | dtype=torch.int32, |
348 | 355 | quant_min=torch.iinfo(torch.uint16).min, |
@@ -385,10 +392,10 @@ def get_ptq_per_channel_quant_config( |
385 | 392 | act_observer=MovingAverageMinMaxObserver, |
386 | 393 | act_symmetric: bool = False, |
387 | 394 | ch_axis: int = 0, |
| 395 | + eps: float = None, |
388 | 396 | ) -> QuantizationConfig: |
389 | | - # the smallest scale: 0.0001 / 65535 |
390 | | - extra_args: Dict[str, Any] = {"eps": 2**-29} |
391 | | - |
| 397 | + # the smallest defaults to DEFAULT_EPS_16BIT |
| 398 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_16BIT} |
392 | 399 | supported_act_types = { |
393 | 400 | torch.uint8, |
394 | 401 | torch.uint16, |
@@ -457,9 +464,10 @@ def get_ptq_per_block_quant_config( |
457 | 464 | act_observer=MovingAverageMinMaxObserver, |
458 | 465 | act_symmetric: bool = False, |
459 | 466 | ch_axis: int = 0, |
| 467 | + eps: float = None, |
460 | 468 | ) -> QuantizationConfig: |
461 | | - # the smallest scale: 0.0001 / 65535 |
462 | | - extra_args: Dict[str, Any] = {"eps": 2**-29} |
| 469 | + # the smallest defaults to DEFAULT_EPS_16BIT |
| 470 | + extra_args: Dict[str, Any] = {"eps": eps if eps else DEFAULT_EPS_16BIT} |
463 | 471 | quantization_config = get_ptq_per_channel_quant_config( |
464 | 472 | act_dtype=act_dtype, |
465 | 473 | weight_dtype=weight_dtype, |
|
0 commit comments