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

Method insert_dequantize_node

ppq/parser/onnxruntime_exporter.py:147–211  ·  view source on GitHub ↗

Insert a DeQuantize Node on given variable, according to given TensorQuantizationConfig. There is two basic type of DeQuantize Node: DeQuantizeLinear and DeQuantizeFloating.

(
        self, graph: BaseGraph, 
        var: Variable, config: TensorQuantizationConfig, 
        op: Operation)

Source from the content-addressed store, hash-verified

145 'Unexpected Quantization property.')
146
147 def insert_dequantize_node(
148 self, graph: BaseGraph,
149 var: Variable, config: TensorQuantizationConfig,
150 op: Operation) -> Operation:
151 """
152 Insert a DeQuantize Node on given variable, according to given TensorQuantizationConfig.
153 There is two basic type of DeQuantize Node: DeQuantizeLinear and DeQuantizeFloating.
154 """
155 if config.policy.has_property(QuantizationProperty.LINEAR):
156 offset_dtype, value_type = self.infer_qtype(config)
157 scale = convert_any_to_torch_tensor(config.scale.clone(), dtype=torch.float32)
158 offset = ppq_tensor_round(config.offset.clone()).type(offset_dtype)
159
160 created = graph.create_operation(op_type='DequantizeLinear', attributes={})
161 if config.policy.has_property(QuantizationProperty.PER_CHANNEL):
162 created.attributes['axis'] = config.channel_axis
163 else: created.attributes['axis'] = None
164
165 if var in op.inputs: graph.insert_op_before(A=created, B=op, input_idx=op.inputs.index(var))
166 elif var in op.outputs: graph.insert_op_after(A=created, B=op, output_idx=op.outputs.index(var))
167 else: raise ValueError(f'Unexpected Error in Exporting Op {op.name}({op.type}).')
168
169 graph.create_variable(name=None, value=scale, is_parameter=True, dest_ops=[created])
170 graph.create_variable(name=None, value=offset, is_parameter=True, dest_ops=[created])
171
172 created.inputs[0].dtype = value_type
173 created.inputs[0].shape = var.shape
174 created.outputs[0].shape = var.shape
175 created.outputs[0].dtype = torch.float32
176
177 return created
178
179 elif config.policy.has_property(QuantizationProperty.FLOATING):
180 scale = convert_any_to_torch_tensor(config.scale.clone(), dtype=torch.float32)
181 offset = convert_any_to_torch_tensor(config.offset.clone(), dtype=torch.float32)
182
183 created = graph.create_operation(
184 op_type='DequantizeFloating',
185 attributes={
186 'min': config.quant_min,
187 'max': config.quant_max,
188 'exponent': config.exponent_bits,
189 'mantissa': config.mantissa_bits})
190
191 if config.policy.has_property(QuantizationProperty.PER_CHANNEL):
192 created.attributes['axis'] = config.channel_axis
193 else: created.attributes['axis'] = None
194
195 if var in op.inputs: graph.insert_op_before(A=created, B=op, input_idx=op.inputs.index(var))
196 elif var in op.outputs: graph.insert_op_after(A=created, B=op, output_idx=op.outputs.index(var))
197 else: raise ValueError(f'Unexpected Error in Exporting Op {op.name}({op.type}).')
198
199 graph.create_variable(name=None, value=scale, is_parameter=True, dest_ops=[created])
200 graph.create_variable(name=None, value=offset, is_parameter=True, dest_ops=[created])
201
202 created.outputs[0].shape = var.shape
203 created.inputs[0].shape = var.shape
204

Callers 2

remove_activation_opsMethod · 0.95
convert_operationMethod · 0.95

Calls 9

infer_qtypeMethod · 0.95
ppq_tensor_roundFunction · 0.90
has_propertyMethod · 0.80
typeMethod · 0.80
create_operationMethod · 0.80
insert_op_beforeMethod · 0.80
insert_op_afterMethod · 0.80
create_variableMethod · 0.80

Tested by

no test coverage detected