ashutosh-arm commented on code in PR #12353:
URL: https://github.com/apache/tvm/pull/12353#discussion_r944485521


##########
src/relay/backend/contrib/cmsisnn/fuse_pads.cc:
##########
@@ -0,0 +1,219 @@
+
+/*
+ * 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 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 {
+
+/*!
+ * \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
+   * convolution layer to update Conv2DAttrs's padding attribute. */
+  void UpdateConv2DPadding(const CallNode* conv2d_call, const 
Array<Array<Integer>>& pad_width,
+                           const Conv2DAttrs* conv2d_attrs, Attrs* new_attrs) {
+    auto attrs = make_object<Conv2DAttrs>();
+    attrs->strides = std::move(conv2d_attrs->strides);
+    attrs->dilation = std::move(conv2d_attrs->dilation);
+    attrs->groups = conv2d_attrs->groups;
+    attrs->channels = std::move(conv2d_attrs->channels);
+    attrs->kernel_size = std::move(conv2d_attrs->kernel_size);
+    attrs->data_layout = std::move(conv2d_attrs->data_layout);
+    attrs->kernel_layout = std::move(conv2d_attrs->kernel_layout);
+    attrs->out_layout = std::move(conv2d_attrs->out_layout);
+    attrs->out_dtype = std::move(conv2d_attrs->out_dtype);
+
+    // pad_width: ((), (top, bottom), (left, right), ()) for NHWC layout
+    // conv2d_attrs->padding: (top, left, bottom, right)
+    std::string data_layout = conv2d_attrs->data_layout.c_str();
+    int pos_h = data_layout.find("H");
+    int pos_w = data_layout.find("W");
+
+    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]);
+
+    int pad_diff_w = pad_right - pad_left;
+    int pad_diff_h = pad_bottom - pad_top;
+    bool can_pad_be_fused =
+        ((pad_diff_w == 0 || pad_diff_w == 1) && (pad_diff_h == 0 || 
pad_diff_h == 1));
+    std::string error = "Difference on each side of a dimension should be 
either 0 or 1. ";
+    error += "Effective padding in this case: (pad_top, pad_left, pad_bottom, 
pad_right)=(";
+    error += std::to_string(pad_top);
+    error += ", ";
+    error += std::to_string(pad_left);
+    error += ", ";
+    error += std::to_string(pad_bottom);
+    error += ", ";
+    error += std::to_string(pad_right);
+    error += ")";
+    ICHECK(can_pad_be_fused) << error;
+
+    attrs->padding = {pad_top, pad_left, pad_bottom, pad_right};
+    *new_attrs = tvm::Attrs{attrs};
+  }
+
+  /*!
+   * \brief Identifies the sequence for qnn.conv2D and fuses the preceding 
nn.pad present within the
+   * CMSIS-NN partitioned function. */
+  Expr FusePadConv2d(const Expr& expr) {
+    const CallNode* clip_call = nullptr;
+    const CallNode* requantize_call = nullptr;
+    const CallNode* bias_add_call = nullptr;

Review Comment:
   @lhutton1 Could you PTAL? I've tried compressing the recreation of the new 
Conv2D now.



-- 
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: commits-unsubscr...@tvm.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to