28 #ifndef MXNET_TENSOR_BLOB_H_ 29 #define MXNET_TENSOR_BLOB_H_ 31 #include <dmlc/logging.h> 32 #include <dmlc/json.h> 33 #include <dlpack/dlpack.h> 43 constexpr
const int kCPU = kDLCPU;
44 constexpr
const int kGPU = kDLGPU;
79 type_flag_(mshadow::DataType<
real_t>::kFlag) {
80 SetDLTensor(cpu::kDevMask, 0);
89 template<
typename DType>
91 : dptr_(dptr), shape_(shape),
92 type_flag_(mshadow::DataType<DType>::kFlag) {
93 SetDLTensor(dev_mask,
dev_id);
104 : dptr_(dptr), shape_(shape), type_flag_(type_flag) {
105 SetDLTensor(dev_mask,
dev_id);
114 template<
typename Device,
int dim,
typename DType>
115 TBlob(
const mshadow::Tensor<Device, dim, DType> &src) {
126 template<
typename Device,
int dim,
typename DType>
130 type_flag_ = mshadow::DataType<DType>::kFlag;
131 SetDLTensor(Device::kDevMask, -1);
146 CHECK_EQ(this->shape_.Size(), shape.Size()) <<
"Shape size mismatch " 147 << this->shape_.
Size() <<
" v.s. " << shape.Size();
158 template<
typename Device,
typename DType>
160 mshadow::Stream<Device> *stream = NULL)
const {
161 CHECK(Device::kDevMask == this->
dev_mask())
162 <<
"TBlob.get: device type do not match specified type";
163 CHECK(mshadow::DataType<DType>::kFlag == type_flag_)
164 <<
"TBlob.get_with_shape: data type do not match specified type." 165 <<
"Expected: " << type_flag_ <<
" v.s. given " << mshadow::DataType<DType>::kFlag;
166 return mshadow::Tensor<Device, 2, DType>(
static_cast<DType*
>(
dptr_),
168 shape_[shape_.ndim() - 1],
178 template<
typename Device,
typename DType>
180 mshadow::Stream<Device> *stream = NULL)
const {
181 return this->get_with_shape<Device, 1, DType>(
182 mshadow::Shape1(shape_.Size()), stream);
186 return shape_.ndim();
198 return shape_.Size();
201 template<
typename DType>
203 CHECK(mshadow::DataType<DType>::kFlag == type_flag_)
204 <<
"TBlob.get_with_shape: data type do not match specified type." 205 <<
"Expected: " << type_flag_ <<
" v.s. given " << mshadow::DataType<DType>::kFlag;
206 return static_cast<DType*
>(
dptr_);
210 return dltensor_.ctx.device_type;
214 return dltensor_.ctx.device_id;
233 template<
typename Device,
int dim,
typename DType>
234 inline mshadow::Tensor<Device, dim, DType>
get(mshadow::Stream<Device> *stream = NULL)
const {
235 CHECK(Device::kDevMask == this->
dev_mask())
236 <<
"TBlob.get: device type do not match specified type";
237 return mshadow::Tensor<Device, dim, DType>(dptr<DType>(),
238 shape_.get<dim>(), shape_[shape_.ndim() - 1], stream);
250 template<
typename Device,
int dim,
typename DType>
252 const mshadow::Shape<dim> &shape,
253 mshadow::Stream<Device> *stream = NULL)
const {
254 CHECK(Device::kDevMask == this->
dev_mask())
255 <<
"TBlob.get: device type do not match specified type";
256 CHECK_EQ(this->
CheckContiguous(),
true) <<
"TBlob.get_reshape: must be contiguous";
257 CHECK_EQ(this->shape_.Size(), shape.Size())
258 <<
"TBlob.get_with_shape: new and old shape do not match total elements";
259 return mshadow::Tensor<Device, dim, DType>(dptr<DType>(), shape,
260 shape[dim - 1], stream);
271 template<
typename Device,
typename DType>
273 int axis, mshadow::Stream<Device> *stream = NULL)
const {
274 return this->get_with_shape<Device, 3, DType>(
275 this->shape_.FlatTo3D(axis), stream);
287 template<
typename Device,
typename DType>
289 int axis_begin,
int axis_end,
290 mshadow::Stream<Device> *stream = NULL)
const {
291 return this->get_with_shape<Device, 3, DType>(
292 this->shape_.FlatTo3D(axis_begin, axis_end), stream);
303 template<
typename Device,
int dim,
typename DType>
304 inline mshadow::Tensor<Device, dim, DType>
FlatToKD(
305 mshadow::Stream<Device> *stream = NULL)
const {
306 mshadow::Shape<dim> shape;
309 for (
int i = 0; i < dim -
ndim(); ++i) {
313 for (
int i = 0; i <
ndim() - dim + 1; ++i) {
314 shape[0] *= shape_[i];
318 shape[i -
ndim() + dim] = shape_[i];
320 return this->get_with_shape<Device, dim, DType>(shape, stream);
324 static DLDataType DTypeTransform(
int type_flag) {
326 case mshadow::kFloat32:
return DLDataType{kDLFloat, 32, 1};
327 case mshadow::kFloat64:
return DLDataType{kDLFloat, 64, 1};
328 case mshadow::kFloat16:
return DLDataType{kDLFloat, 16, 1};
329 case mshadow::kUint8:
return DLDataType{kDLUInt, 8, 1};
330 case mshadow::kInt32:
return DLDataType{kDLInt, 32, 1};
331 case mshadow::kInt8:
return DLDataType{kDLInt, 8, 1};
332 case mshadow::kInt64:
return DLDataType{kDLInt, 64, 1};
334 LOG(FATAL) <<
"Unknown type_flag=" << type_flag;
341 dltensor_.data =
dptr_;
342 dltensor_.ctx = DLContext{
static_cast<DLDeviceType
>(
dev_mask), dev_id};
343 dltensor_.ndim = shape_.ndim();
344 dltensor_.dtype = DTypeTransform(type_flag_);
345 dltensor_.shape = shape_.data();
346 dltensor_.strides = NULL;
347 dltensor_.byte_offset = 0;
362 namespace parameter {
366 :
public FieldEntryBase<FieldEntry<mxnet::TShape>, mxnet::TShape> {
372 virtual void Check(
void *head)
const {
375 if (expect_ndim_ != 0 && v.ndim() != expect_ndim_) {
376 std::ostringstream os;
377 os <<
"value " << v <<
"for Parameter " << this->key_
378 <<
" has wrong dimensions, expected dimension=" << expect_ndim_;
379 throw dmlc::ParamError(os.str());
381 if (enforce_nonzero_) {
384 std::ostringstream os;
385 os <<
"value " << v <<
"for Parameter " << this->key_
386 <<
" is invalid, the input shape must be nonzero in all dimensions";
387 throw dmlc::ParamError(os.str());
393 this->enforce_nonzero_ =
true;
403 bool enforce_nonzero_;
411 #endif // MXNET_TENSOR_BLOB_H_ TBlob & operator=(const mshadow::Tensor< Device, dim, DType > &src)
assignment from tensor
Definition: tensor_blob.h:127
DMLC_DECLARE_TYPE_NAME(nnvm::Tuple< dmlc::optional< int >>,"Shape(tuple)")
constexpr const int kTVMNDArrayTypeCode
Definition: tensor_blob.h:49
TShape shape_
shape of the tensor
Definition: tensor_blob.h:72
FieldEntry< mxnet::TShape > & set_expect_ndim(mxnet::index_t ndim)
Definition: tensor_blob.h:396
namespace of mxnet
Definition: base.h:118
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:126
TBlob(void)
default constructor, default copy assign will work
Definition: tensor_blob.h:77
Definition: tensor_blob.h:365
int type_flag_
type flag of the tensor blob
Definition: tensor_blob.h:74
mshadow::Tensor< Device, dim, DType > get_with_shape(const mshadow::Shape< dim > &shape, mshadow::Stream< Device > *stream=NULL) const
fetch a tensor in given shape If size do not match the stored size, an error will be issued ...
Definition: tensor_blob.h:251
nnvm::TShape TShape
Shape data structure used to record shape information.
Definition: base.h:128
FieldEntry< mxnet::TShape > & enforce_nonzero()
Definition: tensor_blob.h:392
FieldEntryBase< FieldEntry< mxnet::TShape >, mxnet::TShape > Parent
Definition: tensor_blob.h:370
mshadow::Tensor< Device, 1, DType > FlatTo1D(mshadow::Stream< Device > *stream=NULL) const
flatten the tensor to 1 dimension, collapse all the dimensions together.
Definition: tensor_blob.h:179
constexpr const int kGPU
Definition: tensor_blob.h:44
Symbol max(const std::string &symbol_name, Symbol data, dmlc::optional< Shape > axis=dmlc::optional< Shape >(), bool keepdims=false, bool exclude=false)
Definition: op.h:2735
index_t size(index_t idx) const
return size of i-th dimension, start counting from highest dimension
Definition: tensor_blob.h:193
void * dptr_
pointer to the data
Definition: tensor_blob.h:70
Definition: ndarray.h:1368
DType * dptr() const
get pointer in dtype
Definition: tensor_blob.h:202
int ndim(void) const
return number of dimension of the tensor inside
Definition: tensor_blob.h:185
TBlob(const mshadow::Tensor< Device, dim, DType > &src)
constructor from tensor
Definition: tensor_blob.h:115
const DLTensor & dltensor() const
return the corresponding DLTensor
Definition: tensor_blob.h:220
mshadow::Tensor< Device, 3, DType > FlatTo3D(int axis_begin, int axis_end, mshadow::Stream< Device > *stream=NULL) const
flatten the tensor to 3 dimension, collapse the dimension: [0, axis_begin), [axis_begin, axis_end], (axis_end, ndim).
Definition: tensor_blob.h:288
mshadow::Tensor< Device, dim, DType > FlatToKD(mshadow::Stream< Device > *stream=NULL) const
flatten the tensor to specified number of dimensions, collapse the highest dimensions or pad with hig...
Definition: tensor_blob.h:304
mshadow::Tensor< Device, 2, DType > FlatTo2D(mshadow::Stream< Device > *stream=NULL) const
flatten the tensor to 2 dimension, collapse the higher dimensions together
Definition: tensor_blob.h:159
virtual void Check(void *head) const
Definition: tensor_blob.h:372
index_t Size(void) const
total number of elements in the tensor
Definition: tensor_blob.h:197
bool CheckContiguous(void) const
Definition: tensor_blob.h:137
TBlob(DType *dptr, const TShape &shape, int dev_mask, int dev_id=-1)
constructor that construct TBlob from contiguous memory
Definition: tensor_blob.h:90
mshadow::Tensor< Device, 3, DType > FlatTo3D(int axis, mshadow::Stream< Device > *stream=NULL) const
flatten the tensor to 3 dimension, collapse the dimension before and after specified axis...
Definition: tensor_blob.h:272
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:124
TBlob(void *dptr, const TShape &shape, int dev_mask, int type_flag, int dev_id=-1)
constructor that construct TBlob from contiguous memory
Definition: tensor_blob.h:103
constexpr const int kCPU
Definition: tensor_blob.h:43
TBlob reshape(const TShape &shape) const
reshape to shape
Definition: tensor_blob.h:145
FieldEntry()
Definition: tensor_blob.h:368
ndarray interface
Definition: ndarray.h:82
int dev_mask() const
device mask of the corresponding device
Definition: tensor_blob.h:209
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:213