ciyongch commented on a change in pull request #17147: [WIP] Quantized Elemwise Mul Operator URL: https://github.com/apache/incubator-mxnet/pull/17147#discussion_r360802245
########## File path: src/operator/quantization/quantized_elemwise_mul.cc ########## @@ -0,0 +1,265 @@ +/* + * 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. + */ + +/*! + * Copyright (c) 2016 by Contributors + * \file quantized_elemwise_mul.cc + * \brief CPU Implementation of basic elementwise binary broadcast operators + */ +#include <mxnet/op_attr_types.h> +#include "../tensor/elemwise_binary_op-inl.h" +#include "./quantized_elemwise_mul-inl.h" +#include "./quantization_utils.h" + +namespace mxnet { +namespace op { + +DMLC_REGISTER_PARAMETER(QuantizeElemwiseMulParam); + +static std::vector<std::string> QuantizedElemwiseMulOutputNames(const NodeAttrs &attrs) { + const QuantizeElemwiseMulParam& params = nnvm::get<QuantizeElemwiseMulParam>(attrs.parsed); + if (params.enable_float_output) + return std::vector<std::string>{"output"}; + else + return std::vector<std::string>{"output", "min_output", "max_output"}; +} + +inline bool QuantizedElemwiseMulOpShape(const nnvm::NodeAttrs& attrs, + mxnet::ShapeVector *in_attrs, + mxnet::ShapeVector *out_attrs) { + using namespace mshadow; + const QuantizeElemwiseMulParam& params = nnvm::get<QuantizeElemwiseMulParam>(attrs.parsed); + const mxnet::TShape &lshape = (*in_attrs)[quantized_elemwise_mul::kLhs]; + const mxnet::TShape &rshape = (*in_attrs)[quantized_elemwise_mul::kRhs]; + if (!ndim_is_known(lshape) || !ndim_is_known(rshape)) return false; + CHECK_EQ(lshape.ndim(), rshape.ndim()) << "Currently, quantized elemwise multiply doesn't support broadcast."; + for (int i = 0; i < lshape.ndim(); ++i) { + CHECK_EQ(lshape[i], rshape[i]); + } + SHAPE_ASSIGN_CHECK(*in_attrs, quantized_elemwise_mul::kLhsMin, mxnet::TShape(1, 1)); + SHAPE_ASSIGN_CHECK(*in_attrs, quantized_elemwise_mul::kLhsMax, mxnet::TShape(1, 1)); + SHAPE_ASSIGN_CHECK(*in_attrs, quantized_elemwise_mul::kRhsMin, mxnet::TShape(1, 1)); + SHAPE_ASSIGN_CHECK(*in_attrs, quantized_elemwise_mul::kRhsMax, mxnet::TShape(1, 1)); + out_attrs->clear(); + + mxnet::TShape oshape(lshape); + out_attrs->push_back(oshape); + if (!params.enable_float_output) { + out_attrs->push_back(mxnet::TShape(1, 1)); + out_attrs->push_back(mxnet::TShape(1, 1)); + } + return shape_is_known(oshape); +} + +inline bool QuantizedElemwiseMulOpType(const nnvm::NodeAttrs& attrs, + std::vector<int> *in_type, + std::vector<int> *out_type) { + const QuantizeElemwiseMulParam& params = nnvm::get<QuantizeElemwiseMulParam>(attrs.parsed); + for (int i = 0; i < 2; ++i) { + if (in_type->at(i) == mshadow::kInt8) { + TYPE_ASSIGN_CHECK(*in_type, i, mshadow::kInt8); + } else { + LOG(ERROR) << "currently, quantized elemwise mul only support int8 inputs."; + } + } + TYPE_ASSIGN_CHECK(*in_type, 2, mshadow::kFloat32); Review comment: Suggest to use enum same as above. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
