|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * Copyright (c) 2019 by Contributors |
| 22 | + * \file src/relay/qnn/op/quantize.cc |
| 23 | + * \brief QNN dequantize operator. Dequantize operator converts from quantized |
| 24 | + * domain to unquantized domain. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <tvm/relay/analysis.h> |
| 28 | +#include <tvm/relay/op_attr_types.h> |
| 29 | +#include <tvm/relay/qnn/attrs.h> |
| 30 | +#include "../../pass/pattern_util.h" |
| 31 | +#include "../util.h" |
| 32 | + |
| 33 | +namespace tvm { |
| 34 | +namespace relay { |
| 35 | +namespace qnn { |
| 36 | + |
| 37 | +TVM_REGISTER_NODE_TYPE(QuantizeAttrs); |
| 38 | + |
| 39 | +bool QuantizeRel(const Array<Type>& types, |
| 40 | + int num_inputs, |
| 41 | + const Attrs& attrs, |
| 42 | + const TypeReporter& reporter) { |
| 43 | + CHECK_EQ(types.size(), 2); |
| 44 | + const auto* data = types[0].as<TensorTypeNode>(); |
| 45 | + const auto input_dtype = data->dtype; |
| 46 | + CHECK(input_dtype == Float(32)) |
| 47 | + << "Input type should be one of float32 but was " << input_dtype; |
| 48 | + const auto* quantize_attrs = attrs.as<QuantizeAttrs>(); |
| 49 | + const Array<tvm::Expr> oshape = data->shape; |
| 50 | + const DataType out_dtype = quantize_attrs->out_dtype; |
| 51 | + CHECK(out_dtype == Int(8) || out_dtype == UInt(8)) |
| 52 | + << "Output type should be one of [int8, unit8 ] but was " << out_dtype; |
| 53 | + // assign output type |
| 54 | + reporter->Assign(types[1], TensorTypeNode::make(oshape, out_dtype)); |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +Expr MakeQuantize(Expr data, |
| 59 | + double output_scale, |
| 60 | + int32_t output_zero_point, |
| 61 | + DataType out_dtype) { |
| 62 | + auto attrs = make_node<QuantizeAttrs>(); |
| 63 | + attrs->output_scale = output_scale; |
| 64 | + attrs->output_zero_point = output_zero_point; |
| 65 | + attrs->out_dtype = std::move(out_dtype); |
| 66 | + // result_quantized_value = result_zero_point + result_real_value / result_scale. |
| 67 | + // A more detailed explanation can be found here - https://github.com/google/gemmlowp/blob/master/doc/quantization.md |
| 68 | + static const Op& op = Op::Get("qnn.quantize"); |
| 69 | + return CallNode::make(op, {data}, Attrs(attrs), {}); |
| 70 | +} |
| 71 | + |
| 72 | +Expr QuantizeLower(const Expr& input_tensor, |
| 73 | + const QuantizeAttrs* attrs) { |
| 74 | + const auto out_dtype = attrs->out_dtype; |
| 75 | + const auto output_zero_point = MakeConstantScalar(Int(32), attrs->output_zero_point); |
| 76 | + const auto scale = MakeConstantScalar(Float(32), attrs->output_scale); |
| 77 | + const int32_t min_val = GetQmin(out_dtype); |
| 78 | + const int32_t max_val = GetQmax(out_dtype); |
| 79 | + auto scale_data = Cast(Round(Divide(input_tensor, scale)), Int(32)); |
| 80 | + auto add_zero_point = Add(scale_data, output_zero_point); |
| 81 | + auto clamped_output = Clip(add_zero_point, min_val, max_val); |
| 82 | + auto clamp_out_dtype = Cast(clamped_output, out_dtype); |
| 83 | + return clamp_out_dtype; |
| 84 | +} |
| 85 | + |
| 86 | +Expr QuantizeLegalize(const Attrs& attrs, |
| 87 | + const Array<Expr>& new_args, |
| 88 | + const Array<tvm::relay::Type>& arg_types) { |
| 89 | + CHECK_EQ(new_args.size(), 1); |
| 90 | + auto& data = new_args[0]; |
| 91 | + const auto* quantize_attrs = attrs.as<QuantizeAttrs>(); |
| 92 | + CHECK(quantize_attrs != nullptr); |
| 93 | + |
| 94 | + CHECK_EQ(arg_types.size(), 1); |
| 95 | + return QuantizeLower(data, quantize_attrs); |
| 96 | +} |
| 97 | + |
| 98 | +RELAY_REGISTER_OP("qnn.quantize") |
| 99 | +.describe(R"code(Quantizes the input and produces quantized output. |
| 100 | +The input can be either float or quantized(int8, unit8). If the input is float, |
| 101 | +this op takes scale and zero point and quantize the float value to |
| 102 | +quantized output, in int8 or uint8 format. If the input is quantized value, |
| 103 | +the op requantize the input (of a certain type, with a given scale and zero |
| 104 | +point) to the output of the same or different type with a same or different |
| 105 | +scale and zero point. |
| 106 | +- **data**: Tensor of any shape to quantize. The input data can be of floating point |
| 107 | + or quantized. |
| 108 | +)code" TVM_ADD_FILELINE) |
| 109 | +.set_attrs_type_key("relay.attrs.QuantizeAttrs") |
| 110 | +.set_num_inputs(1) |
| 111 | +.add_argument("data", "Tensor", "The tensor to quantize.") |
| 112 | +.set_support_level(11) |
| 113 | +.add_type_rel("Quantize", QuantizeRel) |
| 114 | +.set_attr<FTVMLegalize>("FTVMLegalize", QuantizeLegalize); |
| 115 | + |
| 116 | +TVM_REGISTER_API("relay.qnn.op._make.quantize") |
| 117 | +.set_body_typed(MakeQuantize); |
| 118 | + |
| 119 | +} // namespace qnn |
| 120 | +} // namespace relay |
| 121 | +} // namespace tvm |
0 commit comments