Skip to content

Commit

Permalink
Refine ORT QDQ quantization graph (#589)
Browse files Browse the repository at this point in the history
Signed-off-by: yuwenzho <yuwen.zhou@intel.com>
  • Loading branch information
yuwenzho committed Feb 28, 2023
1 parent 64db5c5 commit c64a5ba
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions neural_compressor/adaptor/ox_utils/quantizer.py
Expand Up @@ -220,6 +220,23 @@ def merge_dedicated_qdq_pair(self):
for node, old_input_name, new_input_name in self.replace_input:
self.model.replace_node_input(node, old_input_name, new_input_name)
self.model.update()

if self.mode == 'qdq':
for node in self.model.nodes():
if node.op_type in ['QuantizeLinear'] and len(self.model.get_parents(node)) > 0:
if 'QuantizeLinear' in [sibling.op_type \
for sibling in self.model.get_siblings(node)]:
continue
for sibling in self.model.get_siblings(node):
if not self.should_quantize(sibling) and sibling.op_type in OPERATORS:
for inp_idx in range(len(sibling.input)):
if sibling.input[inp_idx] == node.input[0]:
self.replace_input.append([sibling,
sibling.input[inp_idx],
self.model.get_children(node)[0].output[0]])
for node, old_input_name, new_input_name in self.replace_input:
self.model.replace_node_input(node, old_input_name, new_input_name)
self.model.update()

def should_cast(self, node):
"""Check if node should be casted."""
Expand Down
1 change: 1 addition & 0 deletions neural_compressor/experimental/benchmark.py
Expand Up @@ -604,6 +604,7 @@ def postprocess(self, user_postprocess):
logger.warning("Override the value of `postprocess` field defined in yaml file" \
" as user defines the value of `postprocess` attribute by code.")
deep_set(self.conf.usr_cfg, "evaluation.accuracy.postprocess.transform", postprocess_cfg)
from neural_compressor.data import TRANSFORMS
postprocesses = TRANSFORMS(self.framework, 'postprocess')
postprocesses.register(user_postprocess.name, user_postprocess.postprocess_cls)

Expand Down
9 changes: 9 additions & 0 deletions neural_compressor/model/onnx_model.py
Expand Up @@ -253,6 +253,15 @@ def _get_output_name_to_node(self, nodes):
for output_name in node.output:
self._output_name_to_node[output_name] = node

def get_siblings(self, node):
"""Get siblings nodes."""
siblings = []
for parent in self.get_parents(node):
for child in self.get_children(parent):
if child.name != node.name:
siblings.append(child)
return siblings

def get_children(self, node, input_name_to_nodes=None):
"""Get children nodes."""
if input_name_to_nodes is None:
Expand Down
10 changes: 10 additions & 0 deletions test/model/test_onnx_model.py
Expand Up @@ -203,6 +203,16 @@ def test_output_name_to_node(self):
opts = ['X1', 'X2', 'X3', 'X4', 'X5', 'output']
for opt in opts:
self.assertTrue(opt in opts_name)

def test_get_siblings(self):
for node in self.model.nodes():
if node.name == "Conv1":
siblings = self.model.get_siblings(node)
self.assertEqual(len(siblings), 1)
siblings_name = [sibling.name for sibling in siblings]
names = ["Conv3"]
for name in names:
self.assertTrue(name in siblings_name)

def test_get_children(self):
for node in self.model.nodes():
Expand Down

0 comments on commit c64a5ba

Please sign in to comment.