tensor.h 7.6 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 {
L
liaogang 已提交
37
 public:
38
  template <typename T, size_t D, int MajorType, typename IndexType>
Y
Yi Wang 已提交
39 40
  friend struct EigenTensor;

41
  template <typename T, int MajorType, typename IndexType>
F
WIP  
fengjiayi 已提交
42 43
  friend struct EigenMatrix;

44
  template <typename T, int MajorType, typename IndexType>
Y
Yi Wang 已提交
45 46
  friend struct EigenVector;

Y
Yi Wang 已提交
47
 public:
C
chengduoZH 已提交
48
  Tensor() : offset_(0) {}
49

D
dzhwinter 已提交
50
  /*! Constructor with place should only be used in pybind. */
C
chengduoZH 已提交
51
  explicit Tensor(const platform::Place& place) : offset_(0) {
D
dzhwinter 已提交
52 53 54
    holder_->set_place(place);
  }

L
liaogang 已提交
55
  /*! Return a pointer to mutable memory block. */
Y
Yi Wang 已提交
56
  template <typename T>
L
liaogang 已提交
57
  inline T* data();
Y
Yi Wang 已提交
58

L
liaogang 已提交
59
  /*! Return a pointer to constant memory block. */
Q
qijun 已提交
60
  template <typename T>
L
liaogang 已提交
61 62
  inline const T* data() const;

63 64
  inline bool IsInitialized() const;

Y
Yang Yang 已提交
65 66
  inline void switch_place(platform::Place new_place);

L
liaogang 已提交
67 68 69 70 71
  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
  template <typename T>
C
chengduoZH 已提交
72
  inline T* mutable_data(platform::Place place);
L
liaogang 已提交
73

C
chengduoZH 已提交
74
  inline void* mutable_data(platform::Place place, std::type_index type);
75

C
chengduoZH 已提交
76
  inline void* mutable_data(platform::Place place);
77

L
liaogang 已提交
78 79 80 81 82 83 84 85 86
  /**
   * @brief     Return a pointer to mutable memory block.
   *
   * @param[in] dims    The dimensions of the memory block.
   * @param[in] place   The place of the memory block.
   *
   * @note      If not exist, then allocation.
   */
  template <typename T>
C
chengduoZH 已提交
87
  inline T* mutable_data(DDim dims, platform::Place place);
Y
Yi Wang 已提交
88

L
liaogang 已提交
89 90 91
  /*! Return the dimensions of the memory block. */
  inline const DDim& dims() const;

92 93 94
  /*! Return the numel of the memory block. */
  inline int64_t numel() const;

L
liaogang 已提交
95
  /*! Resize the dimensions of the memory block. */
S
Superjom 已提交
96
  inline Tensor& Resize(const DDim& dims);
L
liaogang 已提交
97 98

  /*! The internal of two tensors share the same memory block. */
S
Superjom 已提交
99
  inline Tensor& ShareDataWith(const Tensor& src);
L
liaogang 已提交
100

T
typhoonzero 已提交
101 102 103
  /*! Share part of the memory of the two tensors */
  inline Tensor& ShareDataWith(Tensor* src, size_t offset);

L
liaogang 已提交
104
  /**
105
   * @brief  Return a sub-tensor of the given tensor.
L
liaogang 已提交
106
   *
107 108 109 110
   * @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 已提交
111
   */
Y
Yu Yang 已提交
112
  inline Tensor Slice(int begin_idx, int end_idx) const;
113

Y
Yu Yang 已提交
114
  platform::Place place() const {
115
    PADDLE_ENFORCE_NOT_NULL(
Q
Qiao Longfei 已提交
116
        holder_, "Tensor not initialized yet when Tensor::place() is called.");
Y
Yu Yang 已提交
117 118
    return holder_->place();
  }
Q
qijun 已提交
119

Q
Qiao Longfei 已提交
120 121 122 123 124
  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 已提交
125

Y
Yu Yang 已提交
126
  // memory size returns the holding memory size in byte.
Y
Yu Yang 已提交
127
  size_t memory_size() const;
Y
Yu Yang 已提交
128

L
liaogang 已提交
129 130
  inline void check_memory_size() const;

D
dzhwinter 已提交
131 132 133 134
  inline DataLayout layout() const { return layout_; }

  inline void set_layout(const DataLayout layout) { layout_ = layout; }

L
liaogang 已提交
135 136 137 138 139
 private:
  /**
   * @note    Placeholder hides type T, so it doesn't appear as a template
   *          parameter of Variable.
   */
Y
Yi Wang 已提交
140
  struct Placeholder {
141
    virtual ~Placeholder() = default;
F
fengjiayi 已提交
142 143
    virtual void* ptr() const = 0;
    virtual size_t size() const = 0;
Y
Yu Yang 已提交
144
    virtual std::type_index type() const = 0;
L
liaogang 已提交
145
    virtual platform::Place place() const = 0;
146
    virtual void set_type(std::type_index type) = 0;
D
dzhwinter 已提交
147
    virtual void set_place(platform::Place place) = 0;
Y
Yi Wang 已提交
148 149
  };

150
  template <typename Place>
Y
Yi Wang 已提交
151
  struct PlaceholderImpl : public Placeholder {
C
chengduoZH 已提交
152 153 154
    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 已提交
155
          place_(place),
156
          size_(size),
C
chengduoZH 已提交
157
          type_(type) {
Y
Yan Chunwei 已提交
158 159
      PADDLE_ENFORCE_NOT_NULL(ptr_, "Insufficient %s memory to allocation.",
                              (is_cpu_place(place_) ? "CPU" : "GPU"));
L
liaogang 已提交
160
    }
Y
Yi Wang 已提交
161

F
fengjiayi 已提交
162
    virtual size_t size() const { return size_; }
L
liaogang 已提交
163 164
    virtual platform::Place place() const { return place_; }
    virtual void* ptr() const { return static_cast<void*>(ptr_.get()); }
165 166
    virtual std::type_index type() const { return type_; }
    virtual void set_type(std::type_index type) { type_ = type; }
D
dzhwinter 已提交
167
    virtual void set_place(platform::Place place) { place_ = place; }
Y
Yi Wang 已提交
168

L
liaogang 已提交
169
    /*! the pointer of memory block. */
170
    std::unique_ptr<uint8_t, memory::PODDeleter<uint8_t, Place>> ptr_;
L
liaogang 已提交
171 172 173 174 175 176

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

    /*! the size of memory block. */
    size_t size_;
177 178 179

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

T
typhoonzero 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  template <typename Place>
  struct SharedPlaceholderImpl : public Placeholder {
    SharedPlaceholderImpl(Place place, uint8_t* data, size_t size,
                          std::type_index type)
        : ptr_(data), place_(place), size_(size), type_(type) {}

    virtual size_t size() const { return size_; }
    virtual platform::Place place() const { return place_; }
    virtual void* ptr() const { return static_cast<void*>(ptr_); }
    virtual std::type_index type() const { return type_; }
    virtual void set_type(std::type_index type) { type_ = type; }
    virtual void set_place(platform::Place place) { place_ = place; }

    /*! the pointer of memory block. */
    uint8_t* ptr_;

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

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

    /* the current type of memory */
    std::type_index type_;
  };

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

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

217
  DDim dims_;
L
liaogang 已提交
218

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

  DataLayout layout_ = DataLayout::kNHWC;

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

Y
Yang Yang 已提交
242 243 244 245 246 247 248 249 250
inline void Tensor::switch_place(platform::Place new_place) {
  if (holder_->place() == new_place) {
    return;
  }

  // TODO(tonyyang-svail): do memcpy here.
  PADDLE_THROW("Not Implemented");
}

Y
Yi Wang 已提交
251 252
}  // namespace framework
}  // namespace paddle
L
liaogang 已提交
253

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