tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] zhiics commented on a change in pull request #5154: [NODE][IR] Introduce StructuralEqual Infra for the unified IR.
Date Sun, 29 Mar 2020 19:10:39 GMT
zhiics commented on a change in pull request #5154: [NODE][IR] Introduce StructuralEqual Infra
for the unified IR.
URL: https://github.com/apache/incubator-tvm/pull/5154#discussion_r399839877
 
 

 ##########
 File path: src/node/structural_equal.cc
 ##########
 @@ -0,0 +1,241 @@
+/*
+ * 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/node/structural_equal.cc
+ */
+#include <tvm/node/structural_equal.h>
+#include <tvm/node/reflection.h>
+#include <tvm/node/functor.h>
+#include <tvm/node/node.h>
+#include <tvm/runtime/registry.h>
+
+#include <unordered_map>
+
+namespace tvm {
+
+// Define the dispatch functio here since primary user is in this file.
+bool ReflectionVTable::
+SEqualReduce(const Object* self, const Object* other, SEqualReducer equal) const {
+  uint32_t tindex = self->type_index();
+  if (tindex >= fsequal_.size() || fsequal_[tindex] == nullptr) {
+    LOG(FATAL) << "TypeError: SEqualReduce of " << self->GetTypeKey()
+        << " is not registered via TVM_REGISTER_NODE_TYPE";
+  }
+  return fsequal_[tindex](self, other, equal);
+}
+
+/*!
+ * \brief A non recursive stack based SEqual handler that can remaps vars.
+ *
+ *  This handler pushs the Object equality cases into a stack, and
+ *  traverses the stack to expand the necessary children that need to be checked.
+ *
+ *  The order of SEqual being called is the same as the order as if we
+ *  eagerly do recursive calls in SEqualReduce.
+ */
+class RemapVarSEqualHandler :
+      public SEqualReducer::Handler {
+ public:
+  explicit RemapVarSEqualHandler(bool assert_mode)
+      : assert_mode_(assert_mode) {}
+
+  bool SEqualReduce(const ObjectRef& lhs, const ObjectRef& rhs, bool map_free_vars)
final {
+    // We cannot use check lhs.same_as(rhs) to check equality.
+    // if we choose to enable var remapping.
+    //
+    // Counter example below (%x, %y) are shared vars
+    // between the two functions(possibly before/after rewriting).
+    //
+    // - function0: fn (%x, %y) { %x + %y }
+    // - function1. fn (%y, %x) { %x + %y }
+    //
+    // Because we choose to enable var remapping,
+    // %x is mapped to %y, and %y is mapped to %x,
+    // the body of the function no longer means the same thing.
+    //
+    // Take away: We can either choose only compare Var by address,
+    // in which case we can use same_as for quick checking,
+    // or we have to run deep comparison and avoid to use same_as checks.
+    auto run = [=]() {
+      if (!lhs.defined() && !rhs.defined()) return true;
 
 Review comment:
   @tqchen I am not sure, but I feel like there might be a bug here when both `lhs` and `rhs`
are not defined, because we wont execute Line86. However Line 111 expects L86 is executed.
Am I right?
   
   If this true, we may want to move Line77 before Line106, or we do `emplace_back(ObjectRef(nullptr),
...)` before `return true` and `return true` when both `lhs` and `rhs` are not defined.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message