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/03 05:39:13 UTC

[GitHub] [tvm] lazycal opened a new pull request #10156: Fix broadcast InferCorrectLayout

lazycal opened a new pull request #10156:
URL: https://github.com/apache/tvm/pull/10156


   Thanks for contributing to TVM!   Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from [Reviewers](https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers) by @ them in the pull request thread.
   
   I'm reviewing this function after PR #10118, and found several buggy places. For instance, 
   - it does not respect the original input tensor's layout but always reset it to the other input's layout (see `test_broadcast_respect_input_layouts`);
   - it assumes one can always only transform one layout to adapt to the other. However, this is not always possible, see `test_broadcast_non_adaptable`.
   - `AdjustSubordinateFactors` should also be called in the else branch https://github.com/apache/tvm/blob/6a274af9cb4e7baf6d9dd06f0fb38bea5ac3154a/src/relay/transforms/infer_layout_utils.h#L222-L234. (see `test_alter_layout_nonscalar_broadcast`)
   
   This PR rewrites `BinaryBroadcastLayoutHelper` a bit, and I tried to make it as backward compatible as possible.
   
   ## Misc
   BTW I seem to spot another bug when I was working on this PR. So I sometimes I need to create a scalar layout, and I used `Layout("")`, but it returned an undefined layout. From the doc it should return a scalar layout right? I checked the constructor, and it seems in this if statement: https://github.com/apache/tvm/blob/6a274af9cb4e7baf6d9dd06f0fb38bea5ac3154a/src/tir/ir/data_layout.cc#L99, it directly returns without setting the `data_` field as in normal path: https://github.com/apache/tvm/blob/6a274af9cb4e7baf6d9dd06f0fb38bea5ac3154a/src/tir/ir/data_layout.cc#L144. I didn't fix this issue but work it around somehow, but it does seem like a bug to me :-).


-- 
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



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

Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10156:
URL: https://github.com/apache/tvm/pull/10156#discussion_r799775886



##########
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) {
+  DLOG(INFO) << "Isomorphic: "
+             << "lhs: " << lhs << " rhs: " << rhs << std::endl;
+  ICHECK(lhs.defined());
+  ICHECK(rhs.defined());
+  if (lhs->axes.size() != rhs->axes.size()) return false;
+  std::map<std::string, std::string> map_to, map_back;
+  for (size_t i = 0; i < lhs->axes.size(); ++i) {
+    auto& lhs_axis = LayoutAxis::Get(lhs->axes[i]);
+    auto& rhs_axis = LayoutAxis::Get(rhs->axes[i]);
+    std::string name_lhs = lhs_axis.name();
+    std::string name_rhs = rhs_axis.name();
+    if (lhs_axis.IsPrimal() != rhs_axis.IsPrimal()) return false;
+
+    auto it = map_to.find(name_lhs);
+    if (it == map_to.end())
+      map_to[name_lhs] = name_rhs;
+    else if (it->second != name_rhs)
+      return false;
+
+    it = map_back.find(name_rhs);
+    if (it == map_back.end())
+      map_back[name_rhs] = name_lhs;
+    else if (it->second != name_lhs)
+      return false;
+    if (!lhs_axis.IsPrimal() && lhs.FactorOf(lhs_axis) != rhs.FactorOf(rhs_axis)) return false;
+  }
+  return true;
+}
+Layout TryTransformLike(const Layout& old, const Layout& ref_old, const Layout& ref_new) {
+  DLOG(INFO) << "transform_layout: old = " << old << ", ref_new = " << ref_new
+             << ", ref_old = " << ref_old << std::endl;
+  ICHECK(ref_old.defined());
+  ICHECK(ref_new.defined());
+  ICHECK(old.defined());
+
+  {  // check if old and ref_old are similar enough such that it's
+     // compatible for the transform ref_old -> ref_new
+    const Layout& large = ref_old.ndim() > old.ndim() ? ref_old : old;
+    const Layout& small = large == ref_old ? old : ref_old;
+    Layout large_sublayout = large.SubLayout(large.ndim() - small.ndim(), small.ndim()),
+           rest_sublayout = large.SubLayout(0, large.ndim() - small.ndim());
+    bool orthorgonal = true;
+    for (auto i : rest_sublayout->axes)
+      if (large_sublayout.IndexOf(LayoutAxis::Get(i).ToPrimal()) != -1 ||
+          large_sublayout.IndexOf(LayoutAxis::Get(i).ToSubordinate()) != -1) {
+        orthorgonal = false;
+        break;
+      }
+    if (!orthorgonal || !Isomorphic(large_sublayout, small))
+      return Layout::Undef();  // For now this case is not supported.
+  }
+
+  // `old` is compatible. Now learn the axis name mapping between `old` and `ref_old`
+  if (old.ndim() == 0) return old;  // an optmization for scalar: no-op
+  int mapping[26];
+  bool used[26];
+  memset(mapping, -1, sizeof mapping);
+  memset(used, 0, sizeof used);
+  auto find_unused = [&](char preference) -> char {
+    if (!used[preference - 'A']) return preference;  // preference unused
+    for (int i = 0; i < 26; ++i)
+      if (!used[i]) return 'A' + i;
+    LOG(FATAL) << "All letters are used";
+    return 0;
+  };
+  for (int j = old->axes.size() - 1, i = ref_old->axes.size() - 1; j >= 0; --i, --j) {
+    char name_ref = LayoutAxis::Get(ref_old->axes[i]).ToPrimal().name()[0];
+    char name = LayoutAxis::Get(old->axes[j]).ToPrimal().name()[0];
+    mapping[name_ref - 'A'] = name - 'A';
+    used[name - 'A'] = true;
+  }
+  for (int i = ref_old->axes.size() - 1; i >= 0; --i) {
+    char name_ref = LayoutAxis::Get(ref_old->axes[i]).ToPrimal().name()[0];
+    int name = mapping[name_ref - 'A'];
+    if (name == -1) {
+      mapping[name_ref - 'A'] = find_unused(name_ref) - 'A';
+      used[mapping[name_ref - 'A']] = true;
+    }
+  }
+

Review comment:
       Add new lines above below lambda and between for loop




-- 
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



[GitHub] [tvm] lazycal commented on pull request #10156: Fix broadcast InferCorrectLayout

Posted by GitBox <gi...@apache.org>.
lazycal commented on pull request #10156:
URL: https://github.com/apache/tvm/pull/10156#issuecomment-1028622121


   @masahi 


-- 
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



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

Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10156:
URL: https://github.com/apache/tvm/pull/10156#discussion_r799775886



##########
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) {
+  DLOG(INFO) << "Isomorphic: "
+             << "lhs: " << lhs << " rhs: " << rhs << std::endl;
+  ICHECK(lhs.defined());
+  ICHECK(rhs.defined());
+  if (lhs->axes.size() != rhs->axes.size()) return false;
+  std::map<std::string, std::string> map_to, map_back;
+  for (size_t i = 0; i < lhs->axes.size(); ++i) {
+    auto& lhs_axis = LayoutAxis::Get(lhs->axes[i]);
+    auto& rhs_axis = LayoutAxis::Get(rhs->axes[i]);
+    std::string name_lhs = lhs_axis.name();
+    std::string name_rhs = rhs_axis.name();
+    if (lhs_axis.IsPrimal() != rhs_axis.IsPrimal()) return false;
+
+    auto it = map_to.find(name_lhs);
+    if (it == map_to.end())
+      map_to[name_lhs] = name_rhs;
+    else if (it->second != name_rhs)
+      return false;
+
+    it = map_back.find(name_rhs);
+    if (it == map_back.end())
+      map_back[name_rhs] = name_lhs;
+    else if (it->second != name_lhs)
+      return false;
+    if (!lhs_axis.IsPrimal() && lhs.FactorOf(lhs_axis) != rhs.FactorOf(rhs_axis)) return false;
+  }
+  return true;
+}
+Layout TryTransformLike(const Layout& old, const Layout& ref_old, const Layout& ref_new) {
+  DLOG(INFO) << "transform_layout: old = " << old << ", ref_new = " << ref_new
+             << ", ref_old = " << ref_old << std::endl;
+  ICHECK(ref_old.defined());
+  ICHECK(ref_new.defined());
+  ICHECK(old.defined());
+
+  {  // check if old and ref_old are similar enough such that it's
+     // compatible for the transform ref_old -> ref_new
+    const Layout& large = ref_old.ndim() > old.ndim() ? ref_old : old;
+    const Layout& small = large == ref_old ? old : ref_old;
+    Layout large_sublayout = large.SubLayout(large.ndim() - small.ndim(), small.ndim()),
+           rest_sublayout = large.SubLayout(0, large.ndim() - small.ndim());
+    bool orthorgonal = true;
+    for (auto i : rest_sublayout->axes)
+      if (large_sublayout.IndexOf(LayoutAxis::Get(i).ToPrimal()) != -1 ||
+          large_sublayout.IndexOf(LayoutAxis::Get(i).ToSubordinate()) != -1) {
+        orthorgonal = false;
+        break;
+      }
+    if (!orthorgonal || !Isomorphic(large_sublayout, small))
+      return Layout::Undef();  // For now this case is not supported.
+  }
+
+  // `old` is compatible. Now learn the axis name mapping between `old` and `ref_old`
+  if (old.ndim() == 0) return old;  // an optmization for scalar: no-op
+  int mapping[26];
+  bool used[26];
+  memset(mapping, -1, sizeof mapping);
+  memset(used, 0, sizeof used);
+  auto find_unused = [&](char preference) -> char {
+    if (!used[preference - 'A']) return preference;  // preference unused
+    for (int i = 0; i < 26; ++i)
+      if (!used[i]) return 'A' + i;
+    LOG(FATAL) << "All letters are used";
+    return 0;
+  };
+  for (int j = old->axes.size() - 1, i = ref_old->axes.size() - 1; j >= 0; --i, --j) {
+    char name_ref = LayoutAxis::Get(ref_old->axes[i]).ToPrimal().name()[0];
+    char name = LayoutAxis::Get(old->axes[j]).ToPrimal().name()[0];
+    mapping[name_ref - 'A'] = name - 'A';
+    used[name - 'A'] = true;
+  }
+  for (int i = ref_old->axes.size() - 1; i >= 0; --i) {
+    char name_ref = LayoutAxis::Get(ref_old->axes[i]).ToPrimal().name()[0];
+    int name = mapping[name_ref - 'A'];
+    if (name == -1) {
+      mapping[name_ref - 'A'] = find_unused(name_ref) - 'A';
+      used[mapping[name_ref - 'A']] = true;
+    }
+  }
+

Review comment:
       Add new lines above between for loop




-- 
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



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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10156:
URL: https://github.com/apache/tvm/pull/10156#discussion_r799775462



##########
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) {
+  DLOG(INFO) << "Isomorphic: "
+             << "lhs: " << lhs << " rhs: " << rhs << std::endl;
+  ICHECK(lhs.defined());
+  ICHECK(rhs.defined());
+  if (lhs->axes.size() != rhs->axes.size()) return false;
+  std::map<std::string, std::string> map_to, map_back;
+  for (size_t i = 0; i < lhs->axes.size(); ++i) {
+    auto& lhs_axis = LayoutAxis::Get(lhs->axes[i]);
+    auto& rhs_axis = LayoutAxis::Get(rhs->axes[i]);
+    std::string name_lhs = lhs_axis.name();
+    std::string name_rhs = rhs_axis.name();
+    if (lhs_axis.IsPrimal() != rhs_axis.IsPrimal()) return false;
+
+    auto it = map_to.find(name_lhs);
+    if (it == map_to.end())
+      map_to[name_lhs] = name_rhs;
+    else if (it->second != name_rhs)
+      return false;
+
+    it = map_back.find(name_rhs);
+    if (it == map_back.end())
+      map_back[name_rhs] = name_lhs;
+    else if (it->second != name_lhs)
+      return false;
+    if (!lhs_axis.IsPrimal() && lhs.FactorOf(lhs_axis) != rhs.FactorOf(rhs_axis)) return false;
+  }
+  return true;
+}
+Layout TryTransformLike(const Layout& old, const Layout& ref_old, const Layout& ref_new) {
+  DLOG(INFO) << "transform_layout: old = " << old << ", ref_new = " << ref_new
+             << ", ref_old = " << ref_old << std::endl;
+  ICHECK(ref_old.defined());
+  ICHECK(ref_new.defined());
+  ICHECK(old.defined());
+
+  {  // check if old and ref_old are similar enough such that it's
+     // compatible for the transform ref_old -> ref_new
+    const Layout& large = ref_old.ndim() > old.ndim() ? ref_old : old;
+    const Layout& small = large == ref_old ? old : ref_old;
+    Layout large_sublayout = large.SubLayout(large.ndim() - small.ndim(), small.ndim()),
+           rest_sublayout = large.SubLayout(0, large.ndim() - small.ndim());
+    bool orthorgonal = true;
+    for (auto i : rest_sublayout->axes)
+      if (large_sublayout.IndexOf(LayoutAxis::Get(i).ToPrimal()) != -1 ||
+          large_sublayout.IndexOf(LayoutAxis::Get(i).ToSubordinate()) != -1) {
+        orthorgonal = false;
+        break;
+      }
+    if (!orthorgonal || !Isomorphic(large_sublayout, small))
+      return Layout::Undef();  // For now this case is not supported.
+  }
+
+  // `old` is compatible. Now learn the axis name mapping between `old` and `ref_old`
+  if (old.ndim() == 0) return old;  // an optmization for scalar: no-op
+  int mapping[26];
+  bool used[26];
+  memset(mapping, -1, sizeof mapping);
+  memset(used, 0, sizeof used);

Review comment:
       use std::vector




-- 
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



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

Posted by GitBox <gi...@apache.org>.
masahi commented on a change in pull request #10156:
URL: https://github.com/apache/tvm/pull/10156#discussion_r799775208



##########
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) {
+  DLOG(INFO) << "Isomorphic: "
+             << "lhs: " << lhs << " rhs: " << rhs << std::endl;
+  ICHECK(lhs.defined());
+  ICHECK(rhs.defined());
+  if (lhs->axes.size() != rhs->axes.size()) return false;
+  std::map<std::string, std::string> map_to, map_back;
+  for (size_t i = 0; i < lhs->axes.size(); ++i) {
+    auto& lhs_axis = LayoutAxis::Get(lhs->axes[i]);
+    auto& rhs_axis = LayoutAxis::Get(rhs->axes[i]);
+    std::string name_lhs = lhs_axis.name();
+    std::string name_rhs = rhs_axis.name();
+    if (lhs_axis.IsPrimal() != rhs_axis.IsPrimal()) return false;
+
+    auto it = map_to.find(name_lhs);
+    if (it == map_to.end())
+      map_to[name_lhs] = name_rhs;
+    else if (it->second != name_rhs)
+      return false;
+
+    it = map_back.find(name_rhs);
+    if (it == map_back.end())
+      map_back[name_rhs] = name_lhs;
+    else if (it->second != name_lhs)
+      return false;
+    if (!lhs_axis.IsPrimal() && lhs.FactorOf(lhs_axis) != rhs.FactorOf(rhs_axis)) return false;
+  }
+  return true;
+}

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



[GitHub] [tvm] lazycal commented on pull request #10156: Fix broadcast InferCorrectLayout

Posted by GitBox <gi...@apache.org>.
lazycal commented on pull request #10156:
URL: https://github.com/apache/tvm/pull/10156#issuecomment-1030324983


   @masahi The code is changed according to your suggestions.


-- 
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



[GitHub] [tvm] masahi merged pull request #10156: Fix broadcast InferCorrectLayout

Posted by GitBox <gi...@apache.org>.
masahi merged pull request #10156:
URL: https://github.com/apache/tvm/pull/10156


   


-- 
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