tensor.h 6.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 22
#include <vector>

Y
Yi Wang 已提交
23 24 25 26 27 28
#include "paddle/fluid/framework/data_layout.h"
#include "paddle/fluid/framework/ddim.h"
#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

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

32
namespace framework {
Y
Yi Wang 已提交
33

34 35
class LoDTensor;

Y
Yi Wang 已提交
36
class Tensor {
M
mozga-intel 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#ifdef PADDLE_WITH_MKLDNN

 public:
  inline mkldnn::memory::format format() const { return format_; }

  inline void set_format(const mkldnn::memory::format format) {
    format_ = format;
  }

 protected:
  /**
   * @brief the detail format of memory block which have layout as kMKLDNN
   *
   * @note MKLDNN lib support various memory format like nchw, nhwc, nChw8C,
   *       nChw16c, etc. For a MKLDNN memory block, layout will be set as
   *       DataLayout::kMKLDNN meanwhile detail memory format will be kept in
   *       this field.
   */

  mkldnn::memory::format format_ = mkldnn::memory::format::format_undef;
#endif

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

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

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

Y
Yi Wang 已提交
69
 public:
70
  Tensor() : type_(typeid(float)), offset_(0) {}
D
dzhwinter 已提交
71

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

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

M
minqiyang 已提交
80
  inline bool IsInitialized() const;
Y
Yang Yang 已提交
81

L
liaogang 已提交
82 83 84 85 86
  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
  template <typename T>
Y
Yu Yang 已提交
87 88 89
  T* mutable_data(platform::Place place,
                  memory::Allocator::Attr attr = memory::Allocator::kDefault,
                  size_t requested_size = 0);
L
liaogang 已提交
90

91
  void* mutable_data(platform::Place place, std::type_index type,
Y
Yu Yang 已提交
92
                     memory::Allocator::Attr attr = memory::Allocator::kDefault,
93
                     size_t requested_size = 0);
94

Y
Yu Yang 已提交
95 96 97
  void* mutable_data(platform::Place place,
                     memory::Allocator::Attr attr = memory::Allocator::kDefault,
                     size_t requested_size = 0);
98

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

L
liaogang 已提交
113
  /*! Return the dimensions of the memory block. */
114
  const DDim& dims() const;
L
liaogang 已提交
115

116
  /*! Return the numel of the memory block. */
117
  int64_t numel() const;
118

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

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

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

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

Q
Qiao Longfei 已提交
141 142 143
  std::type_index type() const {
    PADDLE_ENFORCE_NOT_NULL(
        holder_, "Tensor not initialized yet when Tensor::type() is called.");
144
    return type_;
Q
Qiao Longfei 已提交
145
  }
Y
Yu Yang 已提交
146

Y
Yu Yang 已提交
147
  // memory size returns the holding memory size in byte.
Y
Yu Yang 已提交
148
  size_t memory_size() const;
Y
Yu Yang 已提交
149

150
  void check_memory_size() const;
L
liaogang 已提交
151

152
  DataLayout layout() const { return layout_; }
D
dzhwinter 已提交
153

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

S
sneaxiy 已提交
156
  void clear() { holder_ = nullptr; }
S
sneaxiy 已提交
157

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

S
sneaxiy 已提交
161 162 163 164
  std::shared_ptr<memory::Allocation> MoveMemory() {
    return std::move(holder_);
  }

L
liaogang 已提交
165 166
 private:
  /*! holds the memory block if allocated. */
167 168
  std::shared_ptr<memory::Allocation> holder_;
  std::type_index type_;
169 170 171 172 173 174
  /**
   * @brief points to elements dimensions.
   *
   * @note dims_ do not indicate the memory block size.
   */

175
  DDim dims_;
L
liaogang 已提交
176

D
dzhwinter 已提交
177
  /**
D
dzhwinter 已提交
178
   * @brief the layout of memory block, default is NHWC.
D
dzhwinter 已提交
179 180 181 182 183 184 185 186
   *
   * @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 已提交
187 188 189 190
  // 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 已提交
191

L
liaogang 已提交
192 193 194 195 196 197 198
  /**
   * @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 已提交
199
  size_t offset_;
200
};
Y
Yi Wang 已提交
201 202 203

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

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