buffer.h 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.
 */

20
/*!
tqchen committed
21
 * \file tvm/buffer.h
22 23 24 25 26 27 28
 * \brief Symbolic n-dimensional array, to represent a memory buffer.
 */
#ifndef TVM_BUFFER_H_
#define TVM_BUFFER_H_

#include <string>

29 30
#include "base.h"
#include "expr.h"
31
#include "expr_operator.h"
32
#include "tvm/node/container.h"
33 34 35 36 37

namespace tvm {

// Internal node container Buffer
class BufferNode;
38

39 40 41 42 43 44
/*! \brief memory access kind */
enum class AccessMask : int {
  kRead = 1,
  kWrite = 2
};

45 46 47
/*!
 * \brief Buffer is a symbolic n-darray structure.
 *  It is a composition of primitive symbolic types,
48
 *  used to specify the memory layout of the Tensor used in program input.
49 50 51 52
 */
class Buffer : public NodeRef {
 public:
  Buffer() {}
53
  explicit Buffer(NodePtr<Node> n) : NodeRef(n) {}
54
  /*!
55 56 57 58
   * \brief Return a new buffer that is equivalent with current one
   *  but always add stride field.
   * \return The strided version of the buffer.
   */
59
  TVM_DLL Buffer MakeStrideView() const;
60 61 62 63 64 65 66 67
  /*!
   * \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.
   */
68
  TVM_DLL Buffer MakeSlice(Array<Expr> begins, Array<Expr> extents) const;
69
  /*!
70 71 72
   * \brief Get access ptr to the entire buffer.
   * \param access_mask The access mask
   * \param ptr_type The type of the pointer.
73
   * \param content_lanes The number of lanes for the (data) type.
74
   * \param offset The offset of ptr.
75
   */
76
  TVM_DLL Expr access_ptr(int access_mask, Type ptr_type = Handle(),
77
                          int content_lanes = 1, Expr offset = make_const(Int(32), 0)) const;
78
  /*!
79 80 81 82
   * \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.
   */
83
  TVM_DLL Expr vload(Array<Expr> begin, Type dtype) const;
84 85 86 87 88
  /*!
   * \brief Create a Stmt that does a vector store at begin index.
   * \param begin The beginning index
   * \param value The value to be stored.
   */
89
  TVM_DLL Stmt vstore(Array<Expr> begin, Expr value) const;
90
  /*!
91 92 93 94
   * \brief access the internal node container
   * \return the pointer to the internal node container
   */
  inline const BufferNode* operator->() const;
95 96 97

  /*! \brief specify container node */
  using ContainerType = BufferNode;
98 99 100 101 102
};

/*! \brief Node to represent a buffer */
class BufferNode : public Node {
 public:
103
  // Data fields.
104 105 106 107
  /*!
   * \brief The pointer to the head of the data
   * \sa data_alignment The alignment of data in bytes.
   */
108
  Var data;
109 110
  /*! \brief data type in the content of the tensor */
  Type dtype;
111 112 113 114 115 116 117
  /*! \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;
118 119
  /*! \brief The offset in terms of number of dtype elements (including lanes) */
  Expr elem_offset;
120 121 122 123 124
  // Meta data
  /*! \brief optional name of the buffer */
  std::string name;
  /*! \brief storage scope of the buffer, if other than global */
  std::string scope;
125 126 127 128 129 130 131
  /*! \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;
132 133 134 135
  /*! \brief constructor */
  BufferNode() {}

  void VisitAttrs(AttrVisitor* v) final {
136
    v->Visit("data", &data);
137
    v->Visit("dtype", &dtype);
138 139
    v->Visit("shape", &shape);
    v->Visit("strides", &strides);
140
    v->Visit("elem_offset", &elem_offset);
141 142
    v->Visit("name", &name);
    v->Visit("scope", &scope);
143 144
    v->Visit("data_alignment", &data_alignment);
    v->Visit("offset_factor", &offset_factor);
145 146
  }

147 148 149 150 151
  /*! \return preferred index type for this buffer node */
  Type DefaultIndexType() const {
    return shape.size() != 0 ? shape[0].type() : Int(32);
  }

152 153
  // User can specify data_alignment and offset_factor to be 0
  // A default value will be picked.
154 155 156 157
  TVM_DLL static Buffer make(Var ptr,
                             Type dtype,
                             Array<Expr> shape,
                             Array<Expr> strides,
158
                             Expr elem_offset,
159 160 161 162
                             std::string name,
                             std::string scope,
                             int data_alignment,
                             int offset_factor);
163 164

  static constexpr const char* _type_key = "Buffer";
165
  TVM_DECLARE_NODE_TYPE_INFO(BufferNode, Node);
166 167 168 169 170 171
};

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

172 173 174 175 176 177 178 179
/*!
 * \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.
 */
180 181 182
TVM_DLL Buffer decl_buffer(Array<Expr> shape,
                           Type dtype = Float(32),
                           std::string name = "buffer");
183 184
}  // namespace tvm
#endif  // TVM_BUFFER_H_