You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/02/04 20:02:20 UTC

[GitHub] [tvm] masahi commented on a change in pull request #10156: Fix broadcast InferCorrectLayout

masahi commented on a change in pull request #10156:
URL: https://github.com/apache/tvm/pull/10156#discussion_r799775086



##########
File path: src/relay/transforms/infer_layout_utils.cc
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.
+ */
+
+#include "infer_layout_utils.h"
+
+#include <tvm/relay/expr.h>
+#include <tvm/relay/op_attr_types.h>
+#include <tvm/tir/data_layout.h>
+
+#include <map>
+#include <string>
+#include <tuple>
+#include <utility>
+
+#include "pattern_utils.h"
+#include "tvm/runtime/logging.h"
+
+namespace tvm {
+namespace relay {
+
+Layout AdjustSubordinateFactors(const Layout& src_layout, const Layout& old_layout,
+                                const Array<tvm::PrimExpr>& old_shape) {
+  // For each subordinate axis
+  //   1) Find the corresponding dual axis.
+  //   2) Find the Index of this dual axis in old_layout.
+  //   3) Find the shape of the that axis in old_shape.
+  //   4) a) Adjust factor to 1, if that shape is 1. b) Else retain the factor.
+  DLOG(INFO) << "AdjustSubordinateFactors"
+             << "src_layout: " << src_layout << " old_layout: " << old_layout
+             << " old_shape: " << old_shape << std::endl;
+  std::string new_layout;
+  for (auto axis : src_layout->axes) {
+    if (!LayoutAxis::Get(axis).IsPrimal()) {
+      bool is_shape_one = false;
+      // 1) Find the corresponding dual axis
+      const auto& dual_axis = LayoutAxis::Get(axis).ToPrimal();
+
+      // 2) Find the index of this dual axis in old_layout
+      int old_axis = old_layout.IndexOf(dual_axis);
+
+      if (old_axis == -1) {
+        new_layout += "1";
+        is_shape_one = true;
+      } else {
+        // 3) Find the shape of this index in old_shape
+        auto shape_val = old_shape[old_axis];
+
+        // 4) a) Check if this shape element is 1.
+        if (auto* shape_int = shape_val.as<IntImmNode>()) {
+          if (shape_int->value == 1) {
+            new_layout += "1";
+            is_shape_one = true;
+          }
+        }
+      }
+
+      // 4) b) If shape is not 1, retain the factor.
+      if (!is_shape_one) {
+        auto new_shape_val = src_layout.FactorOf(dual_axis);
+        new_layout += std::to_string(new_shape_val);
+      }
+    }
+    new_layout += LayoutAxis::Get(axis).name();
+  }
+  return new_layout != "" ? Layout(new_layout)
+                          : Layout("H").SubLayout(0, 0);  // hack to create a scalar layout
+}
+bool Isomorphic(const Layout& lhs, const Layout& rhs) {

Review comment:
       new 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: commits-unsubscribe@tvm.apache.org

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