|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +""" |
| 5 | +tf2onnx.rewriter - rewrite tensorflow QuantizeAndDequantizeV3 op |
| 6 | +""" |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +from tf2onnx.graph_matcher import OpTypePattern, GraphMatcher |
| 10 | +from tf2onnx import utils |
| 11 | + |
| 12 | +# pylint: disable=missing-docstring |
| 13 | + |
| 14 | +def extract_numpy_array(node): |
| 15 | + return np.frombuffer(node.attr["value"].t.raw_data, dtype="float32") |
| 16 | + |
| 17 | +def create_qdq_nodes(g, match_results): |
| 18 | + |
| 19 | + for match in match_results: |
| 20 | + qdq_node = match.get_op('output') |
| 21 | + qdq_node_output_dtype = g.get_dtype(qdq_node.output[0]) |
| 22 | + qdq_node_output_shape = g.get_shape(qdq_node.output[0]) |
| 23 | + |
| 24 | + # Get the attributes of qdq node |
| 25 | + narrow_range = qdq_node.attr['narrow_range'].i |
| 26 | + signed_input = qdq_node.attr['signed_input'].i |
| 27 | + |
| 28 | + min_quantized, max_quantized = [-127, 127] |
| 29 | + if not narrow_range and signed_input: |
| 30 | + min_quantized = -128 |
| 31 | + |
| 32 | + if not signed_input: |
| 33 | + min_quantized, max_quantized = [0, 255] |
| 34 | + |
| 35 | + # Get the min and max value of the inputs to QDQ op |
| 36 | + min_value = extract_numpy_array(qdq_node.inputs[1]) |
| 37 | + max_value = extract_numpy_array(qdq_node.inputs[2]) |
| 38 | + |
| 39 | + # Calculate scales from the min and max values |
| 40 | + scale_from_min_side = min_quantized/min_value if min_quantized*min_value > 0 else max_quantized |
| 41 | + scale_from_max_side = max_quantized/max_value if max_quantized*max_value > 0 else max_quantized |
| 42 | + |
| 43 | + if scale_from_min_side < scale_from_max_side: |
| 44 | + scale = scale_from_min_side |
| 45 | + else: |
| 46 | + scale = scale_from_max_side |
| 47 | + |
| 48 | + utils.make_sure(scale > 0, "Quantize/Dequantize scale must be greater than zero") |
| 49 | + |
| 50 | + if signed_input: |
| 51 | + zero_point = np.int8(0) |
| 52 | + else: |
| 53 | + zero_point = np.uint8(0) |
| 54 | + |
| 55 | + # Split it into QuantizeLinear and DequantizeLinear and remove the QDQ node reference |
| 56 | + y_quant_scale = g.make_const(name=utils.make_name("y_quant_scale"), np_val=1/scale) |
| 57 | + y_zero_point = g.make_const(name=utils.make_name("y_zero_point"), np_val=zero_point) |
| 58 | + quant_node = g.make_node(op_type="QuantizeLinear", |
| 59 | + inputs=[qdq_node.input[0], y_quant_scale.output[0], |
| 60 | + y_zero_point.output[0]], |
| 61 | + shapes=[qdq_node_output_shape], |
| 62 | + dtypes=[qdq_node_output_dtype], |
| 63 | + name=utils.make_name("QuantLinearNode")) |
| 64 | + |
| 65 | + g.set_shape(quant_node.output[0], qdq_node_output_shape) |
| 66 | + |
| 67 | + g.remove_node(qdq_node.name) |
| 68 | + |
| 69 | + y_dequant_scale = g.make_const(name=utils.make_name("y_dequant_scale"), np_val=1/scale) |
| 70 | + y_inv_zero_point = g.make_const(name=utils.make_name("y_inv_zero_point"), np_val=zero_point) |
| 71 | + dequant_node = g.make_node(op_type="DequantizeLinear", |
| 72 | + inputs=[quant_node.output[0], y_dequant_scale.output[0], |
| 73 | + y_inv_zero_point.output[0]], |
| 74 | + outputs=[qdq_node.output[0]], |
| 75 | + shapes=[qdq_node_output_shape], |
| 76 | + dtypes=[qdq_node_output_dtype], |
| 77 | + name=utils.make_name("DequantLinearNode")) |
| 78 | + g.set_shape(dequant_node.output[0], qdq_node_output_shape) |
| 79 | + |
| 80 | + return g.get_nodes() |
| 81 | + |
| 82 | +def rewrite_quantize_and_dequantize(g, ops): |
| 83 | + |
| 84 | + pattern_for_qdq_v2 = \ |
| 85 | + OpTypePattern('QuantizeAndDequantizeV2', name='output', inputs=[ |
| 86 | + OpTypePattern("*"), |
| 87 | + OpTypePattern(None), |
| 88 | + OpTypePattern(None), |
| 89 | + ]) |
| 90 | + pattern_for_qdq_v3 = \ |
| 91 | + OpTypePattern('QuantizeAndDequantizeV3', name='output', inputs=[ |
| 92 | + OpTypePattern("*"), |
| 93 | + OpTypePattern(None), |
| 94 | + OpTypePattern(None), |
| 95 | + OpTypePattern(None), |
| 96 | + ]) |
| 97 | + |
| 98 | + # Match all the patterns for QDQ ops |
| 99 | + patterns = [pattern_for_qdq_v3, pattern_for_qdq_v2] |
| 100 | + match_results = [] |
| 101 | + for pattern in patterns: |
| 102 | + matcher = GraphMatcher(pattern) |
| 103 | + results = list(matcher.match_ops(ops)) |
| 104 | + match_results.extend(results) |
| 105 | + |
| 106 | + return create_qdq_nodes(g, match_results) |
0 commit comments