pass_util.h 5.01 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * 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
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12 13 14 15 16 17 18 19
 * 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.
 */

20 21 22 23
/*!
 *  Copyright (c) 2018 by Contributors.
 *
 * \file tvm/relay/pass/pass_util.h
24
 * \brief Utilities for writing passes
25 26 27 28 29 30 31
 */
#ifndef TVM_RELAY_PASS_PASS_UTIL_H_
#define TVM_RELAY_PASS_PASS_UTIL_H_

#include <tvm/relay/op.h>
#include <tvm/relay/expr.h>
#include <tvm/relay/attrs/transform.h>
32
#include <memory>
33
#include <unordered_map>
34 35 36 37 38 39 40 41 42 43 44 45

namespace tvm {
namespace relay {

/*!
 * \brief Get reference counter of each internal ExprNode in body.
 * \param body The body expression.
 * \return The reference count mapping.
 */
std::unordered_map<const Node*, size_t>
GetExprRefCount(const Expr& body);

46 47 48 49 50 51 52
/*!
 * \brief Check if expr is positive constant.
 * \param expr The expression to be checked.
 * \return Whether all elements of expr is positive constant.
 */
bool IsAllPositiveConstant(const Expr& expr);

53 54 55 56 57 58 59 60 61 62
/*!
 * \brief Substitute var with subst.
 * \param type The type to be substituted.
 * \param tvar The type variable to be substituted.
 * \param subst The target of substitution.
 * \return The substituted result.
 */
Type TypeSubst(const Type& type, const TypeVar& tvar, const Type& subst);

/*!
雾雨魔理沙 committed
63 64 65 66 67 68 69 70 71
 * \brief Substitute var with subst.
 * \param expr The expr to be substituted.
 * \param tvar The type variable to be substituted.
 * \param subst The target of substitution.
 * \return The substituted result.
 */
Expr TypeSubst(const Expr& expr, const TypeVar& tvar, const Type& subst);

/*!
72 73 74 75 76 77 78
 * \brief Substitute type vars in type.
 * \param type The type to be substituted.
 * \param subst_map The map of substitution.
 * \return The substituted result.
 */
Type TypeSubst(const Type& type, const tvm::Map<TypeVar, Type>& subst_map);

雾雨魔理沙 committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
/*!
 * \brief Substitute type vars in type.
 * \param expr The expr to be substituted.
 * \param subst_map The map of substitution.
 * \return The substituted result.
 */
Expr TypeSubst(const Expr& expr, const tvm::Map<TypeVar, Type>& subst_map);

/*!
 * \brief Make arbitrary transformation preserve the out most function.
 * \param func The transformation.
 * \param e The expression
 * \return the transformed expression. If e is a function the return is also a function.
 */
inline Expr TransformF(const std::function<Expr(const Expr&)>& func, const Expr& e) {
  if (const FunctionNode* f = e.as<FunctionNode>()) {
    return FunctionNode::make(f->params, func(f->body), f->ret_type, f->type_params, f->attrs);
  } else {
    return func(e);
  }
}

101 102 103 104 105 106 107 108 109 110 111
/*!
 * \brief Decide whether the expression atomic or not?
 * \param e the expression
 * \return
 *   is it atomic?
 *   if so, the compute cost of the expression is bounded so it can be copy without graph mode.
 */
inline bool IsAtomic(const Expr& e) {
  return e.as<VarNode>() || e.as<OpNode>() || e.as<ConstructorNode>() || e.as<GlobalVarNode>();
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
template<typename ConditionNodePtr>
struct TreeNode {
  typedef std::shared_ptr<TreeNode<ConditionNodePtr>> pointer;
  virtual ~TreeNode() {}
};

template<typename ConditionNodePtr>
struct TreeLeafNode : TreeNode<ConditionNodePtr> {
  using TreeNodePtr = typename TreeNode<ConditionNodePtr>::pointer;

  Expr body;

  explicit TreeLeafNode(Expr body): body(body) {}

  static TreeNodePtr Make(Expr body) {
    return std::make_shared<TreeLeafNode>(body);
  }

  ~TreeLeafNode() {}
};

template<typename ConditionNodePtr>
struct TreeLeafFatalNode : TreeNode<ConditionNodePtr> {
  using TreeNodePtr = typename TreeNode<ConditionNodePtr>::pointer;

  TreeLeafFatalNode() = default;

  static TreeNodePtr Make() {
    return std::make_shared<TreeLeafFatalNode>();
  }

  ~TreeLeafFatalNode() {}
};

template<typename ConditionNodePtr>
struct TreeBranchNode : TreeNode<ConditionNodePtr> {
  using TreeNodePtr = typename TreeNode<ConditionNodePtr>::pointer;

  ConditionNodePtr cond;
  TreeNodePtr then_branch;
  TreeNodePtr else_branch;

  TreeBranchNode(ConditionNodePtr cond,
                 TreeNodePtr then_branch,
                 TreeNodePtr else_branch)
  : cond(cond), then_branch(then_branch), else_branch(else_branch) {}


  static TreeNodePtr Make(ConditionNodePtr cond,
                          TreeNodePtr then_branch,
                          TreeNodePtr else_branch) {
    return std::make_shared<TreeBranchNode>(cond, then_branch, else_branch);
  }

  ~TreeBranchNode() {}
};

169 170 171
}  // namespace relay
}  // namespace tvm
#endif  // TVM_RELAY_PASS_PASS_UTIL_H_