mxnet
io.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_IO_H_
26 #define MSHADOW_IO_H_
27 #include "./tensor.h"
28 
29 namespace mshadow {
30 namespace utils {
36 class IStream {
37  public:
44  virtual size_t Read(void *ptr, size_t size) = 0;
50  virtual void Write(const void *ptr, size_t size) = 0;
52  virtual ~IStream(void) {}
53 };
54 } // namespace utils
63 template<int dim, typename DType, typename TStream>
64 inline void SaveBinary(TStream &fo, const Tensor<cpu, dim, DType> &src); // NOLINT(*)
73 template<int dim, typename DType, typename TStream>
74 inline void SaveBinary(TStream &fo, const Tensor<gpu, dim, DType> &src); // NOLINT(*)
86 template<int dim, typename DType, typename TStream>
87 inline void LoadBinary(TStream &fi, // NOLINT(*)
88  Tensor<cpu, dim, DType> *dst, bool pre_alloc);
101 template<int dim, typename DType, typename TStream>
102 inline void LoadBinary(TStream &fi, // NOLINT(*)
103  Tensor<gpu, dim, DType> *dst, bool pre_alloc);
104 
105 // implementations
106 template<int dim, typename DType, typename TStream>
107 inline void SaveBinary(TStream &fo, const Tensor<cpu, dim, DType> &src_) { // NOLINT(*)
108  fo.Write(&src_.shape_, sizeof(src_.shape_));
109  Tensor<cpu, 2, DType> src = src_.FlatTo2D();
110  for (index_t i = 0; i < src.size(0); ++i) {
111  fo.Write(src[i].dptr_, sizeof(DType) * src.size(1));
112  }
113 }
114 template<int dim, typename DType, typename TStream>
115 inline void SaveBinary(TStream &fo, const Tensor<gpu, dim, DType> &src) { // NOLINT(*)
116  // copy to CPU, then save
118  AllocSpace(&tmp);
119  Stream<gpu> stream;
120  Copy(tmp, src, &stream);
121  SaveBinary(fo, tmp);
122  FreeSpace(&tmp);
123 }
124 template<int dim, typename DType, typename TStream>
125 inline void LoadBinary(TStream &fi, // NOLINT(*)
126  Tensor<cpu, dim, DType> *dst_, bool pre_alloc) {
127  Shape<dim> shape;
128  CHECK_NE(fi.Read(&shape, sizeof(shape)), 0) << "mshadow::LoadBinary";
129  if (pre_alloc) {
130  CHECK_EQ(shape, dst_->shape_) << "LoadBinary, shape do not match pre-allocated shape";
131  } else {
132  dst_->shape_ = shape; AllocSpace(dst_);
133  }
134  Tensor<cpu, 2, DType> dst = dst_->FlatTo2D();
135  if (dst.size(0) == 0) return;
136  for (index_t i = 0; i < dst.size(0); ++i) {
137  CHECK_NE(fi.Read(dst[i].dptr_, sizeof(DType) * dst.size(1)), 0) << "mshadow::LoadBinary";
138  }
139 }
140 template<int dim, typename DType, typename TStream>
141 inline void LoadBinary(TStream &fi, // NOLINT(*)
142  Tensor<gpu, dim, DType> *dst, bool pre_alloc) {
144  LoadBinary(fi, &tmp, false);
145  if (pre_alloc) {
146  CHECK_EQ(tmp.shape, dst->shape_) << "LoadBinary, shape do not match pre-allocated shape";
147  } else {
148  dst->shape = tmp.shape; AllocSpace(dst);
149  }
150  Stream<gpu> stream;
151  Copy(*dst, tmp, &stream);
152  FreeSpace(&tmp);
153 }
154 } // namespace mshadow
155 #endif // MSHADOW_IO_H_
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::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::utils::IStream::Read
virtual size_t Read(void *ptr, size_t size)=0
read data from stream
tensor.h
header file of tensor data structure and functions This lib requires explicit memory allocation and d...
mshadow::Stream< gpu >
Definition: stream_gpu-inl.h:37
mshadow::Tensor::shape_
Shape< dimension > shape_
shape of the tensor
Definition: tensor.h:541
mshadow::index_t
int32_t index_t
type that will be used for index
Definition: base.h:328
mshadow::utils::IStream::Write
virtual void Write(const void *ptr, size_t size)=0
write data to stream
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::utils::IStream::~IStream
virtual ~IStream(void)
virtual destructor
Definition: io.h:52
mshadow::Tensor::FlatTo2D
MSHADOW_XINLINE Tensor< Device, 2, DType > FlatTo2D(void) const
flatten the tensor to 2 dimension, collapse the higher dimensions together
Definition: tensor.h:624
mshadow::Shape< dim >
mshadow::Tensor::dptr_
DType * dptr_
pointer to the data
Definition: tensor.h:539
mshadow::utils::IStream
interface of stream I/O, used to serialize data, mshadow does not restricted to only this interface i...
Definition: io.h:36
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