maekawatoshiki commented on code in PR #14536: URL: https://github.com/apache/tvm/pull/14536#discussion_r1162182323
########## src/relay/qnn/op/softmax.cc: ########## @@ -0,0 +1,144 @@ +/* + * 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. + */ + +/*! + * \file src/relay/qnn/op/softmax.cc + * \brief QNN softmax operator. + */ +#include <tvm/relay/analysis.h> +#include <tvm/relay/op_attr_types.h> + +#include "op_common.h" +#include "tvm/ir/expr.h" +#include "tvm/relay/attrs/nn.h" +#include "tvm/relay/type.h" +#include "tvm/runtime/data_type.h" +#include "tvm/runtime/logging.h" +#include "tvm/topi/reduction.h" + +namespace tvm { +namespace relay { +namespace qnn { + +bool QnnSoftmaxRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, + const TypeReporter& reporter) { + // Expected Types: input, scale, zero_point, output_scale, output_zero_point, output + ICHECK_EQ(types.size(), 6); + const auto* x = types[0].as<TensorTypeNode>(); + if (x == nullptr) return false; + ICHECK(x->dtype == DataType::Int(8)) + << "Expected quantized softmax type(int8) for input but was " << x->dtype; + + // Check the types of scale and zero points. + for (size_t i = 1; i < 5; ++i) { + if (types[i].as<IncompleteTypeNode>()) { + return false; + } + } + + ICHECK(IsScalarType(types[1], DataType::Float(32))); // scale + ICHECK(IsScalarType(types[2], DataType::Int(32))); // zero_point + ICHECK(IsScalarType(types[3], DataType::Float(32))); // scale + ICHECK(IsScalarType(types[4], DataType::Int(32))); // zero_point + + // Assign types for scale and zero points. + reporter->Assign(types[1], TensorType({}, DataType::Float(32))); // scale + reporter->Assign(types[2], TensorType({}, DataType::Int(32))); // zero_point + reporter->Assign(types[3], TensorType({}, DataType::Float(32))); // scale + reporter->Assign(types[4], TensorType({}, DataType::Int(32))); // zero_point + + // Collect the input tensor and output tensor devoid of scale and zero points to reuse Relay + // IdentityRel infer type function. + Array<Type> tensor_types = {types[0], types[5]}; + return IdentityRel(tensor_types, 2, attrs, reporter); +} + +// Positional relay function to create quantized softmax operator used by frontend FFI. +Expr MakeQuantizedSoftmax(Expr x, int axis, Expr scale, Expr zero_point, Expr output_scale, + Expr output_zero_point) { + auto attrs = make_object<SoftmaxAttrs>(); + attrs->axis = axis; + static const Op& op = Op::Get("qnn.softmax"); + return Call(op, {x, scale, zero_point, output_scale, output_zero_point}, Attrs(attrs), {}); +} + +/* + * \brief Canonicalizes the QNN softmax op. + */ +Expr QnnSoftmaxCanonicalize(const Attrs& attrs, const Array<Expr>& new_args, + const Array<tvm::relay::Type>& arg_types) { + // Expected: input, scale, zero_point, output_scale, output_zero_point + ICHECK_EQ(new_args.size(), 5); + + const Expr input_scale = new_args[1]; + const Expr input_zero_point = new_args[2]; + const Expr output_scale = new_args[3]; + const Expr output_zero_point = new_args[4]; + const int axis = attrs.as<SoftmaxAttrs>()->axis; + + // Refer to the Algorithm 1 in https://arxiv.org/pdf/2207.01405.pdf + + const Expr quantized_data = + Subtract(Cast(new_args[0], DataType::Int(32)), Cast(input_zero_point, DataType::Int(32))); + + const Expr x_0 = ConvertDtype( + Round(Divide(MakeConstantScalar(DataType::Float(32), 1.f), input_scale)), DataType::Int(32)); + const Expr max = Max(quantized_data, {axis}, true, false); + const Expr x = Subtract(quantized_data, max); + + const auto const_i32 = [&](int32_t val) { return MakeConstantScalar(DataType::Int(32), val); }; + const int n = 8; + const int m = 30; + const int bits = 8; + const Expr x_p = Subtract(Add(x, RightShift(x, const_i32(1))), RightShift(x, const_i32(4))); + const Expr q = Divide(Negative(x_p), x_0); Review Comment: `x_p` is the same as `I_p` in the Algorithm 1 from the paper. And here: https://github.com/apache/tvm/blob/35b554875943de8fca899a3b86faae448f53ebf0/src/relay/qnn/op/softmax.cc#L102-L103 we subtract the max value in the axis. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
