tensor.h 7.66 KB
Newer Older
tqchen committed
1 2
/*!
 *  Copyright (c) 2016 by Contributors
tqchen committed
3
 * \file tvm/tensor.h
tqchen committed
4 5 6 7 8
 * \brief Dataflow tensor object
 */
#ifndef TVM_TENSOR_H_
#define TVM_TENSOR_H_

tqchen committed
9
#include <ir/FunctionBase.h>
10
#include <tvm/node/container.h>
tqchen committed
11
#include <string>
tqchen committed
12
#include <vector>
tqchen committed
13
#include <type_traits>
tqchen committed
14

15 16
#include "base.h"
#include "expr.h"
17
#include "ir_operator.h"
18
#include "arithmetic.h"
tqchen committed
19 20 21

namespace tvm {

22 23
// Internal node container of Tensor
class TensorNode;
tqchen committed
24 25
// internal node container for Operation
class OperationNode;
26

27
using HalideIR::IR::FunctionRef;
28

tqchen committed
29 30 31 32
/*!
 * \brief Tensor structure representing a possible input,
 *  or intermediate computation result.
 */
33
class Tensor : public NodeRef {
34
 public:
tqchen committed
35 36
  /*! \brief default constructor, used internally */
  Tensor() {}
37
  explicit Tensor(NodePtr<Node> n) : NodeRef(n) {}
tqchen committed
38
  /*!
39 40 41 42
   * \brief access the internal node container
   * \return the pointer to the internal node container
   */
  inline const TensorNode* operator->() const;
43 44 45 46 47 48
  /*!
   * \brief check if two tensors equals each other.
   * \param other tensor to be checked.
   * \return whether the two tensors equals each other.
   */
  inline bool operator==(const Tensor& other) const;
49 50 51 52 53 54
  /*!
   * \brief check if two tensors are different.
   * \param other tensor to be checked.
   * \return whether the two tensors are different.
   */
  inline bool operator!=(const Tensor& other) const;
tqchen committed
55
  /*! \return The dimension of the tensor */
56
  inline size_t ndim() const;
tqchen committed
57 58 59 60 61
  /*!
   * \brief Take elements from the tensor
   * \param args The indices
   * \return the result expression representing tensor read.
   */
62 63 64
  template<typename... Args>
  inline Expr operator()(Args&& ...args) const {
    Array<Expr> indices{std::forward<Args>(args)...};
tqchen committed
65 66 67 68 69 70 71
    return operator()(indices);
  }
  /*!
   * \brief Take elements from the tensor
   * \param indices the indices.
   * \return the result expression representing tensor read.
   */
72
  TVM_DLL Expr operator()(Array<Expr> indices) const;
73
  /*!
74 75 76 77
   * \brief Take elements from the tensor
   * \param indices the indices.
   * \return the result expression representing tensor read.
   */
78
  TVM_DLL Expr operator()(Array<Var> indices) const;
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
   * \brief data structure to represent a slice that fixes first k coordinates.
   *  This is used to enable syntax sugar of Tensor[x][y][z] to get the element.
   */
  class Slice {
   public:
    // construct via tensor and indices
    Slice(const Tensor& tensor, std::vector<Expr> indices)
        : tensor_(tensor), indices_(indices) {}
    /*!
     * \brief get i-th slice from the current slice.
     * \param i the index of the coordinate
     * \return the subsequent slice.
     */
    inline Slice operator[](Expr i) {
      std::vector<Expr> other = indices_;
      other.emplace_back(i);
      return Slice(tensor_, other);
    }
    /*!
     * \brief Convert slice to expression.
     *  This is only valid when all the coordinates are fully specified.
     * \return the corresponding expression of this slice.
     */
    inline operator Expr() const {
      return tensor_(indices_);
    }

   private:
    const Tensor& tensor_;
    std::vector<Expr> indices_;
  };
  /*!
   * \brief get i-th slice from the current Tensor.
   * \param i the index of the coordinate
   * \return the subsequent slice.
   */
  inline Slice operator[](Expr i) const {
    return Slice(*this, {i});
  }
tqchen committed
119 120
  /*! \brief specify container node */
  using ContainerType = TensorNode;
121 122
};

tqchen committed
123
/*! \brief Operation that produces tensors */
124
class Operation : public FunctionRef {
tqchen committed
125 126 127
 public:
  /*! \brief default constructor  */
  Operation() {}
128
  explicit Operation(NodePtr<Node> n) : FunctionRef(n) {}
tqchen committed
129 130 131 132 133 134 135 136 137 138
  /*!
   * \brief access the internal node container
   * \return the pointer to the internal node container
   */
  inline const OperationNode* operator->() const;
  /*!
   * \brief get the i-th output of the operation.
   * \param i the output index.
   * \return The i-th output.
   */
139
  TVM_DLL Tensor output(size_t i) const;
tqchen committed
140 141
  /*! \brief specify container node */
  using ContainerType = OperationNode;
tqchen committed
142
};
tqchen committed
143

144
/*! \brief Node to represent a tensor */
145
class TensorNode : public Node {
146
 public:
tqchen committed
147 148
  /*! \brief The shape of the tensor */
  Array<Expr> shape;
149
  /*! \brief data type in the content of the tensor */
150
  Type dtype;
tqchen committed
151
  /*! \brief the source operation, can be None */
tqchen committed
152
  Operation op;
tqchen committed
153
  /*! \brief the output index from source operation */
tqchen committed
154
  int value_index{0};
155 156
  /*! \brief constructor */
  TensorNode() {}
157

158
  void VisitAttrs(AttrVisitor* v) final {
tqchen committed
159
    v->Visit("shape", &shape);
160
    v->Visit("dtype", &dtype);
tqchen committed
161 162
    v->Visit("op", &op);
    v->Visit("value_index", &value_index);
163
  }
164 165 166 167
  TVM_DLL static Tensor make(Array<Expr> shape,
                             Type dtype,
                             Operation op,
                             int value_index);
168 169

  static constexpr const char* _type_key = "Tensor";
170
  TVM_DECLARE_NODE_TYPE_INFO(TensorNode, Node);
tqchen committed
171 172
};

tqchen committed
173 174

// Implementations of inline functions
175 176 177 178 179 180 181 182
inline const TensorNode* Tensor::operator->() const {
  return static_cast<const TensorNode*>(node_.get());
}

inline size_t Tensor::ndim() const {
  return (*this)->shape.size();
}

183 184 185 186 187 188 189 190 191 192 193
inline bool Tensor::operator==(const Tensor& other) const {
  if (get() == other.get()) return true;
  if (get() == nullptr || other.get() == nullptr) return false;
  if ((*this)->op.defined() || other->op.defined()) {
    return (*this)->op == other->op &&
        (*this)->value_index == other->value_index;
  } else {
    return false;
  }
}

194 195 196 197
inline bool Tensor::operator!=(const Tensor& other) const {
  return !(*this == other);
}

198 199 200 201
// macro to turn every operation of slice to expression
#define DEFINE_OVERLOAD_SLICE_UNARY_OP(Op)                              \
  inline Expr operator Op (const Tensor::Slice& a) {                    \
    return Op a.operator Expr() ;                                       \
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

#define DEFINE_OVERLOAD_SLICE_BINARY_OP(Op)                             \
  template<typename T>                                                  \
  inline Expr operator Op (const Tensor::Slice& a, const T& b) {        \
    return a.operator Expr() Op b;                                      \
  }                                                                     \
  template<typename T>                                                  \
  inline Expr operator Op (const T& a, const Tensor::Slice& b) {        \
    return a Op b.operator Expr();                                      \
  }                                                                     \
  inline Expr operator Op (const Tensor::Slice& a, const Tensor::Slice& b) { \
    return a.operator Expr() Op b.operator Expr();                      \
  }

DEFINE_OVERLOAD_SLICE_UNARY_OP(!);
DEFINE_OVERLOAD_SLICE_UNARY_OP(-);
DEFINE_OVERLOAD_SLICE_BINARY_OP(+);
DEFINE_OVERLOAD_SLICE_BINARY_OP(-);
DEFINE_OVERLOAD_SLICE_BINARY_OP(*);
DEFINE_OVERLOAD_SLICE_BINARY_OP(/);
DEFINE_OVERLOAD_SLICE_BINARY_OP(%);
DEFINE_OVERLOAD_SLICE_BINARY_OP(==);
DEFINE_OVERLOAD_SLICE_BINARY_OP(<=);
DEFINE_OVERLOAD_SLICE_BINARY_OP(>=);
DEFINE_OVERLOAD_SLICE_BINARY_OP(!=);
DEFINE_OVERLOAD_SLICE_BINARY_OP(&&);
DEFINE_OVERLOAD_SLICE_BINARY_OP(||);
DEFINE_OVERLOAD_SLICE_BINARY_OP(>>);
DEFINE_OVERLOAD_SLICE_BINARY_OP(<<);
DEFINE_OVERLOAD_SLICE_BINARY_OP(>);  // NOLINT(*)
DEFINE_OVERLOAD_SLICE_BINARY_OP(<);  // NOLINT(*)

tqchen committed
235
}  // namespace tvm
236 237 238 239 240 241 242 243

namespace std {
template <>
struct hash<::tvm::Operation> {
  std::size_t operator()(const ::tvm::Operation& k) const {
    return k.hash();
  }
};
244 245 246 247 248 249 250 251 252 253 254 255

template <>
struct hash<::tvm::Tensor> {
  std::size_t operator()(const ::tvm::Tensor& k) const {
    if (k.defined() && k->op.defined()) {
      return k->op.hash();
    } else{
      return k.hash();
    }
  }
};
}  // namespace std
tqchen committed
256
#endif  // TVM_TENSOR_H_