diff --git a/onnxruntime/python/tools/quantization/__init__.py b/onnxruntime/python/tools/quantization/__init__.py index f823219491..0559b6c5f9 100644 --- a/onnxruntime/python/tools/quantization/__init__.py +++ b/onnxruntime/python/tools/quantization/__init__.py @@ -1,4 +1,4 @@ -from .quantize import quantize, quantize_static, quantize_dynamic +from .quantize import quantize_static, quantize_dynamic from .quantize import QuantizationMode from .calibrate import CalibrationDataReader, CalibraterBase, MinMaxCalibrater, create_calibrator, CalibrationMethod from .quant_utils import QuantType, QuantFormat, write_calibration_table diff --git a/onnxruntime/python/tools/quantization/quantize.py b/onnxruntime/python/tools/quantization/quantize.py index 0439e40cf4..82be85f8d2 100644 --- a/onnxruntime/python/tools/quantization/quantize.py +++ b/onnxruntime/python/tools/quantization/quantize.py @@ -34,85 +34,6 @@ def check_static_quant_arguments(quant_format : QuantFormat, "Or it will lead to bad performance on x64.") -def quantize(model, - per_channel=False, - nbits=8, - quantization_mode=QuantizationMode.IntegerOps, - static=False, - symmetric_activation=False, - symmetric_weight=False, - quantization_params=None, - nodes_to_quantize=None, - nodes_to_exclude=None, - op_types_to_quantize=[]): - ''' - Given an onnx model, create a quantized onnx model and save it into a file - :param model: ModelProto to quantize - :param per_channel: quantize weights per channel - :param nbits: number of bits to represent quantized data. Currently only supporting 8-bit types - :param quantization_mode: Can be one of the QuantizationMode types. - IntegerOps: - the function will use integer ops. Only ConvInteger and MatMulInteger ops are supported now. - QLinearOps: - the function will use QLinear ops. Only QLinearConv and QLinearMatMul ops are supported now. - :param static: - True: The inputs/activations are quantized using static scale and zero point values - specified through quantization_params. - False: The inputs/activations are quantized using dynamic scale and zero point values - computed while running the model. - :param symmetric_activation: - True: activations are quantized into signed integers. - False: activations are quantized into unsigned integers. - :param symmetric_weight: - True: weights are quantized into signed integers. - False: weights are quantized into unsigned integers. - :param quantization_params: - Dictionary to specify the zero point and scale values for inputs to conv and matmul nodes. - Should be specified when static is set to True. - The quantization_params should be specified in the following format: - { - "input_name": [zero_point, scale] - }. - zero_point should be of type np.uint8 and scale should be of type np.float32. - example: - { - 'resnet_model/Relu_1:0': [np.uint8(0), np.float32(0.019539741799235344)], - 'resnet_model/Relu_2:0': [np.uint8(0), np.float32(0.011359662748873234)] - } - :param nodes_to_quantize: - List of nodes names to quantize. When this list is not None only the nodes in this list - are quantized. - example: - [ - 'Conv__224', - 'Conv__252' - ] - :param nodes_to_exclude: - List of nodes names to exclude. The nodes in this list will be excluded from quantization - when it is not None. - :param op_types_to_quantize: specify the types of operators to quantize, like ['Conv'] to quantize Conv only. It quantizes all supported operators by default. - :return: ModelProto with quantization - ''' - logging.warning("onnxruntime.quantization.quantize is deprecated.\n\ - Please use quantize_static for static quantization, quantize_dynamic for dynamic quantization.") - if nbits == 8 or nbits == 7: - mode = quantization_mode - copy_model = onnx_proto.ModelProto() - copy_model.CopyFrom(model) - - if not op_types_to_quantize or len(op_types_to_quantize) == 0: - op_types_to_quantize = list(QLinearOpsRegistry.keys()) if static else list(IntegerOpsRegistry.keys()) - - quantizer = ONNXQuantizer(copy_model, per_channel, nbits == 7, mode, static, symmetric_weight, - symmetric_activation, quantization_params, nodes_to_quantize, nodes_to_exclude, - op_types_to_quantize) - - quantizer.quantize_model() - return quantizer.model.model - else: - raise ValueError('Only 8 and 7 bit quantization is currently supported') - - def quantize_static(model_input, model_output, calibration_data_reader: CalibrationDataReader,