ashutosh-arm commented on code in PR #12353: URL: https://github.com/apache/tvm/pull/12353#discussion_r951497535
########## src/relay/backend/contrib/cmsisnn/fuse_pads.cc: ########## @@ -0,0 +1,208 @@ +/* + * 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/backend/contrib/cmsisnn/fuse_pads.cc + * \brief Fuses pads that precede qnn.conv2d ops inside CMSIS-NN composite functions. + */ + +#include <tvm/relay/attrs/nn.h> +#include <tvm/relay/attrs/transform.h> +#include <tvm/relay/expr_functor.h> +#include <tvm/relay/transform.h> +#include <tvm/runtime/ndarray.h> + +#include "../../../op/make_op.h" +#include "../../../qnn/utils.h" +#include "../../../transforms/pattern_utils.h" +#include "convolutions.h" + +namespace tvm { +namespace relay { +namespace contrib { +namespace cmsisnn { + +inline IntImm ToIntImm(int32_t value) { return IntImm(DataType::Int(32), value); } + +/*! + * \brief From padding attributes of nn.pad and qnn.conv2d, calculates effective padding along H + * and W dimensions. + */ +Array<IntImm> GetEffectiveConv2DPadding(Expr conv2d, Expr pad) { + // pad_width: ((), (top, bottom), (left, right), ()) for NHWC layout + // conv2d_attrs->padding: (top, left, bottom, right) + auto* conv2d_call = conv2d.as<CallNode>(); + auto* conv2d_attrs = conv2d_call->attrs.as<Conv2DAttrs>(); + std::string data_layout = conv2d_attrs->data_layout.c_str(); + int pos_h = data_layout.find("H"); + int pos_w = data_layout.find("W"); + + auto* pad_call = pad.as<CallNode>(); + Array<Array<Integer>> pad_width = pad_call->attrs.as<PadAttrs>()->pad_width; + int pad_top = + qnn::get_const_int(conv2d_attrs->padding[0]) + qnn::get_const_int(pad_width[pos_h][0]); + int pad_left = + qnn::get_const_int(conv2d_attrs->padding[1]) + qnn::get_const_int(pad_width[pos_w][0]); + int pad_bottom = + qnn::get_const_int(conv2d_attrs->padding[2]) + qnn::get_const_int(pad_width[pos_h][1]); + int pad_right = + qnn::get_const_int(conv2d_attrs->padding[3]) + qnn::get_const_int(pad_width[pos_w][1]); + + return {ToIntImm(pad_top), ToIntImm(pad_left), ToIntImm(pad_bottom), ToIntImm(pad_right)}; +} + +/*! + * \brief This Mutator will find all partitioned functions meant for CMSIS-NN Conv2D. + * Then, it will fuse preceding pads with qnn.conv2d. + */ +class FusePadsMutator : public MixedModeMutator { + public: + explicit FusePadsMutator(const IRModule& mod) : mod_(mod) {} + + private: + /*! * \brief In order to eliminate preceding nn.pad op, pad_width of nn.pad is passed onto Review Comment: ACK -- 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]
