|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +""" |
| 5 | +tensor |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import division |
| 9 | +from __future__ import print_function |
| 10 | +from __future__ import unicode_literals |
| 11 | + |
| 12 | +import logging |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +from onnx.onnx_pb import TensorProto |
| 16 | + |
| 17 | +from tf2onnx import utils |
| 18 | +from tf2onnx.handler import tf_op |
| 19 | +from tf2onnx.utils import make_sure |
| 20 | + |
| 21 | +logger = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +# pylint: disable=unused-argument,missing-docstring,unused-variable,pointless-string-statement,invalid-name |
| 25 | + |
| 26 | + |
| 27 | +@tf_op("FakeQuantWithMinMaxArgs") |
| 28 | +class FakeQuantWithMinMaxArgs: |
| 29 | + # see https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/fake-quant-with-min-max-args |
| 30 | + @classmethod |
| 31 | + def version_10(cls, ctx, node, **kwargs): |
| 32 | + # hack to make up for the missing onnx pack op |
| 33 | + amin = node.get_attr("min").f |
| 34 | + amax = node.get_attr("max").f |
| 35 | + narrow_range = node.get_attr("narrow_range").i |
| 36 | + num_bits = node.get_attr("num_bits").i |
| 37 | + |
| 38 | + make_sure( |
| 39 | + not narrow_range, |
| 40 | + "Unable to convert node FakeQuantWithMinMaxArgs with narrow_range=%r", |
| 41 | + narrow_range) |
| 42 | + make_sure( |
| 43 | + num_bits == 8, |
| 44 | + "Unable to convert node FakeQuantWithMinMaxArgs with " |
| 45 | + "num_bits=%r", num_bits) |
| 46 | + |
| 47 | + scale = (amax - amin) / (2 ** num_bits - 1) |
| 48 | + min_adj = np.around(amin / scale) |
| 49 | + |
| 50 | + dtype = ctx.get_dtype(node.input[0]) |
| 51 | + shape = ctx.get_shape(node.input[0]) |
| 52 | + axis = 1 |
| 53 | + idtype = TensorProto.UINT8 |
| 54 | + |
| 55 | + pb_scale = ctx.make_const( |
| 56 | + utils.make_name("{}_scaley".format(node.name)), |
| 57 | + np.array(scale, dtype=np.float32)) |
| 58 | + zero = np.array(-min_adj, dtype=np.uint8) |
| 59 | + make_sure( |
| 60 | + zero == -min_adj, |
| 61 | + "Cannot convert FakeQuantWithMinMaxArgs with " |
| 62 | + "min=%r max=%r numbits=%r because zero_scale=%r " |
| 63 | + "is outside uint8 boundary", |
| 64 | + amin, amax, num_bits, -min_adj) |
| 65 | + zero_point = ctx.make_const( |
| 66 | + utils.make_name("{}_zpy".format(node.name)), zero) |
| 67 | + |
| 68 | + new_node = ctx.make_node( |
| 69 | + "QuantizeLinear", [node.input[0], pb_scale.name, zero_point.name], |
| 70 | + op_name_scope=node.name, attr={"axis": axis}, |
| 71 | + shapes=[shape], dtypes=[idtype]) |
| 72 | + output_name = new_node.output[0] |
| 73 | + node.input[0] = output_name |
| 74 | + |
| 75 | + ctx.remove_node(node.name) |
| 76 | + |
| 77 | + last_node = ctx.make_node( |
| 78 | + "DequantizeLinear", [new_node.output[0], pb_scale.name, zero_point.name], |
| 79 | + op_name_scope=node.name, attr={"axis": axis}, |
| 80 | + shapes=[shape], dtypes=[dtype]) |
| 81 | + ctx.replace_all_inputs(ctx.get_nodes(), node.output[0], last_node.output[0]) |
0 commit comments