zhiics commented on a change in pull request #4241: [Dynamic Shape] Add Graph 
Dispatcher
URL: https://github.com/apache/incubator-tvm/pull/4241#discussion_r362155524
 
 

 ##########
 File path: src/relay/pass/dispatch_global_func.cc
 ##########
 @@ -0,0 +1,255 @@
+/*
+ * 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 dispatch_global_func.cc
+ *
+ * \brief API for dispatch global function with dynamic input shape.
+ */
+
+#include <tvm/relay/module.h>
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/transform.h>
+#include <tvm/relay/attrs/reduce.h>
+
+namespace tvm {
+namespace relay {
+
+using namespace runtime;
+
+using InputShapeDict = Map<std::string, Array<IndexExpr>>;
+using BucketDict = Map<std::string, Map<Integer, Array<Array<Integer>>>>;
+using ConditionList = Array<Array<Expr>>;
+
+/*! \brief Dispatch a global var for dynamic input shape
+ * Helper class to build dispatching tree.
+ */
+class DispatchTreeBuilder {
+ public:
+  DispatchTreeBuilder(const Module& mod,
+                      const std::string& func_name,
+                      const ConditionList& conditions)
+    : mod_(mod), func_name_(func_name), conditions_(conditions) {
+    func_ = mod_->Lookup(func_name_);
+    param_vars_ = FreeVars(func_->body);
+    for (auto param_var : param_vars_) {
+      param_exprs_.push_back(Expr(param_var));
+    }
+    num_copied_funcs_ = 0;
+  }
+
+  Expr BuildDispatchTree(unsigned level, unsigned pos);
+
+ private:
+  Module mod_;
+  Function func_;
+  Array<Var> param_vars_;
+  Array<Expr> param_exprs_;
+  std::string func_name_;
+  ConditionList conditions_;
+  int num_copied_funcs_;
+};
+
+// Helper function to build dispatch tree.
+// This function build a tree from bottom to up:
+//
+//   1. For leaf node, if it is the rightmost one, insert function call.
+//      Otherwise, insert an IfNode with function call and recursive call.
+//
+//   2. For non-leaf level, if it is the rightmost one, recursively call
+//      the leftmost node on the next level. Otherwise, recursively call
+//      the next node on the same level and the leftmost node on the next
+//      level.
+Expr DispatchTreeBuilder::BuildDispatchTree(unsigned level, unsigned pos) {
+  if (level == conditions_.size() - 1) {
+    GlobalVar copied_global_var =
+      GlobalVarNode::make(func_name_ + "_copy_" +
+                            std::to_string(num_copied_funcs_));
+    Function new_func = FunctionNode::make(param_vars_,
+                                           func_->body,
+                                           func_->ret_type,
+                                           func_->type_params,
+                                           func_->attrs);
+    mod_->Add(copied_global_var, new_func);
+    num_copied_funcs_ += 1;
+    if (pos == conditions_[level].size() - 1) {
+      return CallNode::make(copied_global_var, param_exprs_);
+    } else {
+      return IfNode::make(conditions_[level][pos],
+                          CallNode::make(copied_global_var, param_exprs_),
+                          BuildDispatchTree(level, pos + 1));
+    }
+  } else {
+    if (pos == conditions_[level].size() - 1) {
+      return BuildDispatchTree(level + 1, 0);
+    } else {
+      return IfNode::make(conditions_[level][pos],
+                          BuildDispatchTree(level + 1, 0),
+                          BuildDispatchTree(level, pos + 1));
+    }
+  }
+}
+
+/*! \brief Dispatch a global var for dynamic input shape
+ *
+ * This function needs the following steps:
+ *
+ *   1. Given symbolic input shape, return a BucketDict with using dispatching
+ *      logic provided by dispatch_func.
+ *
+ *   2. Generate a condition expression list given from BucketDict.
+ *
+ *   3. Recursively build a dispatching tree with condition list.
+ *
+ * Note that currently only continuous range is valid as bucket, and is
+ * represented as a pair of (low, high). If high is -1, it means the high
+ * end is positive infinite.
+ */
+Module AddDispatchFunc(const Module& mod,
+                          const std::string& func_name,
 
 Review comment:
   align

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to