(
tensor: torch.Tensor, config: TensorQuantizationConfig)
| 93 | |
| 94 | |
| 95 | def PPQFloatingQuantFunction( |
| 96 | tensor: torch.Tensor, config: TensorQuantizationConfig) -> torch.Tensor: |
| 97 | if not PPQ_CONFIG.USING_CUDA_KERNEL: |
| 98 | raise PermissionError('PPQ Floating Quant Function require PPQ_CONFIG.USING_CUDA_KERNEL = True') |
| 99 | if not tensor.is_cuda: |
| 100 | raise PermissionError('PPQ Floating Quant Function requires tensor device to be cuda, ' |
| 101 | 'CPU floating quantization is not implemented yet.') |
| 102 | |
| 103 | """PPQ 核心量化函数,没啥好说的了吧,这个玩意既做 quant 也做 dequant""" |
| 104 | if not QuantizationStates.is_activated(config.state): return tensor |
| 105 | if not config.policy.has_property(QuantizationProperty.FLOATING): |
| 106 | raise ValueError('Critical Quantization Error! Unexpected policy detected. ' |
| 107 | 'PPQFloatingQuantFunction except a Floating Quantization Config.') |
| 108 | if config.policy.has_property(QuantizationProperty.DYNAMIC): |
| 109 | raise ValueError('Unexpected Dynamic Flag in Quantization Policy.') |
| 110 | |
| 111 | if config.policy.has_property(QuantizationProperty.PER_CHANNEL): |
| 112 | return ChannelwiseFloatingQuantImpl.apply( |
| 113 | tensor, config.scale, config.offset, config.channel_axis, |
| 114 | config.exponent_bits, config.mantissa_bits, |
| 115 | config.quant_min, config.quant_max, config.rounding) |
| 116 | elif config.policy.has_property(QuantizationProperty.PER_TENSOR): |
| 117 | return TensorwiseFloatingQuantImpl.apply( |
| 118 | tensor, config.scale, config.offset, |
| 119 | config.exponent_bits, config.mantissa_bits, |
| 120 | config.quant_min, config.quant_max, config.rounding) |
no test coverage detected