Go to the documentation of this file.
27 #ifndef MXNET_TENSOR_BLOB_H_
28 #define MXNET_TENSOR_BLOB_H_
30 #include <dmlc/logging.h>
87 template <
typename DType>
121 if (strides[
ndim - 1] != 1) {
124 for (
int i =
ndim - 2; i >= 0; --i) {
125 if (strides[i] != shape[i + 1] * strides[i + 1]) {
132 LOG(FATAL) <<
"Unsupported DLPack because MXNet only support compact tensor now";
144 template <
typename Device,
int dim,
typename DType>
163 template <
typename Device,
int dim,
typename DType>
168 SetDLTensor(Device::kDevMask, -1);
195 CHECK_EQ(this->shape_.
Size(), shape.
Size())
196 <<
"Shape size mismatch " << this->shape_.
Size() <<
" v.s. " << shape.
Size();
207 template <
typename Device,
typename DType>
210 CHECK(Device::kDevMask == this->
dev_mask())
211 <<
"TBlob.get: device type do not match specified type";
213 <<
"TBlob.get_with_shape: data type do not match specified type. "
225 template <
typename Device,
typename DType>
244 inline size_t Size(
void)
const {
248 template <
typename DType>
251 <<
"TBlob.get_with_shape: data type do not match specified type. "
254 return static_cast<DType*
>(
dptr_);
281 template <
typename Device,
int dim,
typename DType>
283 CHECK(Device::kDevMask == this->
dev_mask())
284 <<
"TBlob.get: device type do not match specified type";
298 template <
typename Device,
int dim,
typename DType>
302 CHECK(Device::kDevMask == this->
dev_mask())
303 <<
"TBlob.get: device type do not match specified type";
304 CHECK_EQ(this->
CheckContiguous(),
true) <<
"TBlob.get_reshape: must be contiguous";
305 CHECK_EQ(this->shape_.
Size(),
static_cast<size_t>(shape.
Size()))
306 <<
"TBlob.get_with_shape: new and old shape do not match total elements";
318 template <
typename Device,
typename DType>
322 return this->get_with_shape<Device, 3, DType>(this->shape_.FlatTo3D(axis), stream);
334 template <
typename Device,
typename DType>
337 return this->get_with_shape<Device, 3, DType>(this->shape_.FlatTo3D(axis_begin, axis_end),
349 template <
typename Device,
int dim,
typename DType>
355 for (
int i = 0; i < dim -
ndim(); ++i) {
359 for (
int i = 0; i <
ndim() - dim + 1; ++i) {
363 for (
int i = std::max(0,
ndim() - dim + 1); i <
ndim(); ++i) {
366 return this->get_with_shape<Device, dim, DType>(shape, stream);
370 static DLDataType DTypeTransform(
int type_flag) {
399 LOG(FATAL) <<
"Unknown type_flag=" << type_flag;
404 static int DLDataTypeTransform(
DLDataType dldata_type) {
405 if (dldata_type.
lanes != 1) {
406 LOG(FATAL) <<
"Unsupported DLDataType whose lanes != 1";
408 switch (dldata_type.
code) {
410 switch (dldata_type.
bits) {
420 switch (dldata_type.
bits) {
426 switch (dldata_type.
bits) {
440 switch (dldata_type.
bits) {
452 LOG(FATAL) <<
"Unknown DLDataType{" << dldata_type.
code <<
", " << dldata_type.
bits <<
", "
453 << dldata_type.
lanes <<
"}";
481 namespace parameter {
484 class FieldEntry<
mxnet::TShape> :
public FieldEntryBase<FieldEntry<mxnet::TShape>, mxnet::TShape> {
490 virtual void Check(
void* head)
const {
493 if (expect_ndim_ != 0 && v.
ndim() != expect_ndim_) {
494 std::ostringstream os;
495 os <<
"value " << v <<
"for Parameter " << this->key_
496 <<
" has wrong dimensions, expected dimension=" << expect_ndim_;
497 throw dmlc::ParamError(os.str());
499 if (enforce_nonzero_) {
500 for (
int i = 0; i < v.
ndim(); ++i) {
502 std::ostringstream os;
503 os <<
"value " << v <<
"for Parameter " << this->key_
504 <<
" is invalid, the input shape must be nonzero in all dimensions";
505 throw dmlc::ParamError(os.str());
511 this->enforce_nonzero_ =
true;
521 bool enforce_nonzero_;
529 #endif // MXNET_TENSOR_BLOB_H_
namespace of mxnet
Definition: api_registry.h:33
@ kDLFloat
Definition: dlpack.h:82
constexpr const int kCPU
Definition: tensor_blob.h:42
TBlob(const mshadow::Tensor< Device, dim, DType > &src)
constructor from tensor
Definition: tensor_blob.h:145
The data type the tensor can hold.
Definition: dlpack.h:94
MSHADOW_XINLINE index_t Size(void) const
Definition: tensor.h:158
FieldEntry()
Definition: tensor_blob.h:486
computaion stream structure, used for asynchronous computations
Definition: tensor.h:488
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:57
@ kUint16
Definition: base.h:361
TBlob(void)
default constructor, default copy assign will work
Definition: tensor_blob.h:77
@ kUint64
Definition: base.h:363
DType * dptr() const
get pointer in dtype
Definition: tensor_blob.h:249
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
A dynamic sized array data structure that is optimized for storing small number of elements with same...
Definition: tuple.h:52
namespace for dmlc
Definition: array_view.h:12
DLDeviceType device_type
The device type used in the device.
Definition: dlpack.h:71
virtual void Check(void *head) const
Definition: tensor_blob.h:490
Definition: tensor_blob.h:484
@ kInt8
Definition: base.h:357
@ kUint32
Definition: base.h:362
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:100
int ndim
Number of dimensions.
Definition: dlpack.h:136
bool CheckContiguous(void) const
Definition: tensor_blob.h:186
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:208
A Device context for Tensor and operator.
Definition: dlpack.h:69
@ kDLCPU
CPU device.
Definition: dlpack.h:40
uint64_t byte_offset
The offset in bytes to the beginning pointer to data.
Definition: dlpack.h:147
general tensor
Definition: tensor.h:525
void * dptr_
pointer to the data
Definition: tensor_blob.h:70
size_t Size(void) const
total number of elements in the tensor
Definition: tensor_blob.h:244
static const int kDevMask
device flag number, identifies this device
Definition: tensor.h:43
int dev_id() const
device index of the corresponding device
Definition: tensor_blob.h:261
DLDeviceType
The device type in DLContext.
Definition: dlpack.h:38
int ndim() const
Definition: tuple.h:217
DMLC_DECLARE_TYPE_NAME(optional< int >, "int or None")
description for optional int
@ kBool
Definition: base.h:359
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:299
@ kFloat64
Definition: base.h:353
TBlob & operator=(const TBlob &src)
assignment from TBlob (copy assignment)
Definition: tensor_blob.h:176
int dev_mask() const
device mask of the corresponding device
Definition: tensor_blob.h:257
int ndim(void) const
return number of dimension of the tensor inside
Definition: tensor_blob.h:231
@ kInt16
Definition: base.h:360
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
int type_flag_
type flag of the tensor blob
Definition: tensor_blob.h:74
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:88
mshadow::Tensor< Device, dim, DType > get(mshadow::Stream< Device > *stream=nullptr) const
fetch the tensor, with respect to specific dimension if dim do not match the stored dimension,...
Definition: tensor_blob.h:282
TBlob & operator=(const mshadow::Tensor< Device, dim, DType > &src)
assignment from tensor
Definition: tensor_blob.h:164
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:350
@ kDLGPU
CUDA GPU device.
Definition: dlpack.h:42
uint8_t code
Type code of base types. We keep it uint8_t instead of DLDataTypeCode for minimal memory footprint,...
Definition: dlpack.h:100
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:541
ndarray interface
Definition: ndarray.h:82
@ kInt64
Definition: base.h:358
uint8_t bits
Number of bits, common choices are 8, 16, 32.
Definition: dlpack.h:104
@ kDLInt
Definition: dlpack.h:80
tensor blob class that can be used to hold tensor of any dimension, any device and any data type,...
Definition: tensor_blob.h:65
DLContext ctx
The device context of the tensor.
Definition: dlpack.h:134
@ kInt32
Definition: base.h:356
FieldEntry< mxnet::TShape > & set_expect_ndim(int ndim)
Definition: tensor_blob.h:514
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,...
Definition: tensor_blob.h:336
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:240
DLDataType dtype
The data type of the pointer.
Definition: dlpack.h:138
const DLTensor & dltensor() const
return the corresponding DLTensor
Definition: tensor_blob.h:268
@ kDLUInt
Definition: dlpack.h:81
std::string dtype_string(const int dtype)
Definition: base.h:1811
size_t Size() const
Definition: tuple.h:523
const dim_t * data() const
Definition: tuple.h:552
overloaded + operator between half_t and bf16_t
Definition: base.h:319
TBlob(const DLTensor &dltensor)
constructor that construct TBlob from DLTensor
Definition: tensor_blob.h:108
int64_t * shape
The shape of the tensor.
Definition: dlpack.h:140
FieldEntry< mxnet::TShape > & enforce_nonzero()
Definition: tensor_blob.h:510
constexpr const int kTVMNDArrayTypeCode
Definition: tensor_blob.h:48
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:81
@ kUint8
Definition: base.h:355
DType * dptr_
pointer to the data
Definition: tensor.h:539
@ kBfloat16
Definition: base.h:364
TBlob(const TBlob &src)
constructor from TBlob (copy constructor)
Definition: tensor_blob.h:152
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:226
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:319
Lightweight JSON Reader/Writer that read save into C++ data structs. This includes STL composites and...
MSHADOW_XINLINE Shape< 1 > Shape1(index_t s0)
construct a one dimension shape, stride will equal s0
Definition: tensor.h:220
mxnet::TShape shape_
shape of the tensor
Definition: tensor_blob.h:72
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:440
FieldEntryBase< FieldEntry< mxnet::TShape >, mxnet::TShape > Parent
Definition: tensor_blob.h:488
int device_id
The device index.
Definition: dlpack.h:73
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
TBlob reshape(const mxnet::TShape &shape) const
reshape to shape
Definition: tensor_blob.h:194
The common header of DLPack.
constexpr const int kGPU
Definition: tensor_blob.h:43
@ kFloat16
Definition: base.h:354
c++17 compatible optional class.
Definition: optional.h:43
configuration of MXNet as well as basic data structure.
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:85
@ kFloat32
Definition: base.h:352
@ kDLBfloat
Definition: dlpack.h:83