## PPQ 核心量化函数 根据 config 中描述的策略,量化给定的 tensor. 请注意 config.state 必须处在激活状态,该函数起作用。如果 config.state 处于 INITIAL, FP32, PASSIVE_INIT 等未激活状态,该函数不进行任何处理,直接返回 tensor. ### 线性量化(QuantizationProperty.LINEAR): INT8 = Clip(Round((FP32 / scale))) ### 浮点量化(QuantizationPro
(tensor: torch.Tensor, config: TensorQuantizationConfig)
| 8 | |
| 9 | |
| 10 | def PPQuantFunction(tensor: torch.Tensor, config: TensorQuantizationConfig) -> torch.Tensor: |
| 11 | """ |
| 12 | ## PPQ 核心量化函数 |
| 13 | |
| 14 | 根据 config 中描述的策略,量化给定的 tensor. |
| 15 | |
| 16 | 请注意 config.state 必须处在激活状态,该函数起作用。如果 config.state 处于 |
| 17 | INITIAL, FP32, PASSIVE_INIT 等未激活状态,该函数不进行任何处理,直接返回 tensor. |
| 18 | |
| 19 | ### 线性量化(QuantizationProperty.LINEAR): |
| 20 | |
| 21 | INT8 = Clip(Round((FP32 / scale))) |
| 22 | |
| 23 | ### 浮点量化(QuantizationProperty.FLOATING): |
| 24 | |
| 25 | FP8 = Clip(FP32_TO_FP8((FP32 / scale))) |
| 26 | |
| 27 | ### 动态线性量化(QuantizationProperty.DYNMAIC) |
| 28 | |
| 29 | scale = max(FP32) / 255 |
| 30 | |
| 31 | INT8 = Clip(Round((FP32 / scale))) |
| 32 | |
| 33 | """ |
| 34 | if tensor is None: raise ValueError('Tensor is empty.') |
| 35 | if config.policy.has_property(QuantizationProperty.LINEAR): |
| 36 | if not config.policy.has_property(QuantizationProperty.DYNAMIC): |
| 37 | return PPQLinearQuantFunction(tensor, config) |
| 38 | else: return PPQDyamicLinearQuantFunction(tensor, config) |
| 39 | |
| 40 | if config.policy.has_property(QuantizationProperty.FLOATING): |
| 41 | return PPQFloatingQuantFunction(tensor, config) |
| 42 | |
| 43 | raise ValueError('Unexpected Quantization Property Found in PPQuantFunction. ' |
| 44 | 'Do not konw how to quantize your config yet.') |
| 45 | |
| 46 | |
| 47 | def PPQuantFunction_toInt(tensor: torch.Tensor, config: TensorQuantizationConfig) -> torch.Tensor: |
no test coverage detected