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

Method insert_dequantize_node

ppq/parser/openvino_exporter.py:80–144  ·  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

78 'Unexpected Quantization property.')
79
80 def insert_dequantize_node(
81 self, graph: BaseGraph,
82 var: Variable, config: TensorQuantizationConfig,
83 op: Operation) -> Operation:
84 """
85 Insert a DeQuantize Node on given variable, according to given TensorQuantizationConfig.
86 There is two basic type of DeQuantize Node: DeQuantizeLinear and DeQuantizeFloating.
87 """
88 if config.policy.has_property(QuantizationProperty.LINEAR):
89 offset_dtype, value_type = self.infer_qtype(config)
90 scale = convert_any_to_torch_tensor(config.scale.clone(), dtype=torch.float32)
91 offset = ppq_tensor_round(config.offset.clone()).type(offset_dtype)
92
93 created = graph.create_operation(op_type='DequantizeLinear', attributes={})
94 if config.policy.has_property(QuantizationProperty.PER_CHANNEL):
95 created.attributes['axis'] = config.channel_axis
96 else: created.attributes['axis'] = 0
97
98 if var in op.inputs: graph.insert_op_before(A=created, B=op, input_idx=op.inputs.index(var))
99 elif var in op.outputs: graph.insert_op_after(A=created, B=op, output_idx=op.outputs.index(var))
100 else: raise ValueError(f'Unexpected Error in Exporting Op {op.name}({op.type}).')
101
102 graph.create_variable(name=None, value=scale, is_parameter=True, dest_ops=[created])
103 graph.create_variable(name=None, value=offset, is_parameter=True, dest_ops=[created])
104
105 created.inputs[0].dtype = value_type
106 created.inputs[0].shape = var.shape
107 created.outputs[0].shape = var.shape
108 created.outputs[0].dtype = torch.float32
109
110 return created
111
112 elif config.policy.has_property(QuantizationProperty.FLOATING):
113 scale = convert_any_to_torch_tensor(config.scale.clone(), dtype=torch.float32)
114 offset = convert_any_to_torch_tensor(config.offset.clone(), dtype=torch.float32)
115
116 created = graph.create_operation(
117 op_type='DequantizeFloating',
118 attributes={
119 'min': config.quant_min,
120 'max': config.quant_max,
121 'exponent': config.exponent_bits,
122 'mantissa': config.mantissa_bits})
123
124 if config.policy.has_property(QuantizationProperty.PER_CHANNEL):
125 created.attributes['axis'] = config.channel_axis
126 else: created.attributes['axis'] = None
127
128 if var in op.inputs: graph.insert_op_before(A=created, B=op, input_idx=op.inputs.index(var))
129 elif var in op.outputs: graph.insert_op_after(A=created, B=op, output_idx=op.outputs.index(var))
130 else: raise ValueError(f'Unexpected Error in Exporting Op {op.name}({op.type}).')
131
132 graph.create_variable(name=None, value=scale, is_parameter=True, dest_ops=[created])
133 graph.create_variable(name=None, value=offset, is_parameter=True, dest_ops=[created])
134
135 created.outputs[0].shape = var.shape
136 created.inputs[0].shape = var.shape
137

Callers

nothing calls this directly

Calls 9

ppq_tensor_roundFunction · 0.90
has_propertyMethod · 0.80
infer_qtypeMethod · 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