QuantizationPolicy is a core abstraction for PPQ quantization calculation. QuantizationProperty and QuantizationPolicy together build a bitmap to describe quantization policy. A QuantizationPolicy instance contains multiple QuantizationProperty, QuantizationPolicy is used in PPQ
| 211 | |
| 212 | |
| 213 | class QuantizationPolicy: |
| 214 | """QuantizationPolicy is a core abstraction for PPQ quantization |
| 215 | calculation. QuantizationProperty and QuantizationPolicy together build a |
| 216 | bitmap to describe quantization policy. |
| 217 | |
| 218 | A QuantizationPolicy instance contains multiple QuantizationProperty, |
| 219 | QuantizationPolicy is used in PPQ (alone with other configuration) to describe how a tensor is quantized. |
| 220 | |
| 221 | During simulating, executor will quantize tensor corresponding to its QuantizationPolicy. |
| 222 | (QuantizationPolicy is included by TensorQuantizationConfig) |
| 223 | |
| 224 | There are 8 different quantization property(s) supported by PPQ now. |
| 225 | |
| 226 | PER_TENSOR: Also known as per-layer quantization, which mean all parameters of this layer share the same scale and offset. |
| 227 | (For Convulution layer and Gemm layer which has bias, bias layer will be negative quantized, they do not have a valid scale) |
| 228 | |
| 229 | PER_CHANNEL: parameters are quantized alone channel axis, each channel has a stand-alone scale and offset. |
| 230 | |
| 231 | LINEAR: Linear quantization, follow formula: quant(x) = clip(round(x / scale)) |
| 232 | |
| 233 | EXPONENTIAL: Exponential quantization, not yet used. |
| 234 | |
| 235 | SYMMETRICAL: Symmetrical quantization, offset is deactivated in this mode. |
| 236 | |
| 237 | ASYMMETRICAL: Asymmetrical quantization, offset is activated in this mode. |
| 238 | |
| 239 | POWER_OF_2: Power-of-2 quantization, scale must be pow(2, k) in this mode. |
| 240 | |
| 241 | DYNAMIC: Dynamic Activation Quantization, scale is computed on the fly. |
| 242 | |
| 243 | ATTENTION: Not all combinations of all 8 QuantizationProperty are valid, see QuantizationPolicy.__check_valid |
| 244 | ATTENTION: QuantizationPolicy is read-only, user can only assign its value when created, the only interface of |
| 245 | QuantizationPolicy is function QuantizationPolicy.has_property. |
| 246 | """ |
| 247 | def __init__(self, policy: int) -> None: |
| 248 | if not QuantizationPolicy.__check_valid(policy): |
| 249 | raise ValueError( |
| 250 | 'invalid quantization pattern, valid partterns are listed in ' |
| 251 | 'ppq.core.OperationQuantizationPolicy.__check_valid' |
| 252 | ) |
| 253 | self._policy = policy |
| 254 | |
| 255 | def has_property(self, property: QuantizationProperty) -> bool: |
| 256 | return (self._policy & property.value) != 0 |
| 257 | |
| 258 | def __eq__(self, o: object) -> bool: |
| 259 | if not isinstance(o, QuantizationPolicy): |
| 260 | raise TypeError('Can only compare QuantizationPolicy object ' |
| 261 | 'with another QuantizationPolicy object.') |
| 262 | return self._policy == o._policy |
| 263 | |
| 264 | @ classmethod |
| 265 | def __check_valid(cls, policy): |
| 266 | return policy in { |
| 267 | # Standard Int Quantization |
| 268 | QuantizationProperty.ASYMMETRICAL | QuantizationProperty.LINEAR | QuantizationProperty.PER_CHANNEL, |
| 269 | QuantizationProperty.ASYMMETRICAL | QuantizationProperty.LINEAR | QuantizationProperty.PER_TENSOR, |
| 270 | QuantizationProperty.SYMMETRICAL | QuantizationProperty.LINEAR | QuantizationProperty.PER_CHANNEL, |
no outgoing calls
no test coverage detected