|
| 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 | + * \file src/relay/qnn/op/rsqrt.cc |
| 22 | + * \brief QNN rsqrt operator. |
| 23 | + */ |
| 24 | +#include <tvm/relay/analysis.h> |
| 25 | +#include <tvm/relay/op_attr_types.h> |
| 26 | + |
| 27 | +#include "op_common.h" |
| 28 | + |
| 29 | +namespace tvm { |
| 30 | +namespace relay { |
| 31 | +namespace qnn { |
| 32 | + |
| 33 | +bool QnnRsqrtRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, |
| 34 | + const TypeReporter& reporter) { |
| 35 | + // Expected Types: data, scale, zero_point, output_scale, output_zero_point |
| 36 | + ICHECK_EQ(types.size(), 6); |
| 37 | + const auto* x = types[0].as<TensorTypeNode>(); |
| 38 | + if (x == nullptr) return false; |
| 39 | + ICHECK(x->dtype == DataType::Int(8) || x->dtype == DataType::UInt(8)) |
| 40 | + << "Expected quantized rsqrt type(int8, uint8) for input but was " << x->dtype; |
| 41 | + |
| 42 | + // Check the types of scale and zero points. |
| 43 | + for (size_t i = 1; i < 5; ++i) { |
| 44 | + if (types[i].as<IncompleteTypeNode>()) { |
| 45 | + return false; |
| 46 | + } |
| 47 | + } |
| 48 | + ICHECK(IsScalarType(types[1], DataType::Float(32))); // scale |
| 49 | + ICHECK(IsScalarType(types[2], DataType::Int(32))); // zero_point |
| 50 | + ICHECK(IsScalarType(types[3], DataType::Float(32))); // output_scale |
| 51 | + ICHECK(IsScalarType(types[4], DataType::Int(32))); // output_zero_point |
| 52 | + |
| 53 | + // Assign types for scale and zero points. |
| 54 | + reporter->Assign(types[1], TensorType({}, DataType::Float(32))); // scale |
| 55 | + reporter->Assign(types[2], TensorType({}, DataType::Int(32))); // zero_point |
| 56 | + reporter->Assign(types[3], TensorType({}, DataType::Float(32))); // output_scale |
| 57 | + reporter->Assign(types[4], TensorType({}, DataType::Int(32))); // output_zero_point |
| 58 | + |
| 59 | + // Collect the input tensor and output tensor devoid of scale and zero points to reuse Relay |
| 60 | + // IdentityRel infer type function. |
| 61 | + Array<Type> tensor_types = {types[0], types[5]}; |
| 62 | + return IdentityRel(tensor_types, 2, attrs, reporter); |
| 63 | +} |
| 64 | + |
| 65 | +// Positional relay function to create quantized rsqrt operator used by frontend FFI. |
| 66 | +Expr MakeQuantizedRsqrt(Expr x, Expr scale, Expr zero_point, Expr output_scale, |
| 67 | + Expr output_zero_point) { |
| 68 | + static const Op& op = Op::Get("qnn.rsqrt"); |
| 69 | + return Call(op, {x, scale, zero_point, output_scale, output_zero_point}, Attrs(), {}); |
| 70 | +} |
| 71 | + |
| 72 | +/* |
| 73 | + * \brief Canonicalizes the QNN rsqrt op. |
| 74 | + * \param attrs The empty attribute. |
| 75 | + * \param new_args The new mutated args to the call node. |
| 76 | + * \param arg_types The types of input and output. |
| 77 | + * \return The sequence of Relay ops for add op. |
| 78 | + */ |
| 79 | +Expr QnnRsqrtCanonicalize(const Attrs& attrs, const Array<Expr>& new_args, |
| 80 | + const Array<tvm::relay::Type>& arg_types) { |
| 81 | + // Get the args. |
| 82 | + QnnUnaryOpArguments args(new_args); |
| 83 | + |
| 84 | + // Get the input dtype and shape. |
| 85 | + QnnUnaryOpTensorType input_type(arg_types, 0); |
| 86 | + |
| 87 | + // Get the types for dequantize/quantize. |
| 88 | + Array<tvm::relay::Type> types; |
| 89 | + for (size_t i = 1; i < 5; ++i) { |
| 90 | + types.push_back(arg_types[i]); |
| 91 | + } |
| 92 | + |
| 93 | + // Dequantize input. |
| 94 | + auto dequantized_arg = Dequantize(args.x, args.scale, args.zero_point, types, -1); |
| 95 | + |
| 96 | + // Compute Rsqrt(Q_x') |
| 97 | + auto output = Rsqrt(dequantized_arg); |
| 98 | + |
| 99 | + // Quantize output. |
| 100 | + return Quantize(output, args.output_scale, args.output_zero_point, input_type.dtype, types, -1); |
| 101 | +} |
| 102 | + |
| 103 | +RELAY_REGISTER_OP("qnn.rsqrt") |
| 104 | + .describe("Elementwise rsqrt for quantized tensors.") |
| 105 | + .set_num_inputs(5) |
| 106 | + .add_argument("data", "Quantized Tensor", "The input data.") |
| 107 | + .add_argument("scale", "Tensor", "The quantization scale of the input tensor.") |
| 108 | + .add_argument("zero_point", "Tensor", "The quantization zero_point of the input tensor.") |
| 109 | + .add_argument("output_scale", "Tensor", "The quantization scale of the output tensor.") |
| 110 | + .add_argument("output_zero_point", "Tensor", |
| 111 | + "The quantization zero_point of the output tensor.") |
| 112 | + .set_support_level(11) |
| 113 | + .add_type_rel("QRsqrt", QnnRsqrtRel) |
| 114 | + .set_attr<TNonComputational>("TNonComputational", true) |
| 115 | + .set_attr<FTVMLegalize>("FTVMQnnCanonicalize", QnnRsqrtCanonicalize); |
| 116 | + |
| 117 | +TVM_REGISTER_GLOBAL("relay.qnn.op._make.rsqrt").set_body_typed(MakeQuantizedRsqrt); |
| 118 | + |
| 119 | +} // namespace qnn |
| 120 | +} // namespace relay |
| 121 | +} // namespace tvm |
0 commit comments