tensor.h 7.4 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>

D
dzhwinter 已提交
23 24
#if defined(_WIN32)
#define GLOG_NO_ABBREVIATED_SEVERITIES  // msvc conflict logging with windows.h
D
dzhwinter 已提交
25
#define GOOGLE_GLOG_DLL_DECL
D
dzhwinter 已提交
26 27
#endif

Y
Yi Wang 已提交
28 29 30 31 32 33
#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 已提交
34

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

37
namespace framework {
Y
Yi Wang 已提交
38

39 40
class LoDTensor;

Y
Yi Wang 已提交
41
class Tensor {
M
mozga-intel 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#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 已提交
64
 public:
65
  template <typename T, size_t D, int MajorType, typename IndexType>
Y
Yi Wang 已提交
66 67
  friend struct EigenTensor;

68
  template <typename T, int MajorType, typename IndexType>
F
WIP  
fengjiayi 已提交
69 70
  friend struct EigenMatrix;

71
  template <typename T, int MajorType, typename IndexType>
Y
Yi Wang 已提交
72 73
  friend struct EigenVector;

Y
Yi Wang 已提交
74
 public:
C
chengduoZH 已提交
75
  Tensor() : offset_(0) {}
76

D
dzhwinter 已提交
77
  /*! Constructor with place should only be used in pybind. */
C
chengduoZH 已提交
78
  explicit Tensor(const platform::Place& place) : offset_(0) {
D
dzhwinter 已提交
79 80 81
    holder_->set_place(place);
  }

L
liaogang 已提交
82
  /*! Return a pointer to mutable memory block. */
Y
Yi Wang 已提交
83
  template <typename T>
84
  T* data();
Y
Yi Wang 已提交
85

L
liaogang 已提交
86
  /*! Return a pointer to constant memory block. */
Q
qijun 已提交
87
  template <typename T>
88
  const T* data() const;
L
liaogang 已提交
89

M
minqiyang 已提交
90
  inline bool IsInitialized() const;
Y
Yang Yang 已提交
91

L
liaogang 已提交
92 93 94 95 96
  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
  template <typename T>
97
  T* mutable_data(platform::Place place, size_t requested_size = 0);
L
liaogang 已提交
98

99
  void* mutable_data(platform::Place place, std::type_index type,
100
                     size_t requested_size = 0);
101

102
  void* mutable_data(platform::Place place, size_t requested_size = 0);
103

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

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

119
  /*! Return the numel of the memory block. */
120
  int64_t numel() const;
121

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

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

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

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

Q
Qiao Longfei 已提交
144 145 146 147 148
  std::type_index type() const {
    PADDLE_ENFORCE_NOT_NULL(
        holder_, "Tensor not initialized yet when Tensor::type() is called.");
    return holder_->type();
  }
Y
Yu Yang 已提交
149

Y
Yu Yang 已提交
150
  // memory size returns the holding memory size in byte.
Y
Yu Yang 已提交
151
  size_t memory_size() const;
Y
Yu Yang 已提交
152

153
  void check_memory_size() const;
L
liaogang 已提交
154

155
  DataLayout layout() const { return layout_; }
D
dzhwinter 已提交
156

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

L
liaogang 已提交
159 160 161 162 163
 private:
  /**
   * @note    Placeholder hides type T, so it doesn't appear as a template
   *          parameter of Variable.
   */
Y
Yi Wang 已提交
164
  struct Placeholder {
165
    virtual ~Placeholder() = default;
F
fengjiayi 已提交
166 167
    virtual void* ptr() const = 0;
    virtual size_t size() const = 0;
Y
Yu Yang 已提交
168
    virtual std::type_index type() const = 0;
L
liaogang 已提交
169
    virtual platform::Place place() const = 0;
170
    virtual void set_type(std::type_index type) = 0;
D
dzhwinter 已提交
171
    virtual void set_place(platform::Place place) = 0;
Y
Yi Wang 已提交
172 173
  };

174
  template <typename Place>
Y
Yi Wang 已提交
175
  struct PlaceholderImpl : public Placeholder {
C
chengduoZH 已提交
176 177 178
    PlaceholderImpl(Place place, size_t size, std::type_index type)
        : ptr_(static_cast<uint8_t*>(memory::Alloc(place, size)),
               memory::PODDeleter<uint8_t, Place>(place)),
L
liaogang 已提交
179
          place_(place),
180
          size_(size),
C
chengduoZH 已提交
181
          type_(type) {
Y
Yan Chunwei 已提交
182 183
      PADDLE_ENFORCE_NOT_NULL(ptr_, "Insufficient %s memory to allocation.",
                              (is_cpu_place(place_) ? "CPU" : "GPU"));
L
liaogang 已提交
184
    }
Y
Yi Wang 已提交
185

F
fengjiayi 已提交
186
    virtual size_t size() const { return size_; }
L
liaogang 已提交
187 188
    virtual platform::Place place() const { return place_; }
    virtual void* ptr() const { return static_cast<void*>(ptr_.get()); }
189 190
    virtual std::type_index type() const { return type_; }
    virtual void set_type(std::type_index type) { type_ = type; }
D
dzhwinter 已提交
191
    virtual void set_place(platform::Place place) { place_ = place; }
Y
Yi Wang 已提交
192

L
liaogang 已提交
193
    /*! the pointer of memory block. */
194
    std::unique_ptr<uint8_t, memory::PODDeleter<uint8_t, Place>> ptr_;
L
liaogang 已提交
195 196 197 198 199 200

    /*! the place of memory block. */
    platform::Place place_;

    /*! the size of memory block. */
    size_t size_;
201 202 203

    /* the current type of memory */
    std::type_index type_;
Y
Yi Wang 已提交
204 205
  };

L
liaogang 已提交
206 207 208
  /*! holds the memory block if allocated. */
  std::shared_ptr<Placeholder> holder_;

209 210 211 212 213 214
  /**
   * @brief points to elements dimensions.
   *
   * @note dims_ do not indicate the memory block size.
   */

215
  DDim dims_;
L
liaogang 已提交
216

D
dzhwinter 已提交
217
  /**
D
dzhwinter 已提交
218
   * @brief the layout of memory block, default is NHWC.
D
dzhwinter 已提交
219 220 221 222 223 224 225 226
   *
   * @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 已提交
227 228 229 230
  // 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 已提交
231

L
liaogang 已提交
232 233 234 235 236 237 238
  /**
   * @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 已提交
239
  size_t offset_;
240
};
Y
Yi Wang 已提交
241 242 243

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

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