ibsidorenko commented on code in PR #15849: URL: https://github.com/apache/tvm/pull/15849#discussion_r1344299797
########## src/relax/op/tensor/qdq.cc: ########## @@ -0,0 +1,201 @@ +/* + * 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/relax/op/tensor/qdq.cc + * \brief implements quantize/dequantize operators. + */ + +#include "qdq.h" + +#include <utility> + +#include "../../transform/utils.h" +#include "../op_common.h" + +namespace tvm { +namespace relax { + +TVM_REGISTER_NODE_TYPE(QuantizeAttrs); + +/* relax.quantize */ + +Expr quantize(Expr data, Expr scale, Expr zero_point, int axis, DataType out_dtype) { + ObjectPtr<QuantizeAttrs> attrs = make_object<QuantizeAttrs>(); + attrs->axis = axis; + attrs->out_dtype = out_dtype; + static const Op& op = Op::Get("relax.quantize"); + return Call(op, {std::move(data), std::move(scale), std::move(zero_point)}, Attrs(attrs)); +} + +TVM_REGISTER_GLOBAL("relax.op.quantize").set_body_typed(quantize); + +StructInfo InferStructInfoQuantize(const Call& call, const BlockBuilder& ctx) { + const auto* attrs = call->attrs.as<QuantizeAttrs>(); + if (attrs->out_dtype != DataType::Int(8) && attrs->out_dtype != DataType::UInt(8) && + attrs->out_dtype != DataType::Int(16) && attrs->out_dtype != DataType::UInt(16)) { + ctx->ReportFatal(Diagnostic::Error(call) + << "Unsupported output datatype attribute for operation: '" + << attrs->out_dtype); + } + + TensorStructInfo input_sinfo = GetInputTensorStructInfo(call, ctx)[0]; + TensorStructInfo scale_sinfo = GetInputTensorStructInfo(call, ctx)[1]; + TensorStructInfo zp_sinfo = GetInputTensorStructInfo(call, ctx)[2]; + + // Check input datatype: + if (input_sinfo->dtype != DataType::Float(16) && input_sinfo->dtype != DataType::Float(32)) { + ctx->ReportFatal(Diagnostic::Error(call) + << "Unsupported input datatype for operation: " << input_sinfo->dtype); + } + + // Check datatype of scale param: + if (scale_sinfo->dtype != DataType::Float(32) && scale_sinfo->dtype != DataType::Float(16)) { + ctx->ReportFatal(Diagnostic::Error(call) + << "scale param datatype should be one of [float16, float32], but got " + << scale_sinfo->dtype); + } + + // Check datatype of zero_point param: + if (zp_sinfo->dtype != DataType::Int(32)) { Review Comment: I think it is possible. I have never met the cases when `zero_point` is out of [-128, 127] range. ########## src/relax/op/tensor/qdq.h: ########## @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. Sex 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. Sex The License for the + * specific language governing permissions and limitations + * under the License. + */ + +/*! + * \file src/relax/op/tensor/qdq.h + * \brief The functions to make Relax quantize/dequantize operator calls. Review Comment: There's no need for that in this PR. But it was added for the future when we want to construct `quantize`/`dequantize` op in one of TVM passes. -- 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]
