annotated_region_set.h 7.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
/*
 * 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 tvm/relay/pass/annotated_region_set.h
 * \brief Define data structures to extract and manipulate regions from
 * a relay function. Regions are denoted by region_begin and region_end
 * annotations that exist on all the input and output edges of the region.
 */

#ifndef TVM_RELAY_ANALYSIS_ANNOTATED_REGION_SET_H_
#define TVM_RELAY_ANALYSIS_ANNOTATED_REGION_SET_H_

#include <tvm/relay/analysis.h>
#include <tvm/relay/attrs/annotation.h>
#include <tvm/relay/expr.h>
#include <tvm/ir/error.h>
#include <tvm/relay/expr_functor.h>
#include <tvm/relay/transform.h>

#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
#include <list>

namespace tvm {
namespace relay {

class AnnotatedRegion;
class AnnotatedRegionSet;

class AnnotatedRegionNode : public Object {
 public:
  void VisitAttrs(AttrVisitor* v) {
    v->Visit("id", &id);
    Array<Expr> nodes_array(nodes.begin(), nodes.end());
    v->Visit("nodes", &nodes_array);
    Array<Expr> args_array(ins.begin(), ins.end());
    v->Visit("args", &args_array);
    Array<Expr> rets_array(outs.begin(), outs.end());
    v->Visit("rets", &rets_array);
  }

  /*! \brief Get the region ID. */
  int GetID() const {
    return id;
  }

  /*! \brief Get the region's inputs. */
  std::list<Expr> GetInputs() const {
    return ins;
  }

  /*! \brief Get the region's outputs. */
  std::list<Expr> GetOutputs() const {
    return outs;
  }

  /*! \brief Get the region's nodes. */
  std::unordered_set<Expr, ObjectHash, ObjectEqual> GetNodes() const {
    return nodes;
  }

  static constexpr const char* _type_key = "relay.AnnotatedRegion";
  TVM_DECLARE_FINAL_OBJECT_INFO(AnnotatedRegionNode, Object);

 protected:
  /*! \brief The region ID. */
  int id{-1};
  /*! \brief The inputs to this region. */
  std::list<Expr> ins;
  /*! \brief The outputs of this region */
  std::list<Expr> outs;
  /*! \brief Nodes in this region. */
  std::unordered_set<Expr, ObjectHash, ObjectEqual> nodes;

  friend class AnnotatedRegionSet;
  friend class AnnotatedRegionSetNode;
};

/*!
 * \brief An object to hold the properties of a region as used by the
 * AnnotatedRegionSet class. This should be considered read-only.
*/
class AnnotatedRegion : public ObjectRef {
 public:
  AnnotatedRegion() {
    auto n = make_object<AnnotatedRegionNode>();
    data_ = std::move(n);
  }

  /*!
 * \brief Construct from an object pointer.
 * \param n The object pointer.
 */
  explicit AnnotatedRegion(ObjectPtr<Object> n) : ObjectRef(n) {}

  /*! \return Mutable pointers to the node. */
  AnnotatedRegionNode* operator->() const {
    auto* ptr = get_mutable();
    CHECK(ptr != nullptr);
    return static_cast<AnnotatedRegionNode*>(ptr);
  }
};

class AnnotatedRegionSetNode : public Object {
  using UnorderedRegionSet =
  std::unordered_set<AnnotatedRegion, ObjectHash, ObjectEqual>;
  // Create iterator alias for a RegionSet object.
  using iterator = UnorderedRegionSet::iterator;
  using const_iterator = UnorderedRegionSet::const_iterator;

 public:
  /*! \brief Default constructor. */
  AnnotatedRegionSetNode() = default;

  /*! \return The begin iterator */
  iterator begin() {
    return regions_.begin();
  }
  /*! \return The end iterator */
  iterator end() {
    return regions_.end();
  }
  /*! \return The const begin iterator */
  const_iterator begin() const {
    return regions_.begin();
  }
  /*! \return The const end iterator */
  const_iterator end() const {
    return regions_.end();
  }

  /*!
   * \brief Get the region that an expression belongs to.
   *
   * \param expr Which expr to get the region for.
   *
   * \return A pointer to the region, nullptr if the expression
   * doesn't belong to a region.
   */
  AnnotatedRegion GetRegion(const Expr& expr) const;

  /*!
 * \brief Merge src region into dest region.
 *
 * \param src The region to merge - will be erased.
 * \param dest The region into which src will be merged.
 */
  void MergeRegions(AnnotatedRegion src, AnnotatedRegion dest);

  void VisitAttrs(AttrVisitor* v) {
    Array<AnnotatedRegion> regions_array(regions_.begin(), regions_.end());
    v->Visit("regions", &regions_array);
  }

  static constexpr const char* _type_key = "relay.AnnotatedRegionSet";
  TVM_DECLARE_FINAL_OBJECT_INFO(AnnotatedRegionSetNode, Object);

 private:
  /*!
   * \brief Add an expression to a region.
   *
   * \param region The region to add the expression to.
   * \param expr The expression.
   */
  void AddToRegion(AnnotatedRegion region, const Expr& expr);

  /*!
   * \brief Make a new region.
   *
   * \return The new region.
   */
  AnnotatedRegion MakeRegion();

  std::unordered_set<AnnotatedRegion, ObjectHash, ObjectEqual> regions_;
  /*! \brief The next region ID to assign. */
  int region_id_{0};

  friend class AnnotatedRegionSet;
};

/*!
 * \brief A class to hold a set of regions produced from a relay expression
 * that contains 'region_begin' and 'region_end' style annotations. The
 * regions should be disjoint. The class provides both a method to construct
 * the region set of a given relay expression as well as additional methods
 * to update and query regions.
 */
class AnnotatedRegionSet : public ObjectRef {
  using UnorderedRegionSet =
    std::unordered_set<AnnotatedRegion, ObjectHash, ObjectEqual>;
  // Create iterator alias for a RegionSet object.
  using iterator = UnorderedRegionSet::iterator;
  using const_iterator = UnorderedRegionSet::const_iterator;

 public:
  AnnotatedRegionSet() {
    auto n = make_object<AnnotatedRegionSetNode>();
    data_ = std::move(n);
  }

  /*!
 * \brief Construct from an object pointer.
 *
 * \param n The object pointer.
 */
  explicit AnnotatedRegionSet(ObjectPtr<Object> n) : ObjectRef(n) {}

  /*! \return The begin iterator. */
  iterator begin() {
    auto* n = operator->();
    CHECK(n);
    return n->begin();
  }
  /*! \return The end iterator. */
  iterator end() {
    auto* n = operator->();
    CHECK(n);
    return n->end();
  }
  /*! \return The begin iterator. */
  const_iterator begin() const {
    const auto* n = operator->();
    CHECK(n);
    return n->begin();
  }
  /*! \return The end iterator. */
  const_iterator end() const {
    const auto *n = operator->();
    CHECK(n);
    return n->end();
  }

  /*! \return mutable pointers to the node. */
  AnnotatedRegionSetNode* operator->() const {
    auto* ptr = get_mutable();
    CHECK(ptr != nullptr);
    return static_cast<AnnotatedRegionSetNode*>(ptr);
  }

  /*! \return The region an expression belongs to. */
  AnnotatedRegion operator[](const Expr& expr) {
    const auto *n = operator->();
    CHECK(n);
    return n->GetRegion(expr);
  }

  /*! \brief Create a RegionSet from a relay expression.
   *
   * \param expr The relay expr from which to construct the set.
   * \param begin Region begin annotation operator.
   * \param end Region end annotation operator.
   *
   * \return The created RegionSet for the expression.
   */
  static AnnotatedRegionSet Create(const Expr& expr,
                                   const Op& begin,
                                   const Op& end);

 private:
  /*! \brief Helper class to construct a RegionSet from an expr.*/
  class Creator;
};

}  // namespace relay
}  // namespace tvm

#endif  // TVM_RELAY_ANALYSIS_ANNOTATED_REGION_SET_H_