object.h 10.8 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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
/*
 * 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.
 */

/*!
 *  Copyright (c) 2019 by Contributors
 * \file tvm/runtime/object.h
 * \brief A managed object in the TVM runtime.
 */
#ifndef TVM_RUNTIME_OBJECT_H_
#define TVM_RUNTIME_OBJECT_H_

#include <tvm/runtime/ndarray.h>
#include <memory>
#include <utility>
#include <vector>

namespace tvm {
namespace runtime {

template <typename T>
class ObjectPtr;
class Object;

enum struct ObjectTag {
  /*! \brief The tag of a tensor. */
  kTensor = 0U,
  /*! \brief The tag of a closure. */
  kClosure = 1U,
  /*! \brief The tag of a structure. */
  kDatatype = 2U,
};

std::ostream& operator<<(std::ostream& os, const ObjectTag&);

struct ObjectCell {
 public:
  /*!
   * \brief The type of object deleter.
   * \param The self pointer to the ObjectCell.
   */
  typedef void (*FDeleter)(ObjectCell* self);

  /*! \brief The tag of the object.
   *
   * Describes which type of value
   * is represented by this object.
   */
  ObjectTag tag;

  /*!
   * \brief Increment the reference count.
   */
  void IncRef() { ref_counter_.fetch_add(1, std::memory_order_relaxed); }

  /*!
   * \brief Decrement the reference count.
   */
  void DecRef() {
    if (ref_counter_.fetch_sub(1, std::memory_order_release) == 1) {
      std::atomic_thread_fence(std::memory_order_acquire);
      if (this->deleter_ != nullptr) {
        (*this->deleter_)(this);
      }
    }
  }

 protected:
  // default constructor and copy constructor
  ObjectCell() {}

  explicit ObjectCell(ObjectTag tag) : tag(tag) {}

  // override the copy and assign constructors to do nothing.
  // This is to make sure only contents, but not deleter and ref_counter
  // are copied when a child class copies itself.
  ObjectCell(const ObjectCell& other) {  // NOLINT(*)
  }

  ObjectCell(ObjectCell&& other) {  // NOLINT(*)
  }

  ObjectCell& operator=(const ObjectCell& other) {  // NOLINT(*)
    return *this;
  }

  ObjectCell& operator=(ObjectCell&& other) {  // NOLINT(*)
    return *this;
  }

 private:
  /*! \brief Internal reference counter */
  std::atomic<int> ref_counter_{0};
  /*!
   * \brief deleter of this object to enable customized allocation.
   * If the deleter is nullptr, no deletion will be performed.
   * The creator of the Node must always set the deleter field properly.
   */
  FDeleter deleter_ = nullptr;

  int use_count() const { return ref_counter_.load(std::memory_order_relaxed); }

  // friend declaration
  template <typename>
  friend class ObjectPtr;

  template <typename Y, typename... Args>
  friend ObjectPtr<Y> MakeObject(Args&&...);
};

/*!
 * \brief A custom smart pointer for Object.
 *  must be subclass of NodeBase
 * \tparam T the content data type.
 */
template <typename T>
class ObjectPtr {
 public:
  /*! \brief default constructor */
  ObjectPtr() {}
  /*! \brief default constructor */
  ObjectPtr(std::nullptr_t) {}  // NOLINT(*)
  /*!
   * \brief copy constructor
   * \param other The value to be moved
   */
  ObjectPtr(const ObjectPtr<T>& other)  // NOLINT(*)
      : ObjectPtr(other.data_) {}
  /*!
   * \brief copy constructor
   * \param other The value to be moved
   */
  template <typename U>
  ObjectPtr(const ObjectPtr<U>& other)  // NOLINT(*)
      : ObjectPtr(other.data_) {
    static_assert(std::is_base_of<T, U>::value,
                  "can only assign of child class ObjectPtr to parent");
  }
  /*!
   * \brief move constructor
   * \param other The value to be moved
   */
  ObjectPtr(ObjectPtr<T>&& other)  // NOLINT(*)
      : data_(other.data_) {
    other.data_ = nullptr;
  }

  /*!
   * \brief move constructor
   * \param other The value to be moved
   */
  template <typename Y>
  ObjectPtr(ObjectPtr<Y>&& other)  // NOLINT(*)
      : data_(other.data_) {
    static_assert(std::is_base_of<T, Y>::value,
                  "can only assign of child class ObjectPtr to parent");
    other.data_ = nullptr;
  }

  /*! \brief destructor */
  ~ObjectPtr() { this->reset(); }

  /*!
   * \brief Swap this array with another Object
   * \param other The other Object
   */
  void swap(ObjectPtr<T>& other) {  // NOLINT(*)
    std::swap(data_, other.data_);
  }

  /*!
   * \return Get the content of the pointer
   */
  T* get() const { return static_cast<T*>(data_); }

  /*!
   * \return The pointer
   */
  T* operator->() const { return get(); }

  /*!
   * \return The reference
   */
  T& operator*() const {  // NOLINT(*)
    return *get();
  }

  /*!
   * \brief copy assignmemt
   * \param other The value to be assigned.
   * \return reference to self.
   */
  ObjectPtr<T>& operator=(const ObjectPtr<T>& other) {  // NOLINT(*)
    // takes in plane operator to enable copy elison.
    // copy-and-swap idiom
    ObjectPtr(other).swap(*this);  // NOLINT(*)
    return *this;
  }

  /*!
   * \brief move assignmemt
   * \param other The value to be assigned.
   * \return reference to self.
   */
  ObjectPtr<T>& operator=(ObjectPtr<T>&& other) {  // NOLINT(*)
    // copy-and-swap idiom
    ObjectPtr(std::move(other)).swap(*this);  // NOLINT(*)
    return *this;
  }

  /*! \brief reset the content of ptr to be nullptr */
  void reset() {
    if (data_ != nullptr) {
      data_->DecRef();
      data_ = nullptr;
    }
  }

  /*! \return The use count of the ptr, for debug purposes */
  int use_count() const { return data_ != nullptr ? data_->use_count() : 0; }

  /*! \return whether the reference is unique */
  bool unique() const { return data_ != nullptr && data_->use_count() == 1; }

  /*! \return Whether two ObjectPtr do not equal each other */
  bool operator==(const ObjectPtr<T>& other) const { return data_ == other.data_; }

  /*! \return Whether two ObjectPtr equals each other */
  bool operator!=(const ObjectPtr<T>& other) const { return data_ != other.data_; }

  /*! \return Whether the pointer is nullptr */
  bool operator==(std::nullptr_t null) const { return data_ == nullptr; }

  /*! \return Whether the pointer is not nullptr */
  bool operator!=(std::nullptr_t null) const { return data_ != nullptr; }

  /* ObjectPtr's support custom allocators.
   *
   * The below allocator represents the simplest
   * possible impl. It can be easily swapped
   * for customized executor's, different allocation
   * strategies, and so on.
   *
   * See memory.h for more discussion on NodePtr's
   * allocator.
   */
  class StdAllocator {
   public:
    template <typename... Args>
    static T* New(Args&&... args) {
      return new T(std::forward<Args>(args)...);
    }

    static ObjectCell::FDeleter Deleter() { return Deleter_; }

   private:
    static void Deleter_(ObjectCell* ptr) { delete static_cast<T*>(ptr); }
  };

  template <typename U>
  ObjectPtr<U> As() const {
    auto ptr = reinterpret_cast<U*>(get());
    return ObjectPtr<U>(ptr);
  }

 private:
  /*! \brief internal pointer field */
  ObjectCell* data_{nullptr};
  /*!
   * \brief constructor from NodeBase
   * \param data The node base pointer
   */
  // TODO(jroesch): NodePtr design doesn't really work here due to the passing.
 public:
  explicit ObjectPtr(ObjectCell* data) : data_(data) {
    if (data != nullptr) {
      data_->IncRef();
    }
  }

 private:
  template <typename Y, typename... Args>
  friend ObjectPtr<Y> MakeObject(Args&&...);
  template <typename>
  friend class ObjectPtr;
  friend class NDArray;
  friend class TVMPODValue_;
  friend class TVMArgValue;
  friend class TVMRetValue;
  friend class RPCWrappedFunc;
};

struct TensorCell;
struct DatatypeCell;
struct ClosureCell;

/*!
 * \brief A managed object in the TVM runtime.
 *
 * For example a tuple, list, closure, and so on.
 *
 * Maintains a reference count for the object.
 */
class Object {
 public:
  ObjectPtr<ObjectCell> ptr_;
  explicit Object(ObjectPtr<ObjectCell> ptr) : ptr_(ptr) {}
  explicit Object(ObjectCell* ptr) : ptr_(ptr) {}
  Object() : ptr_() {}
  Object(const Object& obj) : ptr_(obj.ptr_) {}
  ObjectCell* operator->() { return this->ptr_.operator->(); }

  /*! \brief Construct a tensor object. */
  static Object Tensor(const NDArray& data);
  /*! \brief Construct a datatype object. */
  static Object Datatype(size_t tag, const std::vector<Object>& fields);
  /*! \brief Construct a tuple object. */
  static Object Tuple(const std::vector<Object>& fields);
  /*! \brief Construct a closure object. */
  static Object Closure(size_t func_index, const std::vector<Object>& free_vars);

  ObjectPtr<TensorCell> AsTensor() const;
  ObjectPtr<DatatypeCell> AsDatatype() const;
  ObjectPtr<ClosureCell> AsClosure() const;
};

/*! \brief An object containing an NDArray. */
struct TensorCell : public ObjectCell {
  /*! \brief The NDArray. */
  NDArray data;
  explicit TensorCell(const NDArray& data) : ObjectCell(ObjectTag::kTensor), data(data) {}
};

/*! \brief An object representing a structure or enumeration. */
struct DatatypeCell : public ObjectCell {
  /*! \brief The tag representing the constructor used. */
  size_t tag;
  /*! \brief The fields of the structure. */
  std::vector<Object> fields;

  DatatypeCell(size_t tag, const std::vector<Object>& fields)
      : ObjectCell(ObjectTag::kDatatype), tag(tag), fields(fields) {}
};

/*! \brief An object representing a closure. */
struct ClosureCell : public ObjectCell {
  /*! \brief The index into the VM function table. */
  size_t func_index;
  /*! \brief The free variables of the closure. */
  std::vector<Object> free_vars;

  ClosureCell(size_t func_index, const std::vector<Object>& free_vars)
      : ObjectCell(ObjectTag::kClosure), func_index(func_index), free_vars(free_vars) {}
};

/*! \brief Extract the NDArray from a tensor object. */
NDArray ToNDArray(const Object& obj);

/*!
 * \brief Allocate a node object.
 * \param args arguments to the constructor.
 * \tparam T the node type.
 * \return The NodePtr to the allocated object.
 */
template <typename T, typename... Args>
inline ObjectPtr<T> MakeObject(Args&&... args) {
  using Allocator = typename ObjectPtr<T>::StdAllocator;
  static_assert(std::is_base_of<ObjectCell, T>::value, "MakeObject can only be used to create ");
  T* node = Allocator::New(std::forward<Args>(args)...);
  node->deleter_ = Allocator::Deleter();
  return ObjectPtr<T>(node);
}

}  // namespace runtime
}  // namespace tvm
#endif  // TVM_RUNTIME_OBJECT_H_