From 09fdaa28f53f20a6feb4b6d817d9c42a840bd669 Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Fri, 9 Aug 2019 15:48:36 -0700 Subject: [PATCH 1/9] QNN quantize and dequantize operators. --- include/tvm/relay/qnn/attrs.h | 32 ++++++ python/tvm/relay/qnn/op/qnn.py | 47 +++++++++ src/relay/qnn/op/dequantize.cc | 105 ++++++++++++++++++ src/relay/qnn/op/quantize_op.cc | 123 ++++++++++++++++++++++ src/relay/qnn/op/requantize.cc | 6 +- tests/python/relay/test_qnn_dequantize.py | 73 +++++++++++++ tests/python/relay/test_qnn_quantize.py | 77 ++++++++++++++ tests/python/relay/test_qnn_requantize.py | 1 - 8 files changed, 460 insertions(+), 4 deletions(-) create mode 100644 src/relay/qnn/op/dequantize.cc create mode 100644 src/relay/qnn/op/quantize_op.cc create mode 100644 tests/python/relay/test_qnn_dequantize.py create mode 100644 tests/python/relay/test_qnn_quantize.py diff --git a/include/tvm/relay/qnn/attrs.h b/include/tvm/relay/qnn/attrs.h index e99602813229..1ebdeaa7d708 100644 --- a/include/tvm/relay/qnn/attrs.h +++ b/include/tvm/relay/qnn/attrs.h @@ -65,6 +65,38 @@ struct RequantizeAttrs : public tvm::AttrsNode { } }; +/*! \brief Attribute for quantize operator */ +struct QuantizeAttrs : public tvm::AttrsNode { + int32_t output_zero_point; + double output_scale; + DataType out_dtype; + + TVM_DECLARE_ATTRS(QuantizeAttrs, "relay.attrs.QuantizeAttrs") { + TVM_ATTR_FIELD(out_dtype) + .describe("Output data type, can be one of [int8 or uint8]."); + + TVM_ATTR_FIELD(output_zero_point) + .describe("The zero_point for the activation of this op."); + + TVM_ATTR_FIELD(output_scale) + .describe("The scale for the activation of this op."); + } +}; + +/*! \brief Attribute for dequantize operator */ +struct DequantizeAttrs : public tvm::AttrsNode { + int32_t input_zero_point; + double input_scale; + + TVM_DECLARE_ATTRS(QuantizeAttrs, "relay.attrs.QuantizeAttrs") { + TVM_ATTR_FIELD(input_zero_point) + .describe("The zero_point for the input tensor of this op."); + + TVM_ATTR_FIELD(input_scale) + .describe("The scale for the input tensor of this op."); + } +}; + } // namespace qnn } // namespace relay } // namespace tvm diff --git a/python/tvm/relay/qnn/op/qnn.py b/python/tvm/relay/qnn/op/qnn.py index 1717bc42fe94..4b666bdcc4f8 100644 --- a/python/tvm/relay/qnn/op/qnn.py +++ b/python/tvm/relay/qnn/op/qnn.py @@ -72,3 +72,50 @@ def requantize(data, output_zero_point, rounding, out_dtype) + + +def quantize(input_data, output_zero_point, output_scale, out_dtype='int8'): + r""" Quantize op + This operator takes float32 as input and produces quantized int8 or unit8 as output. + The input tensor can be of any shape. The output shape is the same as input shape. + ..math:: + \mbox{out}[x] = + \mbox{clamp(round(input_tensor/output_scale) + output_zero_point); + out_dtype::min, out_dtype::max} + Parameters + ---------- + input_data : tvm.relay.Expr + The input tensor to be quantized. Can be of type float32. + output_zero_point : + The output zero_point. + output_scale: + The output scale. + input_dtype: + The data type of the input tensor. Can be [int8, uint8] + Returns + ------- + result : tvm.relay.Expr + The computed result. + """ + return _make.quantize(input_data, output_zero_point, output_scale, out_dtype) + + +def dequantize(input_data, input_zero_point, input_scale): + r""" Dequantize op + This operator takes quantized int8 and unit8 as input and produces + dequantized float32 as output. The output shape is the same as input shape. The input + tensor can be of any shape. + Parameters + ---------- + input_data : tvm.relay.Expr + The input tensor to be dequantized. Can be of type [int8, uint8]. + input_zero_point : + The output zero_point. + input_scale: + The output scale. + Returns + ------- + result : tvm.relay.Expr + The computed result. + """ + return _make.dequantize(input_data, input_zero_point, input_scale) diff --git a/src/relay/qnn/op/dequantize.cc b/src/relay/qnn/op/dequantize.cc new file mode 100644 index 000000000000..90b4be896a27 --- /dev/null +++ b/src/relay/qnn/op/dequantize.cc @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file src/relay/qnn/op/dequantize.cc + * \brief QNN dequantize operator. Dequantize operator converts from quantized + * domain to unquantized domain. + */ + +#include +//#include +#include +#include +#include "../../pass/pattern_util.h" +#include "../util.h" + +namespace tvm { +namespace relay { +namespace qnn { + +TVM_REGISTER_NODE_TYPE(DequantizeAttrs); + +bool DequantizeRel(const Array& types, + int num_inputs, + const Attrs& attrs, + const TypeReporter& reporter) { + CHECK_EQ(types.size(), 2); + const auto* data = types[0].as(); + const auto input_dtype = data->dtype; + CHECK(input_dtype == Int(8) || input_dtype == UInt(8)) + << "Input type should be one of the quantized types [unit8, int8] but was " << input_dtype; + const Array oshape = data->shape; + // assign output type, output will always be float 32. + reporter->Assign(types[1], TensorTypeNode::make(oshape, Float(32))); + return true; +} + +Expr MakeDequantize(Expr data, + int32_t input_zero_point, + double input_scale) { + auto attrs = make_node(); + attrs->input_scale = input_scale; + attrs->input_zero_point = input_zero_point; + static const Op& op = Op::Get("qnn.dequantize"); + return CallNode::make(op, {data}, Attrs(attrs), {}); +} + +Expr DequantizeLower(const Expr& input_tensor, const DequantizeAttrs* param) { + const auto input_zero_point = MakeConstantScalar(Int(32), param->input_zero_point); + const auto input_scale = MakeConstantScalar(Float(32), param->input_scale); + auto shift = Subtract(Cast(input_tensor, Int(32)), input_zero_point); + auto scaled_output = Multiply(Cast(shift, Float(32)), input_scale); + return scaled_output; +} + +Expr DequantizeLegalize(const Attrs& attrs, const Array& new_args, + const Array& arg_types) { + CHECK_EQ(new_args.size(), 1); + auto& data = new_args[0]; + const auto* param = attrs.as(); + CHECK(param != nullptr); + + CHECK_EQ(arg_types.size(), 1); + auto input_dtype = arg_types[0]; + auto input_tensor_type = input_dtype.as(); + CHECK(input_tensor_type != nullptr) << "Type information missing." + << " Please run infer_type pass."; + return DequantizeLower(data, param); +} + +RELAY_REGISTER_OP("qnn.dequantize") + .describe(R"code(Dequantizes the input and produces float32 output. +The input is always quantized (int8, uint8) and will be converted to float32 given input scale and zero_point. +- **data**: Quantized tensor of any shape to dequantize. The input data can be of floating point +)code" TVM_ADD_FILELINE) +.set_attrs_type_key("relay.attrs.DequantizeAttrs") +.set_num_inputs(1) +.add_argument("data", "Tensor", "The tensor to dequantize.") +.set_support_level(11) +.add_type_rel("Dequantize", DequantizeRel) +.set_attr("FTVMLegalize", DequantizeLegalize); + +TVM_REGISTER_API("relay.qnn.op._make.dequantize") +.set_body_typed(MakeDequantize); + +} // namespace qnn +} // namespace relay +} // namespace tvm diff --git a/src/relay/qnn/op/quantize_op.cc b/src/relay/qnn/op/quantize_op.cc new file mode 100644 index 000000000000..3a69766eba3c --- /dev/null +++ b/src/relay/qnn/op/quantize_op.cc @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * Copyright (c) 2019 by Contributors + * \file src/relay/qnn/op/quantize_op.cc + * \brief QNN dequantize operator. Dequantize operator converts from quantized + * domain to unquantized domain. + */ + +#include +//#include +#include +#include +#include "../../pass/pattern_util.h" +#include "../util.h" + +namespace tvm { +namespace relay { +namespace qnn { + +TVM_REGISTER_NODE_TYPE(QuantizeAttrs); + +bool QuantizeRel(const Array& types, + int num_inputs, + const Attrs& attrs, + const TypeReporter& reporter) { + CHECK_EQ(types.size(), 2); + const auto* data = types[0].as(); + const auto input_dtype = data->dtype; + CHECK(input_dtype == Float(32)) + << "Input type should be one of float32 but was " << input_dtype; + const auto* param = attrs.as(); + const Array oshape = data->shape; + const DataType out_dtype = param->out_dtype; + CHECK(out_dtype == Int(8) || out_dtype == UInt(8)) + << "Output type should be one of [int8, unit8 ] but was " << out_dtype; + // assign output type + reporter->Assign(types[1], TensorTypeNode::make(oshape, out_dtype)); + return true; +} + +Expr MakeQuantize(Expr data, + int32_t output_zero_point, + double output_scale, + DataType out_dtype) { + auto attrs = make_node(); + attrs->output_scale = output_scale; + attrs->output_zero_point = output_zero_point; + attrs->out_dtype = std::move(out_dtype); + static const Op& op = Op::Get("qnn.quantize"); + return CallNode::make(op, {data}, Attrs(attrs), {}); +} + +Expr QuantizeLower(const Expr& input_tensor, const QuantizeAttrs* param) { + const auto out_dtype = param->out_dtype; + const auto output_zero_point = MakeConstantScalar(Int(32), param->output_zero_point); + const auto scale = MakeConstantScalar(Float(32), param->output_scale); + const int32_t min_val = GetQmin(out_dtype); + const int32_t max_val = GetQmax(out_dtype); + auto scale_data = Cast(Round(Divide(input_tensor, scale)), Int(32)); + // we are trying to do - std::min(std::max(unclamped, min_val), max_val); + auto add_zero_point = Add(scale_data, output_zero_point); + auto clamped_output = Clip(add_zero_point, min_val, max_val); + auto clamp_out_dtype = Cast(clamped_output, out_dtype); + return clamp_out_dtype; +} + +Expr QuantizeLegalize(const Attrs& attrs, const Array& new_args, + const Array& arg_types) { + CHECK_EQ(new_args.size(), 1); + auto& data = new_args[0]; + const auto* param = attrs.as(); + CHECK(param != nullptr); + + CHECK_EQ(arg_types.size(), 1); + auto input_dtype = arg_types[0]; + auto input_tensor_type = input_dtype.as(); + CHECK(input_tensor_type != nullptr) << "Type information missing." + << " Please run infer_type pass."; + return QuantizeLower(data, param); +} + +RELAY_REGISTER_OP("qnn.quantize") +.describe(R"code(Quantizes the input and produces quantized output. +The input can be either float or quantized(int8, unit8). If the input is float, +this op takes scale and zero point and quantize the float value to +quantized output, in int8 or uint8 format. If the input is quantized value, +the op requantize the input (of a certain type, with a given scale and zero +point) to the output of the same or different type with a same or different +scale and zero point. +- **data**: Tensor of any shape to quantize. The input data can be of floating point + or quantized. +)code" TVM_ADD_FILELINE) +.set_attrs_type_key("relay.attrs.QuantizeAttrs") +.set_num_inputs(1) +.add_argument("data", "Tensor", "The tensor to quantize.") +.set_support_level(11) +.add_type_rel("Quantize", QuantizeRel) +.set_attr("FTVMLegalize", QuantizeLegalize); + +TVM_REGISTER_API("relay.qnn.op._make.quantize") +.set_body_typed(MakeQuantize); + +} // namespace qnn +} // namespace relay +} // namespace tvm diff --git a/src/relay/qnn/op/requantize.cc b/src/relay/qnn/op/requantize.cc index 04f7e80d5c64..3bab73935589 100644 --- a/src/relay/qnn/op/requantize.cc +++ b/src/relay/qnn/op/requantize.cc @@ -19,7 +19,7 @@ /*! * Copyright (c) 2019 by Contributors - * \file requantize.cc + * \file src/relay/qnn/op/requantize.cc * \brief QNN requantize operator. */ @@ -228,14 +228,14 @@ bool RequantizeRel(const Array& types, int num_inputs, const Attrs& attrs, const auto* data = types[0].as(); const auto in_dtype = data->dtype; CHECK(in_dtype == Int(8) || in_dtype == UInt(8) || in_dtype == Int(32)) - << "Input type should be an integer but was " << in_dtype; + << "Input type should be one of [int8, uint8, int32] but was " << in_dtype; const Array oshape = data->shape; // assign output type const RequantizeAttrs* param = attrs.as(); auto out_dtype = param->out_dtype; CHECK(out_dtype == Int(8) || out_dtype == UInt(8) || out_dtype == Int(32)) - << "Output type should be an integer but was " << out_dtype; + << "Output type should be one of [int8, uint8, int32] integer but was " << out_dtype; reporter->Assign(types[1], TensorTypeNode::make(oshape, out_dtype)); return true; } diff --git a/tests/python/relay/test_qnn_dequantize.py b/tests/python/relay/test_qnn_dequantize.py new file mode 100644 index 000000000000..6f20d46f6de0 --- /dev/null +++ b/tests/python/relay/test_qnn_dequantize.py @@ -0,0 +1,73 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import tvm +import numpy as np +from tvm import relay +from tvm.contrib import graph_runtime + +def test_dequantize_op(): + + def quantize_test_driver(in_dtype, quant_args, in_data, verify_output_data): + shape = in_data.shape + input_data = relay.var("input_data", shape=shape, dtype=in_dtype) + input_zero_point = quant_args['in_zero_point'] + input_scale = quant_args['in_scale'] + quantized_output = relay.qnn.op.dequantize(input_data, input_zero_point=input_zero_point, + input_scale=input_scale) + mod = relay.Function(relay.analysis.free_vars(quantized_output), quantized_output) + mod = relay.Module.from_expr(mod) + mod = relay.transform.Legalize()(mod) + with relay.build_config(opt_level=3): + graph, lib, params = relay.build(mod, "llvm", params=None) + rt_mod = graph_runtime.create(graph, lib, ctx=tvm.cpu(0)) + rt_mod.set_input(input_data=in_data) + rt_mod.set_input(**params) + rt_mod.run() + res = rt_mod.get_output(0).asnumpy() + np.testing.assert_equal(res, verify_output_data) + assert res.dtype == np.float32 + + def test_uint8_to_float32(): + data = np.array([0, 1, 2, 3, 4, 251, 252, 253, 254, 255]) \ + .astype('uint8') \ + .reshape((2,5)) + output = np.array([-63.5, -63, -62.5, -62, -61.5, 62, 62.5, 63, 63.5, 64]) \ + .astype('float32') \ + .reshape((2,5)) + quant_args = {"in_zero_point":127, "in_scale":0.5} + quantize_test_driver(in_dtype='uint8', quant_args=quant_args, in_data=data, + verify_output_data=output) + + def test_int8_to_float32(): + data = np.array([-128, -127, -126, -125, -124, 123, 124, 125, 126, 127]) \ + .astype('int8') \ + .reshape((2,5)) + output = np.array([-63.5, -63, -62.5, -62, -61.5, 62, 62.5, 63, 63.5, 64]) \ + .astype('float32') \ + .reshape((2,5)) + quant_args = {"in_zero_point":-1, "in_scale":0.5} + quantize_test_driver(in_dtype='int8', quant_args=quant_args, in_data=data, + verify_output_data=output) + + test_uint8_to_float32() + test_int8_to_float32() + + +if __name__ == "__main__": + test_dequantize_op() + diff --git a/tests/python/relay/test_qnn_quantize.py b/tests/python/relay/test_qnn_quantize.py new file mode 100644 index 000000000000..d28460f2f2d1 --- /dev/null +++ b/tests/python/relay/test_qnn_quantize.py @@ -0,0 +1,77 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +import tvm +import numpy as np +from tvm import relay +from tvm.contrib import graph_runtime + +def run_infer_type(expr): + mod = relay.Module.from_expr(expr) + mod = relay.transform.InferType()(mod) + entry = mod["main"] + return entry if isinstance(expr, relay.Function) else entry.body + +def test_quantize_op(): + + def quantize_test_driver(in_dtype, quant_args, out_dtype, in_data, verify_output_data): + shape = in_data.shape + input_data = relay.var("input_data", shape=shape, dtype=in_dtype) + output_zero_point = quant_args['out_zero_point'] + output_scale = quant_args['out_scale'] + quantized_output = relay.qnn.op.quantize(input_data, output_zero_point=output_zero_point, + output_scale=output_scale, out_dtype=out_dtype) + mod = relay.Function(relay.analysis.free_vars(quantized_output), quantized_output) + mod = relay.Module.from_expr(mod) + mod = relay.transform.Legalize()(mod) + with relay.build_config(opt_level=3): + graph, lib, params = relay.build(mod, "llvm", params=None) + rt_mod = graph_runtime.create(graph, lib, ctx=tvm.cpu(0)) + rt_mod.set_input(input_data=in_data) + rt_mod.set_input(**params) + rt_mod.run() + res = rt_mod.get_output(0).asnumpy() + np.testing.assert_equal(res, verify_output_data) + assert res.dtype == out_dtype + + def test_float32_to_uint8(): + data = np.array([-63.5, -63, -62.5, -62, -61.5, 62, 62.5, 63, 63.5, 64]) \ + .astype('float32') \ + .reshape((2,5)) + output = np.array([0, 1, 2, 3, 4, 251, 252, 253, 254, 255]) \ + .astype('uint8') \ + .reshape((2,5)) + quant_args = {"out_zero_point":127, "out_scale":0.5} + quantize_test_driver(in_dtype='float32', quant_args=quant_args, out_dtype='uint8', in_data=data, + verify_output_data=output) + + def test_float32_to_int8(): + data = np.array([-63.5, -63, -62.5, -62, -61.5, 62, 62.5, 63, 63.5, 64]) \ + .astype('float32') \ + .reshape((2,5)) + output = np.array([-128, -127, -126, -125, -124, 123, 124, 125, 126, 127]) \ + .astype('int8') \ + .reshape((2,5)) + quant_args = {"out_zero_point":-1, "out_scale":0.5} + quantize_test_driver(in_dtype='float32', quant_args=quant_args, out_dtype='int8', in_data=data, + verify_output_data=output) + + test_float32_to_uint8() + test_float32_to_int8() + +if __name__ == "__main__": + test_quantize_op() \ No newline at end of file diff --git a/tests/python/relay/test_qnn_requantize.py b/tests/python/relay/test_qnn_requantize.py index cd478fb5ba22..1e6bfa540b69 100644 --- a/tests/python/relay/test_qnn_requantize.py +++ b/tests/python/relay/test_qnn_requantize.py @@ -18,7 +18,6 @@ import tvm import numpy as np from tvm import relay -from tvm.relay.testing import create_workload from tvm.contrib import graph_runtime roundings = ["UPWARD", "TONEAREST"] From 9b86fe61d653f0f79a00109b7df0dd941f6ccb9e Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Mon, 12 Aug 2019 09:47:19 -0700 Subject: [PATCH 2/9] addressing review comments. --- python/tvm/relay/qnn/op/qnn.py | 42 +++++++++++++++-------- src/relay/qnn/op/dequantize.cc | 11 +++--- src/relay/qnn/op/quantize_op.cc | 11 +++--- src/relay/qnn/op/requantize.cc | 2 +- tests/python/relay/test_qnn_dequantize.py | 4 +-- tests/python/relay/test_qnn_quantize.py | 4 +-- 6 files changed, 40 insertions(+), 34 deletions(-) diff --git a/python/tvm/relay/qnn/op/qnn.py b/python/tvm/relay/qnn/op/qnn.py index 4b666bdcc4f8..c4da24b49320 100644 --- a/python/tvm/relay/qnn/op/qnn.py +++ b/python/tvm/relay/qnn/op/qnn.py @@ -74,48 +74,60 @@ def requantize(data, out_dtype) -def quantize(input_data, output_zero_point, output_scale, out_dtype='int8'): +def quantize(input_data, + output_scale, + output_zero_point, + out_dtype='int8'): r""" Quantize op This operator takes float32 as input and produces quantized int8 or unit8 as output. The input tensor can be of any shape. The output shape is the same as input shape. - ..math:: - \mbox{out}[x] = - \mbox{clamp(round(input_tensor/output_scale) + output_zero_point); - out_dtype::min, out_dtype::max} - Parameters + + Q_output = clamp(round(input_tensor/output_scale) + output_zero_point), out_dtype::min, out_dtype::max) + + Parameters ---------- input_data : tvm.relay.Expr The input tensor to be quantized. Can be of type float32. - output_zero_point : + output_zero_point : int The output zero_point. - output_scale: + output_scale : float The output scale. - input_dtype: + input_dtype : str, optional The data type of the input tensor. Can be [int8, uint8] Returns ------- result : tvm.relay.Expr The computed result. """ - return _make.quantize(input_data, output_zero_point, output_scale, out_dtype) + return _make.quantize(input_data, + output_scale, + output_zero_point, + out_dtype) -def dequantize(input_data, input_zero_point, input_scale): + +def dequantize(input_data, + input_scale, + input_zero_point): r""" Dequantize op This operator takes quantized int8 and unit8 as input and produces dequantized float32 as output. The output shape is the same as input shape. The input tensor can be of any shape. - Parameters + + Parameters ---------- input_data : tvm.relay.Expr The input tensor to be dequantized. Can be of type [int8, uint8]. - input_zero_point : + input_zero_point : int The output zero_point. - input_scale: + input_scale : float The output scale. Returns ------- result : tvm.relay.Expr The computed result. """ - return _make.dequantize(input_data, input_zero_point, input_scale) + + return _make.dequantize(input_data, + input_scale, + input_zero_point) diff --git a/src/relay/qnn/op/dequantize.cc b/src/relay/qnn/op/dequantize.cc index 90b4be896a27..c61e213f581b 100644 --- a/src/relay/qnn/op/dequantize.cc +++ b/src/relay/qnn/op/dequantize.cc @@ -25,7 +25,6 @@ */ #include -//#include #include #include #include "../../pass/pattern_util.h" @@ -53,11 +52,13 @@ bool DequantizeRel(const Array& types, } Expr MakeDequantize(Expr data, - int32_t input_zero_point, - double input_scale) { + double input_scale, + int32_t input_zero_point) { auto attrs = make_node(); attrs->input_scale = input_scale; attrs->input_zero_point = input_zero_point; + // real_value = scale * (quantized_value - zero_point) + // A more detailed explanation can be found here - https://github.com/google/gemmlowp/blob/master/doc/quantization.md static const Op& op = Op::Get("qnn.dequantize"); return CallNode::make(op, {data}, Attrs(attrs), {}); } @@ -78,10 +79,6 @@ Expr DequantizeLegalize(const Attrs& attrs, const Array& new_args, CHECK(param != nullptr); CHECK_EQ(arg_types.size(), 1); - auto input_dtype = arg_types[0]; - auto input_tensor_type = input_dtype.as(); - CHECK(input_tensor_type != nullptr) << "Type information missing." - << " Please run infer_type pass."; return DequantizeLower(data, param); } diff --git a/src/relay/qnn/op/quantize_op.cc b/src/relay/qnn/op/quantize_op.cc index 3a69766eba3c..6e527183eee3 100644 --- a/src/relay/qnn/op/quantize_op.cc +++ b/src/relay/qnn/op/quantize_op.cc @@ -25,7 +25,6 @@ */ #include -//#include #include #include #include "../../pass/pattern_util.h" @@ -57,13 +56,14 @@ bool QuantizeRel(const Array& types, } Expr MakeQuantize(Expr data, - int32_t output_zero_point, double output_scale, + int32_t output_zero_point, DataType out_dtype) { auto attrs = make_node(); attrs->output_scale = output_scale; attrs->output_zero_point = output_zero_point; attrs->out_dtype = std::move(out_dtype); + // quantized_output = static const Op& op = Op::Get("qnn.quantize"); return CallNode::make(op, {data}, Attrs(attrs), {}); } @@ -75,7 +75,8 @@ Expr QuantizeLower(const Expr& input_tensor, const QuantizeAttrs* param) { const int32_t min_val = GetQmin(out_dtype); const int32_t max_val = GetQmax(out_dtype); auto scale_data = Cast(Round(Divide(input_tensor, scale)), Int(32)); - // we are trying to do - std::min(std::max(unclamped, min_val), max_val); + // result_quantized_value = result_zero_point + result_real_value / result_scale. + // A more detailed explanation can be found here - https://github.com/google/gemmlowp/blob/master/doc/quantization.md auto add_zero_point = Add(scale_data, output_zero_point); auto clamped_output = Clip(add_zero_point, min_val, max_val); auto clamp_out_dtype = Cast(clamped_output, out_dtype); @@ -90,10 +91,6 @@ Expr QuantizeLegalize(const Attrs& attrs, const Array& new_args, CHECK(param != nullptr); CHECK_EQ(arg_types.size(), 1); - auto input_dtype = arg_types[0]; - auto input_tensor_type = input_dtype.as(); - CHECK(input_tensor_type != nullptr) << "Type information missing." - << " Please run infer_type pass."; return QuantizeLower(data, param); } diff --git a/src/relay/qnn/op/requantize.cc b/src/relay/qnn/op/requantize.cc index 3bab73935589..e3052b71c4ca 100644 --- a/src/relay/qnn/op/requantize.cc +++ b/src/relay/qnn/op/requantize.cc @@ -235,7 +235,7 @@ bool RequantizeRel(const Array& types, int num_inputs, const Attrs& attrs, const RequantizeAttrs* param = attrs.as(); auto out_dtype = param->out_dtype; CHECK(out_dtype == Int(8) || out_dtype == UInt(8) || out_dtype == Int(32)) - << "Output type should be one of [int8, uint8, int32] integer but was " << out_dtype; + << "Output type should be one of [int8, uint8, int32] but was " << out_dtype; reporter->Assign(types[1], TensorTypeNode::make(oshape, out_dtype)); return true; } diff --git a/tests/python/relay/test_qnn_dequantize.py b/tests/python/relay/test_qnn_dequantize.py index 6f20d46f6de0..a942980fdbb2 100644 --- a/tests/python/relay/test_qnn_dequantize.py +++ b/tests/python/relay/test_qnn_dequantize.py @@ -27,8 +27,8 @@ def quantize_test_driver(in_dtype, quant_args, in_data, verify_output_data): input_data = relay.var("input_data", shape=shape, dtype=in_dtype) input_zero_point = quant_args['in_zero_point'] input_scale = quant_args['in_scale'] - quantized_output = relay.qnn.op.dequantize(input_data, input_zero_point=input_zero_point, - input_scale=input_scale) + quantized_output = relay.qnn.op.dequantize(input_data, input_scale=input_scale, + input_zero_point=input_zero_point) mod = relay.Function(relay.analysis.free_vars(quantized_output), quantized_output) mod = relay.Module.from_expr(mod) mod = relay.transform.Legalize()(mod) diff --git a/tests/python/relay/test_qnn_quantize.py b/tests/python/relay/test_qnn_quantize.py index d28460f2f2d1..1d057aca76b5 100644 --- a/tests/python/relay/test_qnn_quantize.py +++ b/tests/python/relay/test_qnn_quantize.py @@ -33,8 +33,8 @@ def quantize_test_driver(in_dtype, quant_args, out_dtype, in_data, verify_output input_data = relay.var("input_data", shape=shape, dtype=in_dtype) output_zero_point = quant_args['out_zero_point'] output_scale = quant_args['out_scale'] - quantized_output = relay.qnn.op.quantize(input_data, output_zero_point=output_zero_point, - output_scale=output_scale, out_dtype=out_dtype) + quantized_output = relay.qnn.op.quantize(input_data, output_scale=output_scale, + output_zero_point=output_zero_point,out_dtype=out_dtype) mod = relay.Function(relay.analysis.free_vars(quantized_output), quantized_output) mod = relay.Module.from_expr(mod) mod = relay.transform.Legalize()(mod) From 323469d7036f4b1e35b19448ffb72a890f1d9300 Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Mon, 12 Aug 2019 10:12:31 -0700 Subject: [PATCH 3/9] addressing review comments. --- python/tvm/relay/qnn/op/qnn.py | 8 ++++---- src/relay/qnn/op/dequantize.cc | 2 +- src/relay/qnn/op/quantize_op.cc | 5 ++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/python/tvm/relay/qnn/op/qnn.py b/python/tvm/relay/qnn/op/qnn.py index c4da24b49320..4c1eb40e7031 100644 --- a/python/tvm/relay/qnn/op/qnn.py +++ b/python/tvm/relay/qnn/op/qnn.py @@ -79,10 +79,10 @@ def quantize(input_data, output_zero_point, out_dtype='int8'): r""" Quantize op - This operator takes float32 as input and produces quantized int8 or unit8 as output. - The input tensor can be of any shape. The output shape is the same as input shape. + This operator takes float32 as input and produces quantized int8 or unit8 as output. + The input tensor can be of any shape. The output shape is the same as input shape. - Q_output = clamp(round(input_tensor/output_scale) + output_zero_point), out_dtype::min, out_dtype::max) + Q_output = clamp(round(input_tensor/output_scale) + output_zero_point), out_dtype::min, out_dtype::max) Parameters ---------- @@ -110,7 +110,7 @@ def dequantize(input_data, input_scale, input_zero_point): r""" Dequantize op - This operator takes quantized int8 and unit8 as input and produces + This operator takes quantized int8 and unit8 as input and produces dequantized float32 as output. The output shape is the same as input shape. The input tensor can be of any shape. diff --git a/src/relay/qnn/op/dequantize.cc b/src/relay/qnn/op/dequantize.cc index c61e213f581b..82cc1ec8e48c 100644 --- a/src/relay/qnn/op/dequantize.cc +++ b/src/relay/qnn/op/dequantize.cc @@ -83,7 +83,7 @@ Expr DequantizeLegalize(const Attrs& attrs, const Array& new_args, } RELAY_REGISTER_OP("qnn.dequantize") - .describe(R"code(Dequantizes the input and produces float32 output. +.describe(R"code(Dequantizes the input and produces float32 output. The input is always quantized (int8, uint8) and will be converted to float32 given input scale and zero_point. - **data**: Quantized tensor of any shape to dequantize. The input data can be of floating point )code" TVM_ADD_FILELINE) diff --git a/src/relay/qnn/op/quantize_op.cc b/src/relay/qnn/op/quantize_op.cc index 6e527183eee3..936abbf62e63 100644 --- a/src/relay/qnn/op/quantize_op.cc +++ b/src/relay/qnn/op/quantize_op.cc @@ -63,7 +63,8 @@ Expr MakeQuantize(Expr data, attrs->output_scale = output_scale; attrs->output_zero_point = output_zero_point; attrs->out_dtype = std::move(out_dtype); - // quantized_output = + // result_quantized_value = result_zero_point + result_real_value / result_scale. + // A more detailed explanation can be found here - https://github.com/google/gemmlowp/blob/master/doc/quantization.md static const Op& op = Op::Get("qnn.quantize"); return CallNode::make(op, {data}, Attrs(attrs), {}); } @@ -75,8 +76,6 @@ Expr QuantizeLower(const Expr& input_tensor, const QuantizeAttrs* param) { const int32_t min_val = GetQmin(out_dtype); const int32_t max_val = GetQmax(out_dtype); auto scale_data = Cast(Round(Divide(input_tensor, scale)), Int(32)); - // result_quantized_value = result_zero_point + result_real_value / result_scale. - // A more detailed explanation can be found here - https://github.com/google/gemmlowp/blob/master/doc/quantization.md auto add_zero_point = Add(scale_data, output_zero_point); auto clamped_output = Clip(add_zero_point, min_val, max_val); auto clamp_out_dtype = Cast(clamped_output, out_dtype); From e25f3aa5e1b494c4584b58a4099d9566847ede06 Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Mon, 12 Aug 2019 10:13:35 -0700 Subject: [PATCH 4/9] Adding new line at the end of the file. --- tests/python/relay/test_qnn_quantize.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/python/relay/test_qnn_quantize.py b/tests/python/relay/test_qnn_quantize.py index 1d057aca76b5..e971f21a7a96 100644 --- a/tests/python/relay/test_qnn_quantize.py +++ b/tests/python/relay/test_qnn_quantize.py @@ -74,4 +74,4 @@ def test_float32_to_int8(): test_float32_to_int8() if __name__ == "__main__": - test_quantize_op() \ No newline at end of file + test_quantize_op() From 025ac9224afa3d3163292d0d50133d40a2036fd3 Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Tue, 13 Aug 2019 17:22:01 -0700 Subject: [PATCH 5/9] Adhering to styling guidelines. --- python/tvm/relay/qnn/op/qnn.py | 14 +++++------ src/relay/qnn/op/dequantize.cc | 19 ++++++++------- .../qnn/op/{quantize_op.cc => quantize.cc} | 24 ++++++++++--------- 3 files changed, 30 insertions(+), 27 deletions(-) rename src/relay/qnn/op/{quantize_op.cc => quantize.cc} (85%) diff --git a/python/tvm/relay/qnn/op/qnn.py b/python/tvm/relay/qnn/op/qnn.py index 4c1eb40e7031..2f6cd7ff3286 100644 --- a/python/tvm/relay/qnn/op/qnn.py +++ b/python/tvm/relay/qnn/op/qnn.py @@ -74,7 +74,7 @@ def requantize(data, out_dtype) -def quantize(input_data, +def quantize(data, output_scale, output_zero_point, out_dtype='int8'): @@ -82,11 +82,11 @@ def quantize(input_data, This operator takes float32 as input and produces quantized int8 or unit8 as output. The input tensor can be of any shape. The output shape is the same as input shape. - Q_output = clamp(round(input_tensor/output_scale) + output_zero_point), out_dtype::min, out_dtype::max) + Q_output = clamp((round(input_tensor/output_scale) + output_zero_point), out_dtype::min, out_dtype::max) Parameters ---------- - input_data : tvm.relay.Expr + data : tvm.relay.Expr The input tensor to be quantized. Can be of type float32. output_zero_point : int The output zero_point. @@ -100,13 +100,13 @@ def quantize(input_data, The computed result. """ - return _make.quantize(input_data, + return _make.quantize(data, output_scale, output_zero_point, out_dtype) -def dequantize(input_data, +def dequantize(data, input_scale, input_zero_point): r""" Dequantize op @@ -116,7 +116,7 @@ def dequantize(input_data, Parameters ---------- - input_data : tvm.relay.Expr + data : tvm.relay.Expr The input tensor to be dequantized. Can be of type [int8, uint8]. input_zero_point : int The output zero_point. @@ -128,6 +128,6 @@ def dequantize(input_data, The computed result. """ - return _make.dequantize(input_data, + return _make.dequantize(data, input_scale, input_zero_point) diff --git a/src/relay/qnn/op/dequantize.cc b/src/relay/qnn/op/dequantize.cc index 82cc1ec8e48c..1e5944014934 100644 --- a/src/relay/qnn/op/dequantize.cc +++ b/src/relay/qnn/op/dequantize.cc @@ -63,23 +63,24 @@ Expr MakeDequantize(Expr data, return CallNode::make(op, {data}, Attrs(attrs), {}); } -Expr DequantizeLower(const Expr& input_tensor, const DequantizeAttrs* param) { - const auto input_zero_point = MakeConstantScalar(Int(32), param->input_zero_point); - const auto input_scale = MakeConstantScalar(Float(32), param->input_scale); +Expr DequantizeLower(const Expr& input_tensor, + const DequantizeAttrs* attrs) { + const auto input_zero_point = MakeConstantScalar(Int(32), attrs->input_zero_point); + const auto input_scale = MakeConstantScalar(Float(32), attrs->input_scale); auto shift = Subtract(Cast(input_tensor, Int(32)), input_zero_point); auto scaled_output = Multiply(Cast(shift, Float(32)), input_scale); return scaled_output; } -Expr DequantizeLegalize(const Attrs& attrs, const Array& new_args, - const Array& arg_types) { +Expr DequantizeLegalize(const Attrs& attrs, + const Array& new_args, + const Array& arg_types) { CHECK_EQ(new_args.size(), 1); auto& data = new_args[0]; - const auto* param = attrs.as(); - CHECK(param != nullptr); - + const auto* dequantize_attrs = attrs.as(); + CHECK(dequantize_attrs != nullptr); CHECK_EQ(arg_types.size(), 1); - return DequantizeLower(data, param); + return DequantizeLower(data, dequantize_attrs); } RELAY_REGISTER_OP("qnn.dequantize") diff --git a/src/relay/qnn/op/quantize_op.cc b/src/relay/qnn/op/quantize.cc similarity index 85% rename from src/relay/qnn/op/quantize_op.cc rename to src/relay/qnn/op/quantize.cc index 936abbf62e63..bc655ea5f82d 100644 --- a/src/relay/qnn/op/quantize_op.cc +++ b/src/relay/qnn/op/quantize.cc @@ -45,9 +45,9 @@ bool QuantizeRel(const Array& types, const auto input_dtype = data->dtype; CHECK(input_dtype == Float(32)) << "Input type should be one of float32 but was " << input_dtype; - const auto* param = attrs.as(); + const auto* quantize_attrs = attrs.as(); const Array oshape = data->shape; - const DataType out_dtype = param->out_dtype; + const DataType out_dtype = quantize_attrs->out_dtype; CHECK(out_dtype == Int(8) || out_dtype == UInt(8)) << "Output type should be one of [int8, unit8 ] but was " << out_dtype; // assign output type @@ -69,10 +69,11 @@ Expr MakeQuantize(Expr data, return CallNode::make(op, {data}, Attrs(attrs), {}); } -Expr QuantizeLower(const Expr& input_tensor, const QuantizeAttrs* param) { - const auto out_dtype = param->out_dtype; - const auto output_zero_point = MakeConstantScalar(Int(32), param->output_zero_point); - const auto scale = MakeConstantScalar(Float(32), param->output_scale); +Expr QuantizeLower(const Expr& input_tensor, + const QuantizeAttrs* attrs) { + const auto out_dtype = attrs->out_dtype; + const auto output_zero_point = MakeConstantScalar(Int(32), attrs->output_zero_point); + const auto scale = MakeConstantScalar(Float(32), attrs->output_scale); const int32_t min_val = GetQmin(out_dtype); const int32_t max_val = GetQmax(out_dtype); auto scale_data = Cast(Round(Divide(input_tensor, scale)), Int(32)); @@ -82,15 +83,16 @@ Expr QuantizeLower(const Expr& input_tensor, const QuantizeAttrs* param) { return clamp_out_dtype; } -Expr QuantizeLegalize(const Attrs& attrs, const Array& new_args, - const Array& arg_types) { +Expr QuantizeLegalize(const Attrs& attrs, + const Array& new_args, + const Array& arg_types) { CHECK_EQ(new_args.size(), 1); auto& data = new_args[0]; - const auto* param = attrs.as(); - CHECK(param != nullptr); + const auto* quantize_attrs = attrs.as(); + CHECK(quantize_attrs != nullptr); CHECK_EQ(arg_types.size(), 1); - return QuantizeLower(data, param); + return QuantizeLower(data, quantize_attrs); } RELAY_REGISTER_OP("qnn.quantize") From 2ce11bec75e63eb155b08424382bb34489bf775c Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Tue, 13 Aug 2019 17:27:25 -0700 Subject: [PATCH 6/9] Adding name to contributors. --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 28087bd45fdc..af977448f470 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -111,3 +111,4 @@ We do encourage everyone to work anything they are interested in. - [Haolong Zhang](https://github.com/haolongzhangm) - [Cody Hao Yu](https://github.com/comaniac) - [Chris Nuernberger](https://github.com/cnuernber) +- [Shoubhik Bhattacharya](https://github.com/shoubhik) From cc43d7e4c0b631fb5ce5d3e78721ed4dc44921dc Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Wed, 14 Aug 2019 15:37:34 -0700 Subject: [PATCH 7/9] Fixing lint issue. --- python/tvm/relay/qnn/op/qnn.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/tvm/relay/qnn/op/qnn.py b/python/tvm/relay/qnn/op/qnn.py index 2f6cd7ff3286..8c4cb34875eb 100644 --- a/python/tvm/relay/qnn/op/qnn.py +++ b/python/tvm/relay/qnn/op/qnn.py @@ -82,7 +82,9 @@ def quantize(data, This operator takes float32 as input and produces quantized int8 or unit8 as output. The input tensor can be of any shape. The output shape is the same as input shape. - Q_output = clamp((round(input_tensor/output_scale) + output_zero_point), out_dtype::min, out_dtype::max) + Q_output = clamp((round(input_tensor/output_scale) + output_zero_point), + out_dtype::min, + out_dtype::max) Parameters ---------- From c8585cb4fa4e24fc4970742eed49a08607806d62 Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Wed, 14 Aug 2019 15:51:35 -0700 Subject: [PATCH 8/9] Fixing file name. --- src/relay/qnn/op/quantize.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/relay/qnn/op/quantize.cc b/src/relay/qnn/op/quantize.cc index bc655ea5f82d..2f494008cc46 100644 --- a/src/relay/qnn/op/quantize.cc +++ b/src/relay/qnn/op/quantize.cc @@ -19,7 +19,7 @@ /*! * Copyright (c) 2019 by Contributors - * \file src/relay/qnn/op/quantize_op.cc + * \file src/relay/qnn/op/quantize.cc * \brief QNN dequantize operator. Dequantize operator converts from quantized * domain to unquantized domain. */ From 6d62dc5aa72028ddbce1aca5e3f6f0f5342542c9 Mon Sep 17 00:00:00 2001 From: "shoubhikbhatti@gmail.com" Date: Thu, 15 Aug 2019 17:53:01 -0700 Subject: [PATCH 9/9] Removing unnecessary code. --- tests/python/relay/test_qnn_quantize.py | 6 ------ tests/python/relay/test_qnn_requantize.py | 7 ------- 2 files changed, 13 deletions(-) diff --git a/tests/python/relay/test_qnn_quantize.py b/tests/python/relay/test_qnn_quantize.py index e971f21a7a96..47808cfcedba 100644 --- a/tests/python/relay/test_qnn_quantize.py +++ b/tests/python/relay/test_qnn_quantize.py @@ -20,12 +20,6 @@ from tvm import relay from tvm.contrib import graph_runtime -def run_infer_type(expr): - mod = relay.Module.from_expr(expr) - mod = relay.transform.InferType()(mod) - entry = mod["main"] - return entry if isinstance(expr, relay.Function) else entry.body - def test_quantize_op(): def quantize_test_driver(in_dtype, quant_args, out_dtype, in_data, verify_output_data): diff --git a/tests/python/relay/test_qnn_requantize.py b/tests/python/relay/test_qnn_requantize.py index 1e6bfa540b69..61f42893f81f 100644 --- a/tests/python/relay/test_qnn_requantize.py +++ b/tests/python/relay/test_qnn_requantize.py @@ -22,13 +22,6 @@ roundings = ["UPWARD", "TONEAREST"] -def run_infer_type(expr): - mod = relay.Module.from_expr(expr) - mod = relay.transform.InferType()(mod) - entry = mod["main"] - return entry if isinstance(expr, relay.Function) else entry.body - - def test_requantize(): def verify(mod, goldens): with relay.build_config(opt_level=3):