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 2020/09/10 04:19:35 UTC

[GitHub] [incubator-tvm] hypercubestart commented on a change in pull request #6400: [Relay] Add Defunctionalization Pass

hypercubestart commented on a change in pull request #6400:
URL: https://github.com/apache/incubator-tvm/pull/6400#discussion_r486053273



##########
File path: src/relay/transforms/defunctionalization.cc
##########
@@ -0,0 +1,432 @@
+/*
+ * 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 defunctionalization.cc
+ *
+ * \brief Defunctionalization for Relay IR
+ *
+ * This pass transforms a higher-order program into a first-order program with defunctionalization.
+ * This means that all higher order functions (i.e functions that take function arguments or return
+ * functions) should be transformed into a semantically equivalent first order one.
+ *
+ * This pass implements a basic typed defunctionalization method.
+ * All higher order functions are cloned and specialized (so that there are no type params).
+ * Function type arguments are encoded as datatypes and a helper `apply` function is used
+ * to "call" them.
+ *
+ * For example, take the following higher order program:
+ * fun map F y = case y of
+ *          Nil => Nil
+ *          | Cons(x, XS) => Cons(F z, map F XS)
+ * fun addone 1 = map (\x -> \x + 1) 1
+ *
+ * where `addone` is our program.
+ * When we call the `map` function, we see that it is a higher-order function,
+ * but we can clone `map ` function and specialize it with the type_params of the call.
+ * In addition, our function argument `(\x -> \x + 1)` will be encoded as a datatype constructor,
+ * which we will call `incr`, and all calls to `F` in our specialized map function will use the
+ * helper `apply` function.
+ *
+ * After defunctionalization, we get:
+ * fun apply encoding arg =  case encoding of
+ *     “incr” => incr arg
+ * fun map’ F y = case y of
+ *           Nil => Nil
+ *           | Cons(x, xs) => Cons(apply F x, map’ F xs)
+ * fun addone 1 = map’ “incr” 1
+ *
+ * Currently, defunctionalization makes the following assumptions:
+ * - functions cannot return function values
+ * - function arguments are in two forms: identifier or a lambda abstraction
+ * - no functions stored in datatype
+ * - functions are not let binded
+ */
+
+#include <tvm/ir/type_functor.h>
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/feature.h>
+#include <tvm/relay/transform.h>
+#include <tvm/te/operation.h>
+
+#include "../analysis/type_solver.h"
+#include "../transforms/pass_util.h"
+namespace tvm {
+namespace relay {
+
+// determine if type contains a FuncType
+bool HasFuncType(const Type& t) {
+  struct FuncTypeVisitor : TypeVisitor {
+    bool has_func_type;
+    FuncTypeVisitor() : has_func_type(false) {}
+
+    void VisitType_(const FuncTypeNode* op) { this->has_func_type = true; }
+  };
+
+  auto visitor = FuncTypeVisitor();
+  visitor.VisitType(t);
+  return visitor.has_func_type;
+}
+// determine if FuncType is a higher order type
+bool IsHigherOrderFunc(const FuncType& t) {
+  bool higher_order = false;
+  for (auto arg : t->arg_types) {
+    higher_order |= HasFuncType(arg);
+  }
+  return higher_order |= HasFuncType(t->ret_type);
+}
+
+/*!
+ * \brief mutator for driving the Defunctionalization transformation
+ */
+class DefuncMutator : public ExprMutator {
+ public:
+  explicit DefuncMutator(const IRModule& mod) : mod(mod), constructor_counter(0) {}
+
+  Expr VisitExpr_(const CallNode* call) {
+    if (auto op = call->op.as<GlobalVarNode>()) {
+      CHECK_EQ(call->type_args.size(), op->checked_type().as<FuncTypeNode>()->type_params.size())
+          << "all type args must be explicit";
+
+      auto op_type = InstFuncType(op->checked_type().as<FuncTypeNode>(), call->type_args);
+      CHECK_EQ(FreeTypeVars(op_type, mod).size(), 0) << "free type vars in instantiated";
+      CHECK(!HasFuncType(op_type->ret_type)) << "returning functions not supported";
+
+      if (!IsHigherOrderFunc(op_type)) {
+        // not higher order function
+        return ExprMutator::VisitExpr_(call);
+      }
+
+      // first we encode function arguments
+      Array<Expr> args;
+      for (size_t i = 0; i < call->args.size(); i++) {
+        auto arg = call->args[i];
+        auto type = op_type->arg_types[i];
+        if (!HasFuncType(type)) {
+          args.push_back(arg);
+          continue;
+        }
+
+        args.push_back(EncodeArg(arg, type));

Review comment:
       yep, looks like CI issue was caused by this: https://github.com/apache/incubator-tvm/pull/6434




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