tensor.h 6.5 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 <utility>
22
#include <vector>
Y
Yi Wang 已提交
23 24
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/ddim.h"
Y
Yu Yang 已提交
25
#include "paddle/fluid/framework/framework.pb.h"
Y
Yi Wang 已提交
26 27 28 29
#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 已提交
30

Y
Yi Wang 已提交
31
namespace paddle {
L
liaogang 已提交
32

33
namespace framework {
Y
Yi Wang 已提交
34

35 36
class LoDTensor;

Y
Yi Wang 已提交
37
class Tensor {
M
mozga-intel 已提交
38 39 40
#ifdef PADDLE_WITH_MKLDNN

 public:
A
Adam 已提交
41
  inline mkldnn::memory::format_tag format() const { return format_; }
M
mozga-intel 已提交
42

A
Adam 已提交
43
  inline void set_format(const mkldnn::memory::format_tag format) {
44
    format_ = format;
M
mozga-intel 已提交
45 46 47 48 49 50 51
  }

 protected:
  /**
   * @brief the detail format of memory block which have layout as kMKLDNN
   *
   * @note MKLDNN lib support various memory format like nchw, nhwc, nChw8C,
52 53 54
   *       nChw16c, etc. For a MKLDNN memory block, layout will be set as
   *       DataLayout::kMKLDNN meanwhile detail memory format will be kept in
   *       this field.
M
mozga-intel 已提交
55
   */
56

A
Adam 已提交
57
  mkldnn::memory::format_tag format_ = mkldnn::memory::format_tag::undef;
M
mozga-intel 已提交
58 59
#endif

L
liaogang 已提交
60
 public:
61
  template <typename T, size_t D, int MajorType, typename IndexType>
Y
Yi Wang 已提交
62 63
  friend struct EigenTensor;

64
  template <typename T, int MajorType, typename IndexType>
F
WIP  
fengjiayi 已提交
65 66
  friend struct EigenMatrix;

67
  template <typename T, int MajorType, typename IndexType>
Y
Yi Wang 已提交
68 69
  friend struct EigenVector;

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

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

L
liaogang 已提交
75
  /*! Return a pointer to mutable memory block. */
Y
Yi Wang 已提交
76
  template <typename T>
77
  T* data();
Y
Yi Wang 已提交
78

L
liaogang 已提交
79
  /*! Return a pointer to constant memory block. */
Q
qijun 已提交
80
  template <typename T>
81
  const T* data() const;
L
liaogang 已提交
82

M
minqiyang 已提交
83
  inline bool IsInitialized() const;
Y
Yang Yang 已提交
84

L
liaogang 已提交
85 86 87 88 89
  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
  template <typename T>
90
  T* mutable_data(const platform::Place& place, size_t requested_size = 0);
L
liaogang 已提交
91

92
  void* mutable_data(const platform::Place& place, proto::VarType::Type type,
93
                     size_t requested_size = 0);
94

95
  void* mutable_data(const platform::Place& place, size_t requested_size = 0);
96

L
liaogang 已提交
97 98 99
  /**
   * @brief     Return a pointer to mutable memory block.
   *
100 101 102
   * @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 已提交
103 104 105 106
   *
   * @note      If not exist, then allocation.
   */
  template <typename T>
107 108
  T* mutable_data(const DDim& dims, const platform::Place& place,
                  size_t requested_size = 0);
Y
Yi Wang 已提交
109

L
liaogang 已提交
110
  /*! Return the dimensions of the memory block. */
111
  const DDim& dims() const;
L
liaogang 已提交
112

113
  /*! Return the numel of the memory block. */
114
  int64_t numel() const;
115

L
liaogang 已提交
116
  /*! Resize the dimensions of the memory block. */
117
  Tensor& Resize(const DDim& dims);
L
liaogang 已提交
118 119

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

  /**
123
   * @brief  Return a sub-tensor of the given tensor.
L
liaogang 已提交
124
   *
125 126 127 128
   * @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 已提交
129
   */
C
chengduo 已提交
130
  Tensor Slice(int64_t begin_idx, int64_t end_idx) const;
131

132
  const platform::Place& place() const {
133
    PADDLE_ENFORCE_NOT_NULL(
Q
Qiao Longfei 已提交
134
        holder_, "Tensor not initialized yet when Tensor::place() is called.");
Y
Yu Yang 已提交
135 136
    return holder_->place();
  }
Q
qijun 已提交
137

Y
Yu Yang 已提交
138
  proto::VarType::Type type() const {
Q
Qiao Longfei 已提交
139 140
    PADDLE_ENFORCE_NOT_NULL(
        holder_, "Tensor not initialized yet when Tensor::type() is called.");
141
    return type_;
Q
Qiao Longfei 已提交
142
  }
Y
Yu Yang 已提交
143

Y
Yu Yang 已提交
144
  // memory size returns the holding memory size in byte.
Y
Yu Yang 已提交
145
  size_t memory_size() const;
Y
Yu Yang 已提交
146

147
  void check_memory_size() const;
L
liaogang 已提交
148

149
  DataLayout layout() const { return layout_; }
D
dzhwinter 已提交
150

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

153 154 155 156 157 158 159 160
  void clear() {
    holder_ = nullptr;
    offset_ = 0;
  }

  void ShareBufferWith(const Tensor& tensor) {
    holder_ = tensor.holder_;
    offset_ = tensor.offset_;
161
    type_ = tensor.type_;
162
  }
S
sneaxiy 已提交
163

164 165 166 167
  bool IsSharedBufferWith(const Tensor& src) const {
    return holder_ && holder_ == src.Holder();
  }

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

S
sneaxiy 已提交
171
  std::shared_ptr<memory::Allocation> MoveMemoryHolder() {
S
sneaxiy 已提交
172 173 174
    return std::move(holder_);
  }

175 176
  void ResetHolder(std::shared_ptr<memory::Allocation> holder);

177 178 179
  void ResetHolderWithType(std::shared_ptr<memory::Allocation> holder,
                           const proto::VarType::Type type);

L
liaogang 已提交
180 181
 private:
  /*! holds the memory block if allocated. */
182
  std::shared_ptr<memory::Allocation> holder_;
Y
Yu Yang 已提交
183
  proto::VarType::Type type_;
184 185 186 187 188 189
  /**
   * @brief points to elements dimensions.
   *
   * @note dims_ do not indicate the memory block size.
   */

190
  DDim dims_;
L
liaogang 已提交
191

D
dzhwinter 已提交
192
  /**
D
dzhwinter 已提交
193
   * @brief the layout of memory block, default is NHWC.
D
dzhwinter 已提交
194 195 196 197 198 199 200 201
   *
   * @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 已提交
202 203 204 205
  // 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 已提交
206

L
liaogang 已提交
207 208 209 210 211 212 213
  /**
   * @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 已提交
214
  size_t offset_;
215
};
Y
Yi Wang 已提交
216 217 218

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

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