MCPcopy
hub / github.com/OpenPPL/ppq / PPQLinearQuant_toInt

Function PPQLinearQuant_toInt

ppq/quantization/qfunction/linear.py:218–238  ·  view source on GitHub ↗

PPQ 核心量化函数,没啥好说的了吧,这个玩意只做 quant 不做 dequant

(tensor: torch.Tensor, config: TensorQuantizationConfig)

Source from the content-addressed store, hash-verified

216 config.quant_min, config.quant_max, config.rounding)
217
218def PPQLinearQuant_toInt(tensor: torch.Tensor, config: TensorQuantizationConfig) -> torch.Tensor:
219 """PPQ 核心量化函数,没啥好说的了吧,这个玩意只做 quant 不做 dequant"""
220 if not config.policy.has_property(QuantizationProperty.LINEAR):
221 raise ValueError('Critical Quantization Error! Non-linear config detected.')
222 if config.policy.has_property(QuantizationProperty.PER_CHANNEL):
223 shape = [1 if axis != config.channel_axis else -1 for axis in range(tensor.ndim)]
224 scale, offset = config.scale.view(shape), config.offset.view(shape)
225 tensor = ppq_tensor_round((tensor / scale), config.rounding) + offset
226 tensor = torch.clamp(tensor, config.quant_min, config.quant_max)
227 elif config.policy.has_property(QuantizationProperty.PER_TENSOR):
228 tensor = ppq_tensor_round((tensor / config.scale), config.rounding) + config.offset
229 tensor = torch.clamp(tensor, config.quant_min, config.quant_max)
230
231 if config.num_of_bits == 8:
232 if config.policy.has_property(QuantizationProperty.SYMMETRICAL):
233 return tensor.type(dtype=torch.int8)
234 if config.policy.has_property(QuantizationProperty.ASYMMETRICAL):
235 return tensor.type(dtype=torch.uint8)
236 elif config.num_of_bits > 8:
237 return tensor.type(dtype=torch.int32)
238 else: raise Exception('Do not konw how to convert value into int. num of bits is unexpected.')

Callers 2

convert_operationMethod · 0.90
PPQuantFunction_toIntFunction · 0.85

Calls 3

ppq_tensor_roundFunction · 0.90
has_propertyMethod · 0.80
typeMethod · 0.80

Tested by

no test coverage detected