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 
26 #ifndef MSHADOW_TENSOR_CONTAINER_H_
27 #define MSHADOW_TENSOR_CONTAINER_H_
28 #include "./tensor.h"
29 #include "./io.h"
30 
31 namespace mshadow {
40 template<typename Device, int dimension, typename DType = default_real_t>
41 class TensorContainer: public Tensor<Device, dimension, DType> {
42  public:
48  this->pad_ = pad;
49  this->dptr_ = data_.dptr_ = NULL;
50  this->shape_[0] = 0;
51  this->stride_ = 0;
52  this->data_.stride_ = 0;
53  this->data_.shape_[0] = 0;
54  }
59  explicit TensorContainer(const Shape<dimension> &shape) {
60  this->pad_ = MSHADOW_ALLOC_PAD;
61  data_.dptr_ = NULL;
62  this->AllocByShape(shape);
63  }
69  explicit TensorContainer(const Shape<dimension> &shape, DType initv) {
70  this->pad_ = MSHADOW_ALLOC_PAD;
71  data_.dptr_ = NULL;
72  this->AllocByShape(shape);
73  (*this) = initv;
74  }
81  : pad_(src.pad_) {
82  this->dptr_ = data_.dptr_ = NULL;
83  this->shape_[0] = 0;
84  this->stride_ = 0;
85  this->data_.stride_ = 0;
86  this->data_.shape_[0] = 0;
87  this->stream_ = src.stream_;
88  if (src.dptr_ != NULL) {
89  this->AllocByShape(src.shape_);
90  mshadow::Copy(*this, src, this->stream_);
91  }
92  }
94  this->Release();
95  }
100  inline void Resize(const Shape<dimension> &shape) {
101  Shape<2> s2 = shape.FlatTo2D();
102  if (s2.shape_[1] > data_.stride_ || s2.shape_[0] > data_.size(0)) {
103  this->AllocByShape(shape);
104  } else {
105  this->shape_ = shape;
106  if (this->pad_) {
107  this->stride_ = data_.stride_;
108  } else {
109  this->stride_ = s2.shape_[1];
110  }
111  }
112  }
118  inline void Resize(const Shape<dimension> &shape, DType initv) {
119  this->Resize(shape);
120  (*this) = initv;
121  }
123  inline void set_pad(bool pad) {
124  this->pad_ = pad;
125  }
131  template<typename TStream>
132  inline void SaveBinary(TStream &fo) const { // NOLINT(*)
133  mshadow::SaveBinary(fo, *this);
134  }
140  template<typename TStream>
141  inline void LoadBinary(TStream &fi) { // NOLINT(*)
143  mshadow::LoadBinary(fi, &tmp, false);
144  this->Resize(tmp.shape_);
145  Stream<Device> stream;
146  Copy(*this, tmp, &stream);
147  mshadow::FreeSpace(&tmp);
148  }
154  inline TensorContainer &operator=
156  this->pad_ = src.pad_;
157  this->stream_ = src.stream_;
158  if (src.dptr_ != NULL) {
159  this->Resize(src.shape_);
160  mshadow::Copy(*this, src, this->stream_);
161  }
162  return *this;
163  }
166  return this->__assign(s);
167  }
169  template<typename E>
172  return this->__assign(exp);
173  }
175  template<typename E>
178  return this->__assign(exp);
179  }
181  template<typename E>
184  return this->__assign(exp);
185  }
191  inline void Release(void) {
192  if (data_.dptr_ != NULL) {
193  this->shape_[0] = 0;
194  this->stride_ = 0;
195  this->data_.stride_ = 0;
196  this->data_.shape_[0] = 0;
197  try {
198  mshadow::FreeSpace(&data_);
199  } catch (const dmlc::Error &e) {
200  this->dptr_ = data_.dptr_ = NULL;
201  throw e;
202  }
203  this->dptr_ = data_.dptr_ = NULL;
204  }
205  }
206 
207  private:
209  bool pad_;
212 
213  inline void AllocByShape(const Shape<dimension>& shape) {
214  if (data_.dptr_ != NULL) this->Release();
215  data_.shape_ = shape.FlatTo2D();
216  mshadow::AllocSpace(&data_, pad_);
217  this->dptr_ = data_.dptr_;
218  this->shape_ = shape;
219  if (this->pad_) {
220  this->stride_ = data_.stride_;
221  } else {
222  this->stride_ = data_.size(1);
223  }
224  }
225 };
226 } // namespace mshadow
227 #endif // MSHADOW_TENSOR_CONTAINER_H_
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:141
void Resize(const Shape< dimension > &shape, DType initv)
resize the container to given shape, and initialize, content is NOT preserved
Definition: tensor_container.h:118
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], and shape[1]
Definition: pad.h:72
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kComplex > &exp)
functions to fit expression template
Definition: tensor_container.h:183
DType * dptr_
pointer to the data
Definition: tensor.h:435
TensorContainer(const Shape< dimension > &shape, DType initv)
constructor
Definition: tensor_container.h:69
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:126
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:146
shape of a tensor
Definition: tensor.h:54
void set_pad(bool pad)
set whether padding is allowed in tensor
Definition: tensor_container.h:123
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:437
TensorContainer(bool pad=MSHADOW_ALLOC_PAD)
constructor
Definition: tensor_container.h:47
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kChainer > &exp)
functions to fit expression template
Definition: tensor_container.h:177
void Release(void)
Release the llocated space, The TensorContainer is still functionable, but will restart allocating sp...
Definition: tensor_container.h:191
MSHADOW_XINLINE Shape< 2 > FlatTo2D(void) const
Definition: tensor.h:133
header file of tensor data structure and functions This lib requires explicit memory allocation and d...
#define MSHADOW_ALLOC_PAD
whether do padding during allocation
Definition: base.h:73
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:117
TensorContainer(const Shape< dimension > &shape)
constructor
Definition: tensor_container.h:59
Tensor< Device, dimension, DType > & operator=(DType s)
functions to fit expression template
Definition: tensor_container.h:165
tensor container that does memory allocation and resize like STL, use it to save the lines of FreeSpa...
Definition: tensor_container.h:41
void SaveBinary(TStream &fo) const
save by binary format
Definition: tensor_container.h:132
index_t shape_[kDimension]
storing the dimension information
Definition: tensor.h:76
~TensorContainer(void) MSHADOW_THROW_EXCEPTION
Definition: tensor_container.h:93
void LoadBinary(TStream &fi)
load by binary format, a temp Tensor<cpu,dim> storage will be allocated
Definition: tensor_container.h:141
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:80
overloaded + operator between half_t and bf16_t
Definition: base.h:327
#define MSHADOW_THROW_EXCEPTION
Definition: base.h:253
MSHADOW_XINLINE index_t size(int idx) const
return size of i-th dimension, start counting from highest dimension
Definition: tensor.h:506
Tensor< Device, dimension, DType > & operator=(const expr::Exp< E, DType, expr::type::kMapper > &exp)
functions to fit expression template
Definition: tensor_container.h:171
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:108
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:442
Tensor< Device, dimension, DType > & __assign(DType s)
operator overload
Definition: expression.h:179
general tensor
Definition: tensor.h:421
void Resize(const Shape< dimension > &shape)
resize the container to given shape, content is NOT preserved
Definition: tensor_container.h:100
Stream< Device > * stream_
stream where the computation lies stream is a device dependency concept where each computation ...
Definition: tensor.h:447
computaion stream structure, used for asynchronous computations
Definition: tensor.h:384