MNN  1.0
Tensor.hpp
浏览该文件的文档.
1 //
2 // Tensor.hpp
3 // MNN
4 //
5 // Created by MNN on 2018/08/14.
6 // Copyright © 2018, Alibaba Group Holding Limited
7 //
8 
9 #ifndef Tensor_hpp
10 #define Tensor_hpp
11 
12 #include <vector>
13 #include "HalideRuntime.h"
14 #include "MNNDefine.h"
15 
16 namespace MNN {
17 
26 public:
27  struct InsideDescribe;
28 
36  CAFFE_C4
37  };
38 
42  HANDLE_NONE = 0,
44  HANDLE_STRING = 1
45  };
46 
50  NO_REORDER = 0,
52  REORDER_4 = 1,
54  REORDER_8
55  };
56 
57 public:
63  Tensor(int dimSize = 4, DimensionType type = CAFFE);
64 
72  Tensor(const Tensor* tensor, DimensionType type = CAFFE, bool allocMemory = true);
73 
75  ~Tensor();
76 
77 private:
78  // remove all assignment operator
79  Tensor(const Tensor& tensor) = delete;
80  Tensor(const Tensor&& tensor) = delete;
81  Tensor& operator=(const Tensor&) = delete;
82  Tensor& operator=(const Tensor&&) = delete;
83 
84 public:
93  static Tensor* createDevice(const std::vector<int>& shape, halide_type_t type, DimensionType dimType = TENSORFLOW);
94 
102  template <typename T>
103  static Tensor* createDevice(const std::vector<int>& shape, DimensionType dimType = TENSORFLOW) {
104  return createDevice(shape, halide_type_of<T>(), dimType);
105  }
106 
115  static Tensor* create(const std::vector<int>& shape, halide_type_t type, void* data = NULL,
116  DimensionType dimType = TENSORFLOW);
117 
125  template <typename T>
126  static Tensor* create(const std::vector<int>& shape, void* data = NULL, DimensionType dimType = TENSORFLOW) {
127  return create(shape, halide_type_of<T>(), data, dimType);
128  }
129 
130 public:
136  bool copyFromHostTensor(const Tensor* hostTensor);
137 
143  bool copyToHostTensor(Tensor* hostTensor) const;
144 
151  static Tensor* createHostTensorFromDevice(const Tensor* deviceTensor, bool copyData = true);
152 
153 public:
154  const halide_buffer_t& buffer() const {
155  return mBuffer;
156  }
158  return mBuffer;
159  }
160 
165  DimensionType getDimensionType() const;
166 
171  HandleDataType getHandleDataType() const;
172 
177  void setType(int type);
178 
183  inline halide_type_t getType() const {
184  return mBuffer.type;
185  }
186 
191  template <typename T>
192  T* host() const {
193  return (T*)mBuffer.host;
194  }
195 
200  uint64_t deviceId() const {
201  return mBuffer.device;
202  }
203 
204 public:
205  int dimensions() const {
206  return mBuffer.dimensions;
207  }
208 
213  std::vector<int> shape() const;
214 
219  int size() const;
220 
225  inline int elementSize() const {
226  return size() / mBuffer.type.bytes();
227  }
228 
229 public:
230  // for CAFFE tensors only.
231  inline int width() const {
232  return mBuffer.dim[3].extent;
233  }
234  inline int height() const {
235  return mBuffer.dim[2].extent;
236  }
237  inline int channel() const {
238  return mBuffer.dim[1].extent;
239  }
240  inline int batch() const {
241  return mBuffer.dim[0].extent;
242  }
243 
244  // for TENSORFLOW tensors only.
245  inline int tfWidth() const {
246  return mBuffer.dim[2].extent;
247  }
248  inline int tfHeight() const {
249  return mBuffer.dim[1].extent;
250  }
251  inline int tfChannel() const {
252  return mBuffer.dim[3].extent;
253  }
254  inline int tfBatch() const {
255  return mBuffer.dim[0].extent;
256  }
257 
258  // visit dimension's extent & stride
259  inline int stride(int index) const {
260  return mBuffer.dim[index].stride;
261  }
262  inline int length(int index) const {
263  return mBuffer.dim[index].extent;
264  }
265  inline void setStride(int index, int stride) {
266  mBuffer.dim[index].stride = stride;
267  }
268  inline void setLength(int index, int length) {
269  mBuffer.dim[index].extent = length;
270  }
271 
272 public:
276  void print() const;
277 
278 private:
279  halide_buffer_t mBuffer;
280  struct InsideDescribe* mDescribe;
281 
282 private:
283  friend class TensorUtils;
284 };
285 } // namespace MNN
286 
287 #endif /* Tensor_hpp */
int elementSize() const
calculate number of elements needed to store data taking reordering flag into account.
Definition: Tensor.hpp:225
int batch() const
Definition: Tensor.hpp:240
static Tensor * createDevice(const std::vector< int > &shape, DimensionType dimType=TENSORFLOW)
create tensor with shape and dimension type. data type is represented by T.
Definition: Tensor.hpp:103
Definition: Tensor.hpp:34
DataReorderType
Definition: Tensor.hpp:48
int tfBatch() const
Definition: Tensor.hpp:254
Definition: Tensor.hpp:32
int width() const
Definition: Tensor.hpp:231
halide_buffer_t & buffer()
Definition: Tensor.hpp:157
int length(int index) const
Definition: Tensor.hpp:262
void setLength(int index, int length)
Definition: Tensor.hpp:268
int height() const
Definition: Tensor.hpp:234
int tfWidth() const
Definition: Tensor.hpp:245
int stride(int index) const
Definition: Tensor.hpp:259
Definition: Tensor.hpp:25
T * host() const
visit host memory, data type is represented by T.
Definition: Tensor.hpp:192
#define MNN_PUBLIC
Definition: MNNDefine.h:53
const halide_buffer_t & buffer() const
Definition: Tensor.hpp:154
DimensionType
Definition: Tensor.hpp:30
int tfChannel() const
Definition: Tensor.hpp:251
Definition: HalideRuntime.h:82
halide_type_t getType() const
get data type.
Definition: Tensor.hpp:183
Definition: AutoTime.hpp:16
void setStride(int index, int stride)
Definition: Tensor.hpp:265
int dimensions() const
Definition: Tensor.hpp:205
int channel() const
Definition: Tensor.hpp:237
int tfHeight() const
Definition: Tensor.hpp:248
Definition: HalideRuntime.h:203
uint64_t deviceId() const
visit device memory.
Definition: Tensor.hpp:200
static Tensor * create(const std::vector< int > &shape, void *data=NULL, DimensionType dimType=TENSORFLOW)
create tensor with shape, data and dimension type. data type is represented by T.
Definition: Tensor.hpp:126
HandleDataType
Definition: Tensor.hpp:40