27 #ifndef MXNET_TENSOR_BLOB_H_ 28 #define MXNET_TENSOR_BLOB_H_ 30 #include <dmlc/logging.h> 31 #include <dmlc/json.h> 32 #include <dlpack/dlpack.h> 38 #if MXNET_USE_MKL2017 == 1 39 #include <mkl_memory.h> 69 #if MKL_EXPERIMENTAL == 1 70 std::shared_ptr<MKLMemHolder> Mkl_mem_;
75 type_flag_(mshadow::DataType<
real_t>::kFlag) {
76 #if MKL_EXPERIMENTAL == 1 79 SetDLTensor(cpu::kDevMask, 0);
88 template<
typename DType>
90 : dptr_(dptr), shape_(shape),
91 type_flag_(mshadow::DataType<DType>::kFlag) {
92 #if MKL_EXPERIMENTAL == 1 95 SetDLTensor(dev_mask,
dev_id);
106 : dptr_(dptr), shape_(shape), type_flag_(type_flag) {
107 #if MKL_EXPERIMENTAL == 1 110 SetDLTensor(dev_mask,
dev_id);
119 template<
typename Device,
int dim,
typename DType>
120 TBlob(
const mshadow::Tensor<Device, dim, DType> &src) {
131 template<
typename Device,
int dim,
typename DType>
135 type_flag_ = mshadow::DataType<DType>::kFlag;
136 SetDLTensor(Device::kDevMask, -1);
137 #if MKL_EXPERIMENTAL == 1 154 CHECK_EQ(this->shape_.Size(), shape.Size()) <<
"Shape size mismatch " 155 << this->shape_.
Size() <<
" v.s. " << shape.Size();
166 template<
typename Device,
typename DType>
168 mshadow::Stream<Device> *stream = NULL)
const {
169 CHECK(Device::kDevMask == this->
dev_mask())
170 <<
"TBlob.get: device type do not match specified type";
171 CHECK(mshadow::DataType<DType>::kFlag == type_flag_)
172 <<
"TBlob.get_with_shape: data type do not match specified type." 173 <<
"Expected: " << type_flag_ <<
" v.s. given " << mshadow::DataType<DType>::kFlag;
174 #if MKL_EXPERIMENTAL == 1 175 if (Mkl_mem_ !=
nullptr) {
176 Mkl_mem_->check_and_prv_to_cpu(dptr_);
179 return mshadow::Tensor<Device, 2, DType>(
static_cast<DType*
>(
dptr_),
181 shape_[shape_.ndim() - 1],
191 template<
typename Device,
typename DType>
193 mshadow::Stream<Device> *stream = NULL)
const {
194 return this->get_with_shape<Device, 1, DType>(
195 mshadow::Shape1(shape_.Size()), stream);
199 return shape_.ndim();
211 return shape_.Size();
214 template<
typename DType>
216 CHECK(mshadow::DataType<DType>::kFlag == type_flag_)
217 <<
"TBlob.get_with_shape: data type do not match specified type." 218 <<
"Expected: " << type_flag_ <<
" v.s. given " << mshadow::DataType<DType>::kFlag;
219 #if MKL_EXPERIMENTAL == 1 220 if (Mkl_mem_ !=
nullptr) {
221 Mkl_mem_->check_and_prv_to_cpu(dptr_);
224 return static_cast<DType*
>(
dptr_);
228 return dltensor_.ctx.device_type;
232 return dltensor_.ctx.device_id;
251 template<
typename Device,
int dim,
typename DType>
252 inline mshadow::Tensor<Device, dim, DType>
get(mshadow::Stream<Device> *stream = NULL)
const {
253 CHECK(Device::kDevMask == this->
dev_mask())
254 <<
"TBlob.get: device type do not match specified type";
255 return mshadow::Tensor<Device, dim, DType>(dptr<DType>(),
256 shape_.get<dim>(), shape_[shape_.ndim() - 1], stream);
268 template<
typename Device,
int dim,
typename DType>
270 const mshadow::Shape<dim> &shape,
271 mshadow::Stream<Device> *stream = NULL)
const {
272 CHECK(Device::kDevMask == this->
dev_mask())
273 <<
"TBlob.get: device type do not match specified type";
274 CHECK_EQ(this->
CheckContiguous(),
true) <<
"TBlob.get_reshape: must be contiguous";
275 CHECK_EQ(this->shape_.Size(), shape.Size())
276 <<
"TBlob.get_with_shape: new and old shape do not match total elements";
277 return mshadow::Tensor<Device, dim, DType>(dptr<DType>(), shape,
278 shape[dim - 1], stream);
289 template<
typename Device,
typename DType>
291 int axis, mshadow::Stream<Device> *stream = NULL)
const {
292 return this->get_with_shape<Device, 3, DType>(
293 this->shape_.FlatTo3D(axis), stream);
305 template<
typename Device,
typename DType>
307 int axis_begin,
int axis_end,
308 mshadow::Stream<Device> *stream = NULL)
const {
309 return this->get_with_shape<Device, 3, DType>(
310 this->shape_.FlatTo3D(axis_begin, axis_end), stream);
321 template<
typename Device,
int dim,
typename DType>
322 inline mshadow::Tensor<Device, dim, DType>
FlatToKD(
323 mshadow::Stream<Device> *stream = NULL)
const {
324 mshadow::Shape<dim> shape;
327 for (
int i = 0; i < dim -
ndim(); ++i) {
331 for (
int i = 0; i <
ndim() - dim + 1; ++i) {
332 shape[0] *= shape_[i];
336 shape[i -
ndim() + dim] = shape_[i];
338 return this->get_with_shape<Device, dim, DType>(shape, stream);
342 static DLDataType DTypeTransform(
int type_flag) {
343 static std::unordered_map<int, DLDataType>
344 MSHADOW_DTYPE_TO_DLPACK_DTYPE = {
352 return MSHADOW_DTYPE_TO_DLPACK_DTYPE[type_flag];
356 dltensor_.data =
dptr_;
357 dltensor_.ctx = DLContext{
static_cast<DLDeviceType
>(
dev_mask), dev_id};
358 dltensor_.ndim = shape_.ndim();
359 dltensor_.dtype = DTypeTransform(type_flag_);
360 dltensor_.shape = shape_.data();
361 dltensor_.strides = NULL;
362 dltensor_.byte_offset = 0;
377 namespace parameter {
381 :
public FieldEntryBase<FieldEntry<mxnet::TShape>, mxnet::TShape> {
387 virtual void Check(
void *head)
const {
390 if (expect_ndim_ != 0 && v.ndim() != expect_ndim_) {
391 std::ostringstream os;
392 os <<
"value " << v <<
"for Parameter " << this->key_
393 <<
" has wrong dimensions, expected dimension=" << expect_ndim_;
394 throw dmlc::ParamError(os.str());
396 if (enforce_nonzero_) {
399 std::ostringstream os;
400 os <<
"value " << v <<
"for Parameter " << this->key_
401 <<
" is invalid, the input shape must be nonzero in all dimensions";
402 throw dmlc::ParamError(os.str());
408 this->enforce_nonzero_ =
true;
418 bool enforce_nonzero_;
426 #endif // MXNET_TENSOR_BLOB_H_ TBlob & operator=(const mshadow::Tensor< Device, dim, DType > &src)
assignment from tensor
Definition: tensor_blob.h:132
DMLC_DECLARE_TYPE_NAME(nnvm::Tuple< dmlc::optional< int >>,"Shape(tuple)")
TShape shape_
shape of the tensor
Definition: tensor_blob.h:64
FieldEntry< mxnet::TShape > & set_expect_ndim(mxnet::index_t ndim)
Definition: tensor_blob.h:411
namespace of mxnet
Definition: base.h:126
mshadow::default_real_t real_t
data type that will be used to store ndarray
Definition: base.h:134
TBlob(void)
storing mkl chunk buffer blob, use for experimental only
Definition: tensor_blob.h:73
Definition: tensor_blob.h:380
int type_flag_
type flag of the tensor blob
Definition: tensor_blob.h:66
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:269
nnvm::TShape TShape
Shape data structure used to record shape information.
Definition: base.h:136
FieldEntry< mxnet::TShape > & enforce_nonzero()
Definition: tensor_blob.h:407
FieldEntryBase< FieldEntry< mxnet::TShape >, mxnet::TShape > Parent
Definition: tensor_blob.h:385
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:192
Symbol max(const std::string &symbol_name, Symbol data, Shape axis=Shape(), bool keepdims=false, bool exclude=false)
Definition: op.h:2282
index_t size(index_t idx) const
return size of i-th dimension, start counting from highest dimension
Definition: tensor_blob.h:206
void * dptr_
pointer to the data
Definition: tensor_blob.h:62
Definition: ndarray.h:804
DType * dptr() const
get pointer in dtype
Definition: tensor_blob.h:215
int ndim(void) const
return number of dimension of the tensor inside
Definition: tensor_blob.h:198
TBlob(const mshadow::Tensor< Device, dim, DType > &src)
constructor from tensor
Definition: tensor_blob.h:120
const DLTensor & dltensor() const
return the corresponding DLTensor
Definition: tensor_blob.h:238
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:306
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:322
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:167
virtual void Check(void *head) const
Definition: tensor_blob.h:387
index_t Size(void) const
total number of elements in the tensor
Definition: tensor_blob.h:210
bool CheckContiguous(void) const
Definition: tensor_blob.h:145
TBlob(DType *dptr, const TShape &shape, int dev_mask, int dev_id=-1)
constructor that construct TBlob from contiguous memory
Definition: tensor_blob.h:89
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:290
mshadow::index_t index_t
index type usually use unsigned
Definition: base.h:132
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:105
TBlob reshape(const TShape &shape) const
reshape to shape
Definition: tensor_blob.h:153
FieldEntry()
Definition: tensor_blob.h:383
ndarray interface
Definition: ndarray.h:77
int dev_mask() const
device mask of the corresponding device
Definition: tensor_blob.h:227
tensor blob class that can be used to hold tensor of any dimension, any device and any data type...
Definition: tensor_blob.h:58
int dev_id() const
device index of the corresponding device
Definition: tensor_blob.h:231