mxnet
tensor_container.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 
25 #ifndef MSHADOW_TENSOR_CONTAINER_H_
26 #define MSHADOW_TENSOR_CONTAINER_H_
27 #include "./tensor.h"
28 #include "./io.h"
29 
30 namespace mshadow {
39 template<typename Device, int dimension, typename DType = default_real_t>
40 class TensorContainer: public Tensor<Device, dimension, DType> {
41  public:
47  this->pad_ = pad;
48  this->dptr_ = data_.dptr_ = NULL;
49  this->shape_[0] = 0;
50  this->stride_ = 0;
51  this->data_.stride_ = 0;
52  this->data_.shape_[0] = 0;
53  }
58  explicit TensorContainer(const Shape<dimension> &shape) {
59  this->pad_ = MSHADOW_ALLOC_PAD;
60  data_.dptr_ = NULL;
61  this->AllocByShape(shape);
62  }
68  explicit TensorContainer(const Shape<dimension> &shape, DType initv) {
69  this->pad_ = MSHADOW_ALLOC_PAD;
70  data_.dptr_ = NULL;
71  this->AllocByShape(shape);
72  (*this) = initv;
73  }
80  : pad_(src.pad_) {
81  this->dptr_ = data_.dptr_ = NULL;
82  this->shape_[0] = 0;
83  this->stride_ = 0;
84  this->data_.stride_ = 0;
85  this->data_.shape_[0] = 0;
86  this->stream_ = src.stream_;
87  if (src.dptr_ != NULL) {
88  this->AllocByShape(src.shape_);
89  mshadow::Copy(*this, src, this->stream_);
90  }
91  }
93  this->Release();
94  }
99  inline void Resize(const Shape<dimension> &shape) {
100  Shape<2> s2 = shape.FlatTo2D();
101  if (s2.shape_[1] > data_.stride_ || s2.shape_[0] > data_.size(0)) {
102  this->AllocByShape(shape);
103  } else {
104  this->shape_ = shape;
105  if (this->pad_) {
106  this->stride_ = data_.stride_;
107  } else {
108  this->stride_ = s2.shape_[1];
109  }
110  }
111  }
117  inline void Resize(const Shape<dimension> &shape, DType initv) {
118  this->Resize(shape);
119  (*this) = initv;
120  }
122  inline void set_pad(bool pad) {
123  this->pad_ = pad;
124  }
130  template<typename TStream>
131  inline void SaveBinary(TStream &fo) const { // NOLINT(*)
132  mshadow::SaveBinary(fo, *this);
133  }
139  template<typename TStream>
140  inline void LoadBinary(TStream &fi) { // NOLINT(*)
142  mshadow::LoadBinary(fi, &tmp, false);
143  this->Resize(tmp.shape_);
144  Stream<Device> stream;
145  Copy(*this, tmp, &stream);
146  mshadow::FreeSpace(&tmp);
147  }
153  inline TensorContainer &operator=
155  this->pad_ = src.pad_;
156  this->stream_ = src.stream_;
157  if (src.dptr_ != NULL) {
158  this->Resize(src.shape_);
159  mshadow::Copy(*this, src, this->stream_);
160  }
161  return *this;
162  }
165  return this->__assign(s);
166  }
168  template<typename E>
171  return this->__assign(exp);
172  }
174  template<typename E>
177  return this->__assign(exp);
178  }
180  template<typename E>
183  return this->__assign(exp);
184  }
190  inline void Release(void) {
191  if (data_.dptr_ != NULL) {
192  this->shape_[0] = 0;
193  this->stride_ = 0;
194  this->data_.stride_ = 0;
195  this->data_.shape_[0] = 0;
196  try {
197  mshadow::FreeSpace(&data_);
198  } catch (const dmlc::Error &e) {
199  this->dptr_ = data_.dptr_ = NULL;
200  throw e;
201  }
202  this->dptr_ = data_.dptr_ = NULL;
203  }
204  }
205 
206  private:
208  bool pad_;
211 
212  inline void AllocByShape(const Shape<dimension>& shape) {
213  if (data_.dptr_ != NULL) this->Release();
214  data_.shape_ = shape.FlatTo2D();
215  mshadow::AllocSpace(&data_, pad_);
216  this->dptr_ = data_.dptr_;
217  this->shape_ = shape;
218  if (this->pad_) {
219  this->stride_ = data_.stride_;
220  } else {
221  this->stride_ = data_.size(1);
222  }
223  }
224 };
225 } // namespace mshadow
226 #endif // MSHADOW_TENSOR_CONTAINER_H_
MSHADOW_THROW_EXCEPTION
#define MSHADOW_THROW_EXCEPTION
Definition: base.h:250
mshadow::LoadBinary
void LoadBinary(TStream &fi, Tensor< cpu, dim, DType > *dst, bool pre_alloc)
CPU/GPU: load a tensor by binary format, for GPU version, a temp Tensor<cpu,dim> storage will be allo...
Definition: io.h:125
mshadow::Stream
computaion stream structure, used for asynchronous computations
Definition: tensor.h:488
io.h
definitions of I/O functions for mshadow tensor
mshadow::Copy
void Copy(Tensor< cpu, dim, DType > dst, const Tensor< cpu, dim, DType > &src, Stream< cpu > *stream=NULL)
copy data from one tensor to another, with same shape
Definition: tensor_cpu-inl.h:145
mshadow::FreeSpace
void FreeSpace(Tensor< cpu, dim, DType > *obj)
CPU/GPU: free the space of tensor, will set obj.dptr to NULL.
Definition: tensor_cpu-inl.h:140
mshadow::Tensor
general tensor
Definition: tensor.h:525
mshadow::TensorContainer::LoadBinary
void LoadBinary(TStream &fi)
load by binary format, a temp Tensor<cpu,dim> storage will be allocated
Definition: tensor_container.h:140
mshadow::TensorContainer::~TensorContainer
~TensorContainer(void) MSHADOW_THROW_EXCEPTION
Definition: tensor_container.h:92
mshadow::TensorContainer::Resize
void Resize(const Shape< dimension > &shape, DType initv)
resize the container to given shape, and initialize, content is NOT preserved
Definition: tensor_container.h:117
mshadow::TensorContainer::operator=
Tensor< Device, dimension, DType > & operator=(DType s)
functions to fit expression template
Definition: tensor_container.h:164
mshadow::TensorContainer::TensorContainer
TensorContainer(const Shape< dimension > &shape)
constructor
Definition: tensor_container.h:58
tensor.h
header file of tensor data structure and functions This lib requires explicit memory allocation and d...
mshadow::TensorContainer::TensorContainer
TensorContainer(const Shape< dimension > &shape, DType initv)
constructor
Definition: tensor_container.h:68
mshadow::TensorContainer::operator=
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kMapper > &exp)
functions to fit expression template
Definition: tensor_container.h:170
mshadow::Shape::shape_
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:86
mshadow::Tensor< Device, dimension, default_real_t >::stream_
Stream< Device > * stream_
stream where the computation lies stream is a device dependency concept where each computation
Definition: tensor.h:551
mshadow::TensorContainer::SaveBinary
void SaveBinary(TStream &fo) const
save by binary format
Definition: tensor_container.h:131
mshadow::Shape::FlatTo2D
MSHADOW_XINLINE Shape< 2 > FlatTo2D(void) const
Definition: tensor.h:146
mshadow::TensorContainer::Release
void Release(void)
Release the llocated space, The TensorContainer is still functionable, but will restart allocating sp...
Definition: tensor_container.h:190
mshadow::TensorContainer::operator=
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kChainer > &exp)
functions to fit expression template
Definition: tensor_container.h:176
mshadow::Tensor< Device, dimension, default_real_t >::shape_
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:541
mshadow::TensorContainer::Resize
void Resize(const Shape< dimension > &shape)
resize the container to given shape, content is NOT preserved
Definition: tensor_container.h:99
MSHADOW_ALLOC_PAD
#define MSHADOW_ALLOC_PAD
whether do padding during allocation
Definition: base.h:73
mshadow::TensorContainer::TensorContainer
TensorContainer(bool pad=MSHADOW_ALLOC_PAD)
constructor
Definition: tensor_container.h:46
mshadow::expr::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
mshadow::expr::pad
PaddingExp< SrcExp, DType, ExpInfo< SrcExp >::kDim > pad(const Exp< SrcExp, DType, etype > &src, index_t pad)
padding expression, pad a image with zeros on boundaries, padding affects shape[0],...
Definition: pad.h:71
mshadow::AllocSpace
void AllocSpace(Tensor< cpu, dim, DType > *obj, bool pad=MSHADOW_ALLOC_PAD)
CPU/CPU: allocate space for CTensor, according to the shape in the obj this function is responsible t...
Definition: tensor_cpu-inl.h:116
mshadow
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::TensorContainer::operator=
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kComplex > &exp)
functions to fit expression template
Definition: tensor_container.h:182
mshadow::TensorContainer
tensor container that does memory allocation and resize like STL, use it to save the lines of FreeSpa...
Definition: tensor_container.h:40
mshadow::Shape
shape of a tensor
Definition: tensor.h:64
mshadow::Tensor< Device, dimension, default_real_t >::dptr_
DType * dptr_
pointer to the data
Definition: tensor.h:539
mshadow::SaveBinary
void SaveBinary(TStream &fo, const Tensor< cpu, dim, DType > &src)
CPU/GPU: save a tensor by binary format, for GPU version, a temp Tensor<cpu,dim> storage will be allo...
Definition: io.h:107
mshadow::Tensor::size
MSHADOW_XINLINE index_t size(int idx) const
return size of i-th dimension, start counting from highest dimension
Definition: tensor.h:610
mshadow::expr::RValueExp< Tensor< Device, dimension, DType >, DType >::__assign
Tensor< Device, dimension, DType > & __assign(DType s)
operator overload
Definition: expression.h:178
mshadow::TensorContainer::set_pad
void set_pad(bool pad)
set whether padding is allowed in tensor
Definition: tensor_container.h:122
mshadow::Tensor< Device, dimension, default_real_t >::stride_
index_t stride_
storing the stride information in x dimension this is used to deal with pitch allocation in gpu or ss...
Definition: tensor.h:546