MCPcopy Create free account
hub / github.com/XiaoMi/mace / quantize_tensor

Method quantize_tensor

tools/python/transform/transformer.py:2013–2111  ·  view source on GitHub ↗

Assume biasadd has been already folded with convolution and fc

(self, tensor)

Source from the content-addressed store, hash-verified

2011 return False
2012
2013 def quantize_tensor(self, tensor):
2014 """Assume biasadd has been already folded with convolution and fc"""
2015 if tensor.data_type == mace_pb2.DT_FLOAT:
2016 ops = self._consumers.get(tensor.name, None)
2017 check_conv = False
2018 check_deconv = False
2019 if ops is not None and len(ops) == 1:
2020 if len(ops[0].input) >= 3:
2021 check_conv =\
2022 ops[0].type in [MaceOp.Conv2D.name,
2023 MaceOp.DepthwiseConv2d.name,
2024 MaceOp.FullyConnected.name,
2025 MaceOp.MatMul.name]\
2026 and ops[0].input[2] == tensor.name
2027 # in tensorflow deconv's bias is the forth input
2028 if ops[0].type in [MaceOp.Deconv2D.name,
2029 MaceOp.DepthwiseDeconv2d]:
2030 from_caffe = ConverterUtil.get_arg(
2031 ops[0],
2032 MaceKeyword.mace_framework_type_str).i ==\
2033 FrameworkType.CAFFE.value
2034 if from_caffe and len(ops[0].input) >= 3:
2035 check_deconv = ops[0].input[2] == tensor.name
2036 else:
2037 if len(ops[0].input) >= 4:
2038 check_deconv = ops[0].input[3] == tensor.name
2039 if check_conv or check_deconv:
2040 conv_op = ops[0]
2041 scale_input = self._quantize_activation_info[
2042 conv_op.input[0]].scale
2043 if conv_op.input[1] not in self._quantized_tensor:
2044 self.quantize_tensor(self._consts[conv_op.input[1]])
2045 scale_filter = self._consts[conv_op.input[1]].scale
2046 scale = scale_input * scale_filter
2047 quantized_tensor = \
2048 quantize_util.quantize_with_scale_and_zero(
2049 tensor.float_data, scale, 0)
2050 if self._option.device == DeviceType.HEXAGON.value or \
2051 self._option.device == DeviceType.HTA.value:
2052 quantized_tensor.minval = scale * (-2**31)
2053 quantized_tensor.maxval = scale * (2**31 - 1)
2054 tensor.data_type = mace_pb2.DT_INT32
2055 elif self._option.quantize_schema == \
2056 MaceKeyword.mace_apu_16bit_per_tensor:
2057 quantized_tensor = \
2058 quantize_util.quantize_int16(tensor.float_data)
2059 tensor.data_type = mace_pb2.DT_INT16
2060 elif self._option.quantize_schema == MaceKeyword.mace_int8:
2061 quantized_tensor = quantize_util.quantize_int8(
2062 tensor.float_data)
2063 tensor.data_type = mace_pb2.DT_INT8
2064 else:
2065 non_zero = self._option.device == DeviceType.CPU.value
2066 has_qat = False
2067 if InfoKey.has_qat in self._converter_info:
2068 if tensor.name in self._converter_info[InfoKey.has_qat]:
2069 has_qat = True
2070 if has_qat and self._option.platform.name == "ONNX":

Callers 1

quantize_weightsMethod · 0.95

Calls 2

mace_checkFunction · 0.90
get_argMethod · 0.45

Tested by

no test coverage detected