From 4a4beecf78959859fb8dc37199e062a31e6faf29 Mon Sep 17 00:00:00 2001 From: Animesh Jain Date: Thu, 11 Jul 2019 19:05:03 +0000 Subject: [PATCH] Pushing files that I forgot earlier. Changing util location. --- python/tvm/relay/qnn/_qnn.py | 22 ++++++++ python/tvm/relay/qnn/op/__init__.py | 20 +++++++ python/tvm/relay/qnn/op/_make.py | 20 +++++++ python/tvm/relay/qnn/op/qnn.py | 74 ++++++++++++++++++++++++++ src/relay/qnn/op/requantize.cc | 2 +- src/relay/qnn/pass/quantize_rewrite.cc | 2 +- src/relay/qnn/{include => }/util.h | 0 7 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 python/tvm/relay/qnn/_qnn.py create mode 100644 python/tvm/relay/qnn/op/__init__.py create mode 100644 python/tvm/relay/qnn/op/_make.py create mode 100644 python/tvm/relay/qnn/op/qnn.py rename src/relay/qnn/{include => }/util.h (100%) diff --git a/python/tvm/relay/qnn/_qnn.py b/python/tvm/relay/qnn/_qnn.py new file mode 100644 index 000000000000..bd3cdbb976d6 --- /dev/null +++ b/python/tvm/relay/qnn/_qnn.py @@ -0,0 +1,22 @@ +# 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. +#pylint: disable=unused-argument +"""Internal module for quantization.""" +from __future__ import absolute_import +from tvm._ffi.function import _init_api + +_init_api("relay._qnn", __name__) diff --git a/python/tvm/relay/qnn/op/__init__.py b/python/tvm/relay/qnn/op/__init__.py new file mode 100644 index 000000000000..e9adfa783f93 --- /dev/null +++ b/python/tvm/relay/qnn/op/__init__.py @@ -0,0 +1,20 @@ +# 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. +# pylint: disable=wildcard-import +"""Neural network related operators.""" +from __future__ import absolute_import as _abs +from .qnn import * diff --git a/python/tvm/relay/qnn/op/_make.py b/python/tvm/relay/qnn/op/_make.py new file mode 100644 index 000000000000..b1695629b8f9 --- /dev/null +++ b/python/tvm/relay/qnn/op/_make.py @@ -0,0 +1,20 @@ +# 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. +"""Constructor APIs""" +from ...._ffi.function import _init_api + +_init_api("relay.op.qnn._make", __name__) diff --git a/python/tvm/relay/qnn/op/qnn.py b/python/tvm/relay/qnn/op/qnn.py new file mode 100644 index 000000000000..8db431eebe23 --- /dev/null +++ b/python/tvm/relay/qnn/op/qnn.py @@ -0,0 +1,74 @@ +# 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. +#pylint: disable=invalid-name, too-many-lines +"""Neural network operations.""" +from __future__ import absolute_import as _abs +from . import _make + +def requantize(input_data, input_zero_point, input_scale, output_zero_point, + output_scale, out_dtype="int32", use_int_compute=True, + rounding_mode="FE_AWAY_FROM_ZERO"): + r"""Requantized operator. + + The requantize operator converts one quantized tensor to another quantized + tensor. For the output tensor, we are provided with output scale and zero + point. The computation looks like this + + Q_output = zp_output + (scale_input)/(scale_ouptut) * (Q_input - zp_input) + + The above computation can be done in floating point as the scales are in + FP32. Alternatively, we can approximate floating point with fixed point + computation. This is controlled by use_int_compute. + + Parameters + ---------- + quantized_data : tvm.relay.Expr + The input quantized_data to the operator. + + input_scale: float + The float scalar to scale the quantized_data int8 values back to FP32. + + output_scale: float + The float scalar to scale the quantized_output int8 values back to FP32. + + input_zero_point: int + The zero point of the quantized_data distribution. + + output_zero_point: int + The zero point of the quantized_output distribution. + + out_dtype : str, optional + Specifies the output quantized_data type for mixed precision conv2d. + + use_int_compute : bool, optional + Use fully integer computation for requantizing. + + rounding_mode : string, optional + Defines the rounding direction when the value is midway between two + representable values. + + Returns + ------- + result : tvm.relay.Expr + The computed result. + """ + assert rounding_mode in ("FE_UPWARD", "FE_AWAY_FROM_ZERO"),\ + "Unsupported rounding mode" + + return _make.requantize(input_data, input_zero_point, input_scale, + output_zero_point, output_scale, out_dtype, + use_int_compute, rounding_mode) diff --git a/src/relay/qnn/op/requantize.cc b/src/relay/qnn/op/requantize.cc index c389e82fba80..df4a224fc2ba 100644 --- a/src/relay/qnn/op/requantize.cc +++ b/src/relay/qnn/op/requantize.cc @@ -26,7 +26,7 @@ #include #include #include -#include "../include/util.h" +#include "../util.h" namespace tvm { namespace relay { diff --git a/src/relay/qnn/pass/quantize_rewrite.cc b/src/relay/qnn/pass/quantize_rewrite.cc index 5d4942c80a7c..7d4e0f017050 100644 --- a/src/relay/qnn/pass/quantize_rewrite.cc +++ b/src/relay/qnn/pass/quantize_rewrite.cc @@ -27,7 +27,7 @@ #include #include #include -#include "../include/util.h" +#include "../util.h" #include "../../pass/pattern_util.h" namespace tvm { diff --git a/src/relay/qnn/include/util.h b/src/relay/qnn/util.h similarity index 100% rename from src/relay/qnn/include/util.h rename to src/relay/qnn/util.h