tensor.h 7.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yi Wang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

Y
Yi Wang 已提交
15 16
#pragma once

17
#include <cstdint>
18
#include <cstring>
F
fengjiayi 已提交
19
#include <memory>
Y
Yu Yang 已提交
20
#include <typeindex>
21
#include <vector>
Y
Yi Wang 已提交
22 23
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/ddim.h"
Y
Yu Yang 已提交
24
#include "paddle/fluid/framework/framework.pb.h"
Y
Yi Wang 已提交
25 26 27 28
#include "paddle/fluid/memory/memory.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"
F
fengjiayi 已提交
29

30 31 32 33
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_utils.h"
#endif

Y
Yi Wang 已提交
34
namespace paddle {
L
liaogang 已提交
35

36
namespace framework {
Y
Yi Wang 已提交
37

38 39
class LoDTensor;

Y
Yi Wang 已提交
40
class Tensor {
M
mozga-intel 已提交
41 42 43
#ifdef PADDLE_WITH_MKLDNN

 public:
44 45 46 47 48 49 50 51
  // TODO(jczaja): This is depracted and will be removed
  inline mkldnn::memory::format format() const {
    if (layout_ == DataLayout::kMKLDNN) {
      return static_cast<mkldnn::memory::format>(mem_pd_.desc().data.format);
    } else {
      return mkldnn::memory::format::format_undef;
    }
  }
M
mozga-intel 已提交
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  // TODO(jczaja): This is depracted and will be removed
  inline void set_format(
      const mkldnn::memory::format fmt,
      mkldnn::memory::data_type data_type = mkldnn::memory::f32) {
    mem_pd_ = paddle::platform::create_prim_desc_from_format(
        paddle::framework::vectorize2int(dims()), fmt, data_type);
    layout_ = DataLayout::kMKLDNN;
  }

  inline mkldnn::memory::primitive_desc get_mkldnn_prim_desc() const {
    return mem_pd_;
  }

  inline void set_mkldnn_prim_desc(
      const mkldnn::memory::primitive_desc& mem_pd) {
    // Internally MKL-DNN is just copying (increasing reference counter)
    // to shared_ptr. So asignment should be quite cheap
    mem_pd_ = mem_pd;
    layout_ = DataLayout::kMKLDNN;
M
mozga-intel 已提交
72 73 74 75 76 77 78
  }

 protected:
  /**
   * @brief the detail format of memory block which have layout as kMKLDNN
   *
   * @note MKLDNN lib support various memory format like nchw, nhwc, nChw8C,
79
   *       nChw16c, etc. For a MKLDNN memory block, we store memory descriptor
M
mozga-intel 已提交
80
   */
81
  mutable mkldnn::memory::primitive_desc mem_pd_;
M
mozga-intel 已提交
82 83
#endif

L
liaogang 已提交
84
 public:
85
  template <typename T, size_t D, int MajorType, typename IndexType>
Y
Yi Wang 已提交
86 87
  friend struct EigenTensor;

88
  template <typename T, int MajorType, typename IndexType>
F
WIP  
fengjiayi 已提交
89 90
  friend struct EigenMatrix;

91
  template <typename T, int MajorType, typename IndexType>
Y
Yi Wang 已提交
92 93
  friend struct EigenVector;

Y
Yi Wang 已提交
94
 public:
Y
Yu Yang 已提交
95
  Tensor() : type_(proto::VarType::FP32), offset_(0) {}
D
dzhwinter 已提交
96

C
chengduo 已提交
97
  explicit Tensor(const proto::VarType::Type&);
98

L
liaogang 已提交
99
  /*! Return a pointer to mutable memory block. */
Y
Yi Wang 已提交
100
  template <typename T>
101
  T* data();
Y
Yi Wang 已提交
102

L
liaogang 已提交
103
  /*! Return a pointer to constant memory block. */
Q
qijun 已提交
104
  template <typename T>
105
  const T* data() const;
L
liaogang 已提交
106

M
minqiyang 已提交
107
  inline bool IsInitialized() const;
Y
Yang Yang 已提交
108

L
liaogang 已提交
109 110 111 112 113
  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
  template <typename T>
Y
Yu Yang 已提交
114 115 116
  T* mutable_data(platform::Place place,
                  memory::Allocator::Attr attr = memory::Allocator::kDefault,
                  size_t requested_size = 0);
L
liaogang 已提交
117

Y
Yu Yang 已提交
118
  void* mutable_data(platform::Place place, proto::VarType::Type type,
Y
Yu Yang 已提交
119
                     memory::Allocator::Attr attr = memory::Allocator::kDefault,
120
                     size_t requested_size = 0);
121

Y
Yu Yang 已提交
122 123 124
  void* mutable_data(platform::Place place,
                     memory::Allocator::Attr attr = memory::Allocator::kDefault,
                     size_t requested_size = 0);
125

L
liaogang 已提交
126 127 128
  /**
   * @brief     Return a pointer to mutable memory block.
   *
129 130 131
   * @param[in] dims           The dimensions of the memory block.
   * @param[in] place          The place of the memory block.
   * @param[in] requested_size The size of the block in bytes.
L
liaogang 已提交
132 133 134 135
   *
   * @note      If not exist, then allocation.
   */
  template <typename T>
Y
Yu Yang 已提交
136 137 138
  T* mutable_data(DDim dims, platform::Place place,
                  memory::Allocator::Attr attr = memory::Allocator::kDefault,
                  size_t requested_size = 0);
Y
Yi Wang 已提交
139

L
liaogang 已提交
140
  /*! Return the dimensions of the memory block. */
141
  const DDim& dims() const;
L
liaogang 已提交
142

143
  /*! Return the numel of the memory block. */
144
  int64_t numel() const;
145

L
liaogang 已提交
146
  /*! Resize the dimensions of the memory block. */
147
  Tensor& Resize(const DDim& dims);
L
liaogang 已提交
148 149

  /*! The internal of two tensors share the same memory block. */
150
  Tensor& ShareDataWith(const Tensor& src);
L
liaogang 已提交
151 152

  /**
153
   * @brief  Return a sub-tensor of the given tensor.
L
liaogang 已提交
154
   *
155 156 157 158
   * @param[in] begin_idx   The index of the start row(inclusive) to slice.
   *                        The index number begins from 0.
   * @param[in] end_idx     The index of the end row(exclusive) to slice.
   *                        The index number begins from 0.
L
liaogang 已提交
159
   */
160
  Tensor Slice(int begin_idx, int end_idx) const;
161

Y
Yu Yang 已提交
162
  platform::Place place() const {
163
    PADDLE_ENFORCE_NOT_NULL(
Q
Qiao Longfei 已提交
164
        holder_, "Tensor not initialized yet when Tensor::place() is called.");
Y
Yu Yang 已提交
165 166
    return holder_->place();
  }
Q
qijun 已提交
167

Y
Yu Yang 已提交
168
  proto::VarType::Type type() const {
Q
Qiao Longfei 已提交
169 170
    PADDLE_ENFORCE_NOT_NULL(
        holder_, "Tensor not initialized yet when Tensor::type() is called.");
171
    return type_;
Q
Qiao Longfei 已提交
172
  }
Y
Yu Yang 已提交
173

Y
Yu Yang 已提交
174
  // memory size returns the holding memory size in byte.
Y
Yu Yang 已提交
175
  size_t memory_size() const;
Y
Yu Yang 已提交
176

177
  void check_memory_size() const;
L
liaogang 已提交
178

179
  DataLayout layout() const { return layout_; }
D
dzhwinter 已提交
180

181
  void set_layout(const DataLayout layout) { layout_ = layout; }
D
dzhwinter 已提交
182

S
sneaxiy 已提交
183 184
  void clear() { holder_ = nullptr; }

Y
Yu Yang 已提交
185
  const std::shared_ptr<memory::Allocation>& Holder() const { return holder_; }
Y
Yu Yang 已提交
186
  size_t offset() const { return offset_; }
Y
Yu Yang 已提交
187

S
sneaxiy 已提交
188
  std::shared_ptr<memory::Allocation> MoveMemoryHolder() {
S
sneaxiy 已提交
189 190 191
    return std::move(holder_);
  }

192 193
  void ResetHolder(std::shared_ptr<memory::Allocation> holder);

L
liaogang 已提交
194 195
 private:
  /*! holds the memory block if allocated. */
196
  std::shared_ptr<memory::Allocation> holder_;
Y
Yu Yang 已提交
197
  proto::VarType::Type type_;
198 199 200 201 202 203
  /**
   * @brief points to elements dimensions.
   *
   * @note dims_ do not indicate the memory block size.
   */

204
  DDim dims_;
L
liaogang 已提交
205

D
dzhwinter 已提交
206
  /**
D
dzhwinter 已提交
207
   * @brief the layout of memory block, default is NHWC.
D
dzhwinter 已提交
208 209 210 211 212 213 214 215
   *
   * @note the memory allocation order, describe how weight/data is stored
   *       For example, in 4-D Tensor(rank=4), there are three commonly
   *       used layout. They are
   *            NCHW, NHWC, CHWN.
   *       N,C,H,W for respectively the batch size, the number of
   *       feature maps, the height.
   */
M
mozga-intel 已提交
216 217 218 219
  // Fix me: here just change the default layout to kNCHW
  // it doesn't fix the real issue, i.e. feeder should set up tensor layout
  // according to actual input data
  DataLayout layout_ = DataLayout::kNCHW;
D
dzhwinter 已提交
220

L
liaogang 已提交
221 222 223 224 225 226 227
  /**
   * @brief   A PlaceHolder may be shared by more than one tensor.
   *
   * @note    Some of them may be slices of the others. So the offset_
   *          is introduced here to indicate the byte offset between
   *          PlaceHolder::ptr_ and where the tensor data really begins.
   */
F
fengjiayi 已提交
228
  size_t offset_;
229
};
Y
Yi Wang 已提交
230 231 232

}  // namespace framework
}  // namespace paddle
L
liaogang 已提交
233

Y
Yi Wang 已提交
234
#include "paddle/fluid/framework/tensor_impl.h"