Skip to content

Commit

Permalink
Pushing files that I forgot earlier. Changing util location.
Browse files Browse the repository at this point in the history
  • Loading branch information
anijain2305 committed Jul 15, 2019
1 parent 21168ae commit 4a4beec
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 2 deletions.
22 changes: 22 additions & 0 deletions python/tvm/relay/qnn/_qnn.py
Original file line number Diff line number Diff line change
@@ -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__)
20 changes: 20 additions & 0 deletions python/tvm/relay/qnn/op/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
20 changes: 20 additions & 0 deletions python/tvm/relay/qnn/op/_make.py
Original file line number Diff line number Diff line change
@@ -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__)
74 changes: 74 additions & 0 deletions python/tvm/relay/qnn/op/qnn.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion src/relay/qnn/op/requantize.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <tvm/relay/op_attr_types.h>
#include <tvm/relay/analysis.h>
#include <tvm/relay/qnn/attrs.h>
#include "../include/util.h"
#include "../util.h"

namespace tvm {
namespace relay {
Expand Down
2 changes: 1 addition & 1 deletion src/relay/qnn/pass/quantize_rewrite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <tvm/relay/transform.h>
#include <tvm/relay/op_attr_types.h>
#include <tvm/relay/qnn/attrs.h>
#include "../include/util.h"
#include "../util.h"
#include "../../pass/pattern_util.h"

namespace tvm {
Expand Down
File renamed without changes.

0 comments on commit 4a4beec

Please sign in to comment.