mxnet
tensor_blob.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
28 #ifndef MXNET_TENSOR_BLOB_H_
29 #define MXNET_TENSOR_BLOB_H_
30 
31 #include <dmlc/logging.h>
32 #include <dmlc/json.h>
33 #include <dlpack/dlpack.h>
34 #include <vector>
35 #include <iostream>
36 #include <utility>
37 #include <algorithm>
38 #include "./base.h"
39 
40 namespace mxnet {
41 
42 // redefine DLPack enumeration to be backward compatible.
43 constexpr const int kCPU = kDLCPU;
44 constexpr const int kGPU = kDLGPU;
45 // extension type code under TVM function.
46 // Currently NNVM reserved 16 to 19 type code from TVM
47 // 16, 17, 18 is used by NNVM compiler already.
48 // Pick code 19 for MXNet NDArray
49 constexpr const int kTVMNDArrayTypeCode = 19;
50 
51 /* Forward declaration for friend declaration in TBlob */
52 class NDArray;
53 
66 class TBlob {
67  friend class NDArray;
68  public:
70  void *dptr_;
75 
77  TBlob(void)
78  : dptr_(nullptr),
79  type_flag_(mshadow::DataType<real_t>::kFlag) {
80  SetDLTensor(cpu::kDevMask, 0);
81  }
89  template<typename DType>
90  TBlob(DType *dptr, const mxnet::TShape &shape, int dev_mask, int dev_id = -1)
91  : dptr_(dptr), shape_(shape),
92  type_flag_(mshadow::DataType<DType>::kFlag) {
93  SetDLTensor(dev_mask, dev_id);
94  }
103  TBlob(void *dptr, const mxnet::TShape &shape, int dev_mask, int type_flag, int dev_id = -1)
104  : dptr_(dptr), shape_(shape), type_flag_(type_flag) {
105  SetDLTensor(dev_mask, dev_id);
106  }
111  explicit TBlob(const DLTensor &dltensor)
112  : dptr_(dltensor.data),
113  shape_(mxnet::TShape(dltensor.shape, dltensor.shape + dltensor.ndim)),
114  type_flag_(DLDataTypeTransform(dltensor.dtype)),
115  dltensor_(dltensor) {
116  // compactness check for DLTensor
117  if (dltensor.strides != nullptr) {
118  // check strides
119  const int &ndim = dltensor.ndim;
120  const int64_t *shape = dltensor.shape;
121  const int64_t *strides = dltensor.strides;
122  if (ndim >= 1) {
123  bool err = false;
124  if (strides[ndim - 1] != 1) {
125  err = true;
126  } else {
127  for (int i = ndim - 2; i >= 0; --i) {
128  if (strides[i] != shape[i + 1] * strides[i + 1]) {
129  err = true;
130  break;
131  }
132  }
133  }
134  if (err) {
135  LOG(FATAL) << "Unsupported DLPack because MXNet only support compact tensor now";
136  }
137  }
138  }
139  }
147  template<typename Device, int dim, typename DType>
148  TBlob(const mshadow::Tensor<Device, dim, DType> &src) { // NOLINT(*)
149  *this = src;
150  }
155  TBlob(const TBlob &src): dptr_(src.dptr_), shape_(src.shape_), type_flag_(src.type_flag_) {
156  this->SetDLTensor(src.dev_mask(), src.dev_id());
157  }
166  template<typename Device, int dim, typename DType>
168  dptr_ = src.dptr_;
169  shape_ = src.shape_;
170  type_flag_ = mshadow::DataType<DType>::kFlag;
171  SetDLTensor(Device::kDevMask, -1);
172  return *this;
173  }
179  inline TBlob &operator=(const TBlob &src) {
180  dptr_ = src.dptr_;
181  shape_ = src.shape_;
182  type_flag_ = src.type_flag_;
183  SetDLTensor(src.dev_mask(), src.dev_id());
184  return *this;
185  }
189  inline bool CheckContiguous(void) const {
190  return true;
191  }
197  inline TBlob reshape(const mxnet::TShape& shape) const {
198  CHECK_EQ(this->shape_.Size(), shape.Size()) << "Shape size mismatch "
199  << this->shape_.Size() << " v.s. " << shape.Size();
200  TBlob ret(this->dptr_, shape, this->dev_mask(), this->type_flag_, this->dev_id());
201  return ret;
202  }
210  template<typename Device, typename DType>
212  mshadow::Stream<Device> *stream = nullptr) const {
213  CHECK(Device::kDevMask == this->dev_mask())
214  << "TBlob.get: device type do not match specified type";
215  CHECK(mshadow::DataType<DType>::kFlag == type_flag_)
216  << "TBlob.get_with_shape: data type do not match specified type."
217  << "Expected: " << mshadow::dtype_string(type_flag_)
219  return mshadow::Tensor<Device, 2, DType>(static_cast<DType*>(dptr_),
220  shape_.FlatTo2D(),
221  stream);
222  }
230  template<typename Device, typename DType>
232  mshadow::Stream<Device> *stream = nullptr) const {
233  return this->get_with_shape<Device, 1, DType>(
234  mshadow::Shape1(shape_.Size()), stream);
235  }
237  inline int ndim(void) const {
238  return shape_.ndim();
239  }
246  inline index_t size(index_t idx) const {
247  return shape_[idx];
248  }
250  inline size_t Size(void) const {
251  return shape_.Size();
252  }
254  template<typename DType>
255  inline DType* dptr() const {
256  CHECK(mshadow::DataType<DType>::kFlag == type_flag_)
257  << "TBlob.get_with_shape: data type do not match specified type."
258  << "Expected: " << mshadow::dtype_string(type_flag_)
260  return static_cast<DType*>(dptr_);
261  }
263  inline int dev_mask() const {
264  return dltensor_.ctx.device_type;
265  }
267  inline int dev_id() const {
268  return dltensor_.ctx.device_id;
269  }
274  inline const DLTensor& dltensor() const {
275  return dltensor_;
276  }
277 
287  template<typename Device, int dim, typename DType>
288  inline mshadow::Tensor<Device, dim, DType> get(mshadow::Stream<Device> *stream = nullptr) const {
289  CHECK(Device::kDevMask == this->dev_mask())
290  << "TBlob.get: device type do not match specified type";
291  return mshadow::Tensor<Device, dim, DType>(dptr<DType>(),
292  shape_.get<dim>(), shape_[shape_.ndim() - 1], stream);
293  }
304  template<typename Device, int dim, typename DType>
306  const mshadow::Shape<dim> &shape,
307  mshadow::Stream<Device> *stream = nullptr) const {
308  CHECK(Device::kDevMask == this->dev_mask())
309  << "TBlob.get: device type do not match specified type";
310  CHECK_EQ(this->CheckContiguous(), true) << "TBlob.get_reshape: must be contiguous";
311  CHECK_EQ(this->shape_.Size(), static_cast<size_t>(shape.Size()))
312  << "TBlob.get_with_shape: new and old shape do not match total elements";
313  return mshadow::Tensor<Device, dim, DType>(dptr<DType>(), shape,
314  shape[dim - 1], stream);
315  }
325  template<typename Device, typename DType>
327  int axis, mshadow::Stream<Device> *stream = nullptr) const {
328  return this->get_with_shape<Device, 3, DType>(
329  this->shape_.FlatTo3D(axis), stream);
330  }
341  template<typename Device, typename DType>
343  int axis_begin, int axis_end,
344  mshadow::Stream<Device> *stream = nullptr) const {
345  return this->get_with_shape<Device, 3, DType>(
346  this->shape_.FlatTo3D(axis_begin, axis_end), stream);
347  }
357  template<typename Device, int dim, typename DType>
359  mshadow::Stream<Device> *stream = nullptr) const {
360  mshadow::Shape<dim> shape;
361  shape[0] = 1;
362  // Pad higher dimensions in case dim > ndim()
363  for (int i = 0; i < dim - ndim(); ++i) {
364  shape[i] = 1;
365  }
366  // Collapse higher dimensions in case dim < ndim()
367  for (int i = 0; i < ndim() - dim + 1; ++i) {
368  shape[0] *= shape_[i];
369  }
370  // Preserve lower dimensions.
371  for (int i = std::max(0, ndim() - dim + 1); i < ndim(); ++i) {
372  shape[i - ndim() + dim] = shape_[i];
373  }
374  return this->get_with_shape<Device, dim, DType>(shape, stream);
375  }
376 
377  private:
378  static DLDataType DTypeTransform(int type_flag) {
379  switch (type_flag) {
380  case mshadow::kFloat32: return DLDataType{kDLFloat, 32, 1};
381  case mshadow::kFloat64: return DLDataType{kDLFloat, 64, 1};
382  case mshadow::kFloat16: return DLDataType{kDLFloat, 16, 1};
383  case mshadow::kBfloat16: return DLDataType{kDLBfloat, 16, 1};
384  case mshadow::kUint8: return DLDataType{kDLUInt, 8, 1};
385  case mshadow::kInt32: return DLDataType{kDLInt, 32, 1};
386  case mshadow::kInt8: return DLDataType{kDLInt, 8, 1};
387  case mshadow::kInt64: return DLDataType{kDLInt, 64, 1};
388  case mshadow::kBool: return DLDataType{kDLUInt, 1, 1};
389  default: {
390  LOG(FATAL) << "Unknown type_flag=" << type_flag;
391  return DLDataType();
392  }
393  }
394  }
395  static int DLDataTypeTransform(DLDataType dldata_type) {
396  if (dldata_type.lanes != 1) {
397  LOG(FATAL) << "Unsupported DLDataType whose lanes != 1";
398  }
399  switch (dldata_type.code) {
400  case kDLFloat:
401  switch (dldata_type.bits) {
402  case 16: return mshadow::kFloat16;
403  case 32: return mshadow::kFloat32;
404  case 64: return mshadow::kFloat64;
405  }
406  break;
407  case kDLBfloat:
408  switch (dldata_type.bits) {
409  case 16: return mshadow::kBfloat16;
410  }
411  break;
412  case kDLUInt:
413  switch (dldata_type.bits) {
414  case 1: return mshadow::kBool;
415  case 8: return mshadow::kUint8;
416  }
417  break;
418  case kDLInt:
419  switch (dldata_type.bits) {
420  case 8: return mshadow::kInt8;
421  case 32: return mshadow::kInt32;
422  case 64: return mshadow::kInt64;
423  }
424  break;
425  }
426  LOG(FATAL) << "Unknown DLDataType{" << dldata_type.code
427  << ", " << dldata_type.bits
428  << ", " << dldata_type.lanes << "}";
429  return mshadow::kFloat32;
430  }
431 
432  inline void SetDLTensor(int dev_mask, int dev_id) {
433  dltensor_.data = dptr_;
434  dltensor_.ctx = DLContext{static_cast<DLDeviceType>(dev_mask), dev_id};
435  dltensor_.ndim = shape_.ndim();
436  dltensor_.dtype = DTypeTransform(type_flag_);
437  dltensor_.shape = shape_.data();
438  dltensor_.strides = nullptr;
439  dltensor_.byte_offset = 0;
440  }
441 
442  private:
444  DLTensor dltensor_;
445 };
446 } // namespace mxnet
447 
448 namespace dmlc {
449 // Add a few patches to support mxnet::TShape in dmlc/parameter.
450 DMLC_DECLARE_TYPE_NAME(mxnet::TShape, "Shape(tuple)");
455 
456 namespace parameter {
457 
458 template<>
459 class FieldEntry<mxnet::TShape>
460  : public FieldEntryBase<FieldEntry<mxnet::TShape>, mxnet::TShape> {
461  public:
462  FieldEntry() : enforce_nonzero_(false), expect_ndim_(0) {}
463  // parent class
464  typedef FieldEntryBase<FieldEntry<mxnet::TShape>, mxnet::TShape> Parent;
465 
466  virtual void Check(void *head) const {
467  Parent::Check(head);
468  mxnet::TShape &v = this->Get(head);
469  if (expect_ndim_ != 0 && v.ndim() != expect_ndim_) {
470  std::ostringstream os;
471  os << "value " << v << "for Parameter " << this->key_
472  << " has wrong dimensions, expected dimension=" << expect_ndim_;
473  throw dmlc::ParamError(os.str());
474  }
475  if (enforce_nonzero_) {
476  for (int i = 0; i < v.ndim(); ++i) {
477  if (v[i] == 0U) {
478  std::ostringstream os;
479  os << "value " << v << "for Parameter " << this->key_
480  << " is invalid, the input shape must be nonzero in all dimensions";
481  throw dmlc::ParamError(os.str());
482  }
483  }
484  }
485  }
487  this->enforce_nonzero_ = true;
488  return this->self();
489  }
491  expect_ndim_ = ndim;
492  return this->self();
493  }
494 
495  private:
496  // whether all the entries need to be nonzero
497  bool enforce_nonzero_;
498  // expected number of dimension, default = 0 means no restriction.
499  int expect_ndim_;
500 };
501 
502 } // namespace parameter
503 } // namespace dmlc
504 
505 #endif // MXNET_TENSOR_BLOB_H_
#define DMLC_DECLARE_TYPE_NAME(Type, Name)
macro to quickly declare traits information
Definition: type_traits.h:133
Definition: base.h:352
TBlob & operator=(const mshadow::Tensor< Device, dim, DType > &src)
assignment from tensor
Definition: tensor_blob.h:167
Definition: dlpack.h:83
The common header of DLPack.
Definition: dlpack.h:81
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:145
mxnet::TShape shape_
shape of the tensor
Definition: tensor_blob.h:72
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:51
constexpr const int kTVMNDArrayTypeCode
Definition: tensor_blob.h:49
DType * dptr_
pointer to the data
Definition: tensor.h:435
TBlob(const DLTensor &dltensor)
constructor that construct TBlob from DLTensor
Definition: tensor_blob.h:111
c++17 compatible optional class.
Definition: optional.h:43
mshadow::Tensor< Device, 3, DType > FlatTo3D(int axis_begin, int axis_end, mshadow::Stream< Device > *stream=nullptr) const
flatten the tensor to 3 dimension, collapse the dimension: [0, axis_begin), [axis_begin, axis_end], (axis_end, ndim).
Definition: tensor_blob.h:342
namespace of mxnet
Definition: api_registry.h:33
mshadow::Tensor< Device, 2, DType > FlatTo2D(mshadow::Stream< Device > *stream=nullptr) const
flatten the tensor to 2 dimension, collapse the higher dimensions together
Definition: tensor_blob.h:211
A Device context for Tensor and operator.
Definition: dlpack.h:69
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:437
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:97
TBlob(void)
default constructor, default copy assign will work
Definition: tensor_blob.h:77
int type_flag_
type flag of the tensor blob
Definition: tensor_blob.h:74
FieldEntry< mxnet::TShape > & set_expect_ndim(int ndim)
Definition: tensor_blob.h:490
FieldEntry< mxnet::TShape > & enforce_nonzero()
Definition: tensor_blob.h:486
FieldEntryBase< FieldEntry< mxnet::TShape >, mxnet::TShape > Parent
Definition: tensor_blob.h:464
const dim_t * data() const
Definition: tuple.h:550
Definition: dlpack.h:80
TBlob(void *dptr, const mxnet::TShape &shape, int dev_mask, int type_flag, int dev_id=-1)
constructor that construct TBlob from contiguous memory
Definition: tensor_blob.h:103
Definition: dlpack.h:82
uint8_t code
Type code of base types. We keep it uint8_t instead of DLDataTypeCode for minimal memory footprint...
Definition: dlpack.h:100
int device_id
The device index.
Definition: dlpack.h:73
mshadow::Tensor< Device, 3, DType > FlatTo3D(int axis, mshadow::Stream< Device > *stream=nullptr) const
flatten the tensor to 3 dimension, collapse the dimension before and after specified axis...
Definition: tensor_blob.h:326
constexpr const int kGPU
Definition: tensor_blob.h:44
Lightweight JSON Reader/Writer that read save into C++ data structs. This includes STL composites and...
CPU device.
Definition: dlpack.h:40
index_t size(index_t idx) const
return size of i-th dimension, start counting from highest dimension. return type needs to be a signe...
Definition: tensor_blob.h:246
size_t Size() const
Definition: tuple.h:521
void * dptr_
pointer to the data
Definition: tensor_blob.h:70
std::string dtype_string(const int dtype)
Definition: base.h:1479
namespace for dmlc
Definition: array_view.h:12
uint8_t bits
Number of bits, common choices are 8, 16, 32.
Definition: dlpack.h:104
int64_t * strides
strides of the tensor (in number of elements, not bytes) can be NULL, indicating tensor is compact an...
Definition: dlpack.h:145
Definition: base.h:357
DLDataType dtype
The data type of the pointer.
Definition: dlpack.h:138
DType * dptr() const
get pointer in dtype
Definition: tensor_blob.h:255
DLDeviceType
The device type in DLContext.
Definition: dlpack.h:38
mshadow::Tensor< Device, dim, DType > get_with_shape(const mshadow::Shape< dim > &shape, mshadow::Stream< Device > *stream=nullptr) const
fetch a tensor in given shape If size do not match the stored size, an error will be issued ...
Definition: tensor_blob.h:305
Definition: base.h:359
int ndim(void) const
return number of dimension of the tensor inside
Definition: tensor_blob.h:237
Definition: base.h:353
DLDeviceType device_type
The device type used in the device.
Definition: dlpack.h:71
TBlob reshape(const mxnet::TShape &shape) const
reshape to shape
Definition: tensor_blob.h:197
mshadow::Tensor< Device, dim, DType > FlatToKD(mshadow::Stream< Device > *stream=nullptr) const
flatten the tensor to specified number of dimensions, collapse the highest dimensions or pad with hig...
Definition: tensor_blob.h:358
MSHADOW_XINLINE Shape< 1 > Shape1(index_t s0)
construct a one dimension shape, stride will equal s0
Definition: tensor.h:207
TBlob(const mshadow::Tensor< Device, dim, DType > &src)
constructor from tensor
Definition: tensor_blob.h:148
Definition: base.h:356
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:58
TBlob(DType *dptr, const mxnet::TShape &shape, int dev_mask, int dev_id=-1)
constructor that construct TBlob from contiguous memory
Definition: tensor_blob.h:90
int ndim
Number of dimensions.
Definition: dlpack.h:136
TBlob & operator=(const TBlob &src)
assignment from TBlob (copy assignment)
Definition: tensor_blob.h:179
static const int kDevMask
device flag number, identifies this device
Definition: tensor.h:44
void * data
The opaque data pointer points to the allocated data. This will be CUDA device pointer or cl_mem hand...
Definition: dlpack.h:132
const DLTensor & dltensor() const
return the corresponding DLTensor
Definition: tensor_blob.h:274
Definition: base.h:355
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:438
virtual void Check(void *head) const
Definition: tensor_blob.h:466
mshadow::Tensor< Device, 1, DType > FlatTo1D(mshadow::Stream< Device > *stream=nullptr) const
flatten the tensor to 1 dimension, collapse all the dimensions together.
Definition: tensor_blob.h:231
int ndim() const
Definition: tuple.h:218
Definition: tensor.h:569
bool CheckContiguous(void) const
Definition: tensor_blob.h:189
Definition: base.h:368
overloaded + operator between half_t and bf16_t
Definition: base.h:327
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:95
constexpr const int kCPU
Definition: tensor_blob.h:43
DLContext ctx
The device context of the tensor.
Definition: dlpack.h:134
CUDA GPU device.
Definition: dlpack.h:42
int64_t * shape
The shape of the tensor.
Definition: dlpack.h:140
Definition: base.h:354
Definition: base.h:364
Definition: base.h:358
The data type the tensor can hold.
Definition: dlpack.h:94
general tensor
Definition: tensor.h:421
uint16_t lanes
Number of lanes in the type, used for vector types.
Definition: dlpack.h:106
Plain C Tensor object, does not manage memory.
Definition: dlpack.h:112
FieldEntry()
Definition: tensor_blob.h:462
ndarray interface
Definition: ndarray.h:82
uint64_t byte_offset
The offset in bytes to the beginning pointer to data.
Definition: dlpack.h:147
int dev_mask() const
device mask of the corresponding device
Definition: tensor_blob.h:263
TBlob(const TBlob &src)
constructor from TBlob (copy constructor)
Definition: tensor_blob.h:155
tensor blob class that can be used to hold tensor of any dimension, any device and any data type...
Definition: tensor_blob.h:66
int dev_id() const
device index of the corresponding device
Definition: tensor_blob.h:267
computaion stream structure, used for asynchronous computations
Definition: tensor.h:384
size_t Size(void) const
total number of elements in the tensor
Definition: tensor_blob.h:250