buffer.h 5.14 KB
Newer Older
1 2
/*!
 *  Copyright (c) 2016 by Contributors
tqchen committed
3
 * \file tvm/buffer.h
4 5 6 7 8 9 10
 * \brief Symbolic n-dimensional array, to represent a memory buffer.
 */
#ifndef TVM_BUFFER_H_
#define TVM_BUFFER_H_

#include <string>

11 12
#include "base.h"
#include "expr.h"
13
#include "ir_operator.h"
14
#include "tvm/node/container.h"
15 16 17 18 19

namespace tvm {

// Internal node container Buffer
class BufferNode;
20

21 22 23 24 25 26
/*! \brief memory access kind */
enum class AccessMask : int {
  kRead = 1,
  kWrite = 2
};

27 28 29
/*!
 * \brief Buffer is a symbolic n-darray structure.
 *  It is a composition of primitive symbolic types,
30
 *  used to specify the memory layout of the Tensor used in program input.
31 32 33 34
 */
class Buffer : public NodeRef {
 public:
  Buffer() {}
35
  explicit Buffer(NodePtr<Node> n) : NodeRef(n) {}
36
  /*!
37 38 39 40
   * \brief Return a new buffer that is equivalent with current one
   *  but always add stride field.
   * \return The strided version of the buffer.
   */
41
  TVM_DLL Buffer MakeStrideView() const;
42 43 44 45 46 47 48 49
  /*!
   * \brief Make a new symbolic buffer representing a slice of the buffer.
   * \param begins The beginning position of each dimension.
   * \param extents The extent of each dimension.
   * \note This function will make target buffer as compact as possible.
   *  If stride is not needed in the slice, it won't be presented
   * \return the result buffer.
   */
50
  TVM_DLL Buffer MakeSlice(Array<Expr> begins, Array<Expr> extents) const;
51
  /*!
52 53 54
   * \brief Get access ptr to the entire buffer.
   * \param access_mask The access mask
   * \param ptr_type The type of the pointer.
55
   * \param content_lanes The number of lanes for the (data) type.
56
   * \param offset The offset of ptr.
57
   */
58
  TVM_DLL Expr access_ptr(int access_mask, Type ptr_type = Handle(),
59
                          int content_lanes = 1, Expr offset = make_const(Int(32), 0)) const;
60
  /*!
61 62 63 64
   * \brief Create an Expr that does a vector load at begin index.
   * \param begin The beginning index
   * \param dtype The data type to be loaded.
   */
65
  TVM_DLL Expr vload(Array<Expr> begin, Type dtype) const;
66 67 68 69 70
  /*!
   * \brief Create a Stmt that does a vector store at begin index.
   * \param begin The beginning index
   * \param value The value to be stored.
   */
71
  TVM_DLL Stmt vstore(Array<Expr> begin, Expr value) const;
72
  /*!
73 74 75 76
   * \brief access the internal node container
   * \return the pointer to the internal node container
   */
  inline const BufferNode* operator->() const;
77 78 79

  /*! \brief specify container node */
  using ContainerType = BufferNode;
80 81 82 83 84
};

/*! \brief Node to represent a buffer */
class BufferNode : public Node {
 public:
85
  // Data fields.
86 87 88 89
  /*!
   * \brief The pointer to the head of the data
   * \sa data_alignment The alignment of data in bytes.
   */
90
  Var data;
91 92
  /*! \brief data type in the content of the tensor */
  Type dtype;
93 94 95 96 97 98 99
  /*! \brief The shape of the buffer */
  Array<Expr> shape;
  /*!
   * \brief The strides of each dimension
   *  This can be an empty array, indicating array is contiguous
   */
  Array<Expr> strides;
100 101
  /*! \brief The offset in terms of number of dtype elements (including lanes) */
  Expr elem_offset;
102 103 104 105 106
  // Meta data
  /*! \brief optional name of the buffer */
  std::string name;
  /*! \brief storage scope of the buffer, if other than global */
  std::string scope;
107 108 109 110 111 112 113
  /*! \brief Alignment requirement of data pointer in bytes. */
  int data_alignment;
  /*!
   * \brief Factor of elem_offset field,
   *  elem_offset is guaranteed to be multiple of offset_factor.
   */
  int offset_factor;
114 115 116 117
  /*! \brief constructor */
  BufferNode() {}

  void VisitAttrs(AttrVisitor* v) final {
118
    v->Visit("data", &data);
119
    v->Visit("dtype", &dtype);
120 121
    v->Visit("shape", &shape);
    v->Visit("strides", &strides);
122
    v->Visit("elem_offset", &elem_offset);
123 124
    v->Visit("name", &name);
    v->Visit("scope", &scope);
125 126
    v->Visit("data_alignment", &data_alignment);
    v->Visit("offset_factor", &offset_factor);
127 128
  }

129 130 131 132 133
  /*! \return preferred index type for this buffer node */
  Type DefaultIndexType() const {
    return shape.size() != 0 ? shape[0].type() : Int(32);
  }

134 135
  // User can specify data_alignment and offset_factor to be 0
  // A default value will be picked.
136 137 138 139
  TVM_DLL static Buffer make(Var ptr,
                             Type dtype,
                             Array<Expr> shape,
                             Array<Expr> strides,
140
                             Expr elem_offset,
141 142 143 144
                             std::string name,
                             std::string scope,
                             int data_alignment,
                             int offset_factor);
145 146

  static constexpr const char* _type_key = "Buffer";
147
  TVM_DECLARE_NODE_TYPE_INFO(BufferNode, Node);
148 149 150 151 152 153
};

inline const BufferNode* Buffer::operator->() const {
  return static_cast<const BufferNode*>(node_.get());
}

154 155 156 157 158 159 160 161
/*!
 * \brief Construct a new buffer given shape, and dtype.
 * \param shape The shape of the buffer,
 * \param dtype The content data type.
 * \param name The name of the buffer
 * \return The created buffer.
 * \sa BufferNode::make for complete constructor.
 */
162 163 164
TVM_DLL Buffer decl_buffer(Array<Expr> shape,
                           Type dtype = Float(32),
                           std::string name = "buffer");
165 166
}  // namespace tvm
#endif  // TVM_BUFFER_H_