anko-intel commented on a change in pull request #20430: URL: https://github.com/apache/incubator-mxnet/pull/20430#discussion_r670199194
########## File path: src/operator/quantization/quantize_graph_pass.h ########## @@ -0,0 +1,155 @@ +/* + * 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) 2021 by Contributors + * \file quantize_graph_pass.h + * \brief + */ +#ifndef MXNET_OPERATOR_QUANTIZATION_QUANTIZE_GRAPH_PASS_H_ +#define MXNET_OPERATOR_QUANTIZATION_QUANTIZE_GRAPH_PASS_H_ + +#include <mxnet/op_attr_types.h> +#include <nnvm/graph.h> +#include <nnvm/pass.h> +#include <queue> +#include <unordered_map> +#include <unordered_set> +#include <vector> +#include <string> +#include "quantize_v2-inl.h" +#include "../nn/mkldnn/mkldnn_fully_connected-inl.h" +#include "../../common/utils.h" + +namespace mxnet { +namespace op { + +using nnvm::Symbol; +using nnvm::Node; +using nnvm::ObjectPtr; +using nnvm::NodeEntry; +using nnvm::Graph; + +inline ObjectPtr CreateNode(std::string op_name, std::string node_name) { + ObjectPtr node = Node::Create(); + node->attrs.name = node_name; + if (op_name == "nullptr") { + node->attrs.op = nullptr; + // ugly workaround because VariableParam is not exposed + node->attrs.parsed = + nnvm::Symbol::CreateVariable(node->attrs.name).outputs[0].node->attrs.parsed; + } else { + node->attrs.op = Op::Get(op_name); + } + return node; +} + +template <bool require_bias> +static inline bool IsOneDNNFullyConnected(const ObjectPtr& n) { +#if MXNET_USE_MKLDNN == 1 + if (n->op() == Op::Get("_sg_mkldnn_fully_connected")) { + auto const& param = nnvm::get<MKLDNNFCFullParam>(n->attrs.parsed); + if (!(param.mkldnn_param.channel_wise_quantize.has_value() && + param.mkldnn_param.channel_wise_quantize.value())) { + return !require_bias || (param.default_param.no_bias == false && + n->inputs[2].node->is_variable()); + } + } +#endif + return false; +} + +static inline bool IsQuantize(const ObjectPtr& n) { + if (n->op() == Op::Get("_contrib_quantize_v2")) { + auto const ¶m = nnvm::get<QuantizeV2Param>(n->attrs.parsed); + if (param.min_calib_range.has_value() && + param.min_calib_range.value() < 0.0f) { + return true; + } + } + return false; +} + +static NDArray* FindInArgByName(const Graph &g, const std::string& name) { + const std::vector<std::string>& in_arg_names = + g.GetAttr<std::vector<std::string>>("in_arg_names"); + size_t i = std::distance(in_arg_names.begin(), + std::find(in_arg_names.begin(), in_arg_names.end(), name)); + if (i == in_arg_names.size()) { + LOG(FATAL) << name << " not found in in_arg_names"; + } + return g.GetAttr<NDArray **>("in_args")[i]; +} + +// Rescales weights, min_weight and max_weight. Returns bias_int32_rescale. +static inline float RescaleWeights(const Graph &g, const ObjectPtr &fc, NDArray* weight_tensor) { + ObjectPtr &quantize = fc->inputs[0].node; + auto min_data = std::stof(quantize->attrs.dict.at("min_calib_range")); + auto max_data = std::stof(quantize->attrs.dict.at("max_calib_range")); + + FCInputIndex id(nnvm::get<MKLDNNFCFullParam>(fc->attrs.parsed)); + auto in = fc->inputs; + + float *min_weight = FindInArgByName(g, in[id.weight_min].node->attrs.name)->data().dptr<float>(); Review comment: One suggestion below: ```suggestion float *min_weight = FindInArgByName(g, in[idx.weight_min].node->attrs.name)->data().dptr<float>(); ``` or maybe add another helper function which already return pointer to float to avoid "->data().dptr<float>()" in the end of each line -- 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]
