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

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>

F
fengjiayi 已提交
23 24
#include "paddle/framework/ddim.h"
#include "paddle/memory/memory.h"
L
liaogang 已提交
25
#include "paddle/platform/device_context.h"
L
liaogang 已提交
26
#include "paddle/platform/enforce.h"
F
fengjiayi 已提交
27
#include "paddle/platform/place.h"
Q
init  
qijun 已提交
28
#include "unsupported/Eigen/CXX11/Tensor"
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:
48
  Tensor() : offset_(0) {}
49

L
liaogang 已提交
50
  /*! Return a pointer to mutable memory block. */
Y
Yi Wang 已提交
51
  template <typename T>
L
liaogang 已提交
52
  inline T* data();
Y
Yi Wang 已提交
53

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

  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
  template <typename T>
  inline T* mutable_data(platform::Place place);

65 66 67 68
  inline void* mutable_data(platform::Place place, std::type_index type);

  inline void* mutable_data(platform::Place place);

L
liaogang 已提交
69 70 71 72 73 74 75 76 77 78
  /**
   * @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>
  inline T* mutable_data(DDim dims, platform::Place place);
Y
Yi Wang 已提交
79

L
liaogang 已提交
80 81 82
  /*! Return the dimensions of the memory block. */
  inline const DDim& dims() const;

83 84 85
  /*! Return the numel of the memory block. */
  inline int64_t numel() const;

L
liaogang 已提交
86
  /*! Resize the dimensions of the memory block. */
S
Superjom 已提交
87
  inline Tensor& Resize(const DDim& dims);
L
liaogang 已提交
88 89

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

  /**
   * @brief   Copy the content of external tensor to a new place.
   *
95 96 97
   * @param[in] src        The external tensor.
   * @param[in] dst_place  The dst place.
   * @param[in] ctx        The device context contains device resources.
L
liaogang 已提交
98 99 100
   *
   * @note    CopyFrom supports CPU <-> GPU, GPU <-> GPU.
   */
101 102 103 104 105
  // TODO(qijun): https://github.com/PaddlePaddle/Paddle/issues/4647
  // Remove `CopyFrom` and `CopyFromVector` from Tensor interface
  // and make them global functions
  inline void CopyFrom(const Tensor& src, const platform::Place& dst_place,
                       const platform::DeviceContext& ctx);
106

107 108 109
  /**
   * @brief   Copy the content of an external vector to a tensor.
   *
110 111
   * @param[in] src        The external tensor.
   * @param[in] ctx        The device context contains device resources.
112 113 114 115 116 117
   *
   * * @note    CopyFromVector assumes that the tensor has been resized
   *            before invoking.
   */
  template <typename T>
  inline void CopyFromVector(const std::vector<T>& src,
118
                             const platform::DeviceContext& ctx);
119

L
liaogang 已提交
120
  /**
121
   * @brief  Return a sub-tensor of the given tensor.
L
liaogang 已提交
122
   *
123 124 125 126
   * @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 已提交
127 128
   */
  inline Tensor Slice(const int& begin_idx, const int& end_idx) const;
129

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

Q
Qiao Longfei 已提交
136 137 138 139 140
  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 已提交
141

Y
Yu Yang 已提交
142
  size_t memory_size() const;
Y
Yu Yang 已提交
143

Y
Yi Wang 已提交
144
 private:
L
liaogang 已提交
145 146 147
  inline void check_memory_size() const;

 private:
148 149
  friend class LoDTensor;

L
liaogang 已提交
150 151 152 153
  /**
   * @note    Placeholder hides type T, so it doesn't appear as a template
   *          parameter of Variable.
   */
Y
Yi Wang 已提交
154
  struct Placeholder {
155
    virtual ~Placeholder() = default;
F
fengjiayi 已提交
156 157
    virtual void* ptr() const = 0;
    virtual size_t size() const = 0;
Y
Yu Yang 已提交
158
    virtual std::type_index type() const = 0;
L
liaogang 已提交
159
    virtual platform::Place place() const = 0;
160
    virtual void set_type(std::type_index type) = 0;
Y
Yi Wang 已提交
161 162
  };

163
  template <typename Place>
Y
Yi Wang 已提交
164
  struct PlaceholderImpl : public Placeholder {
165 166 167
    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 已提交
168
          place_(place),
169 170
          size_(size),
          type_(type) {
Y
Yan Chunwei 已提交
171 172
      PADDLE_ENFORCE_NOT_NULL(ptr_, "Insufficient %s memory to allocation.",
                              (is_cpu_place(place_) ? "CPU" : "GPU"));
L
liaogang 已提交
173
    }
Y
Yi Wang 已提交
174

F
fengjiayi 已提交
175
    virtual size_t size() const { return size_; }
L
liaogang 已提交
176 177
    virtual platform::Place place() const { return place_; }
    virtual void* ptr() const { return static_cast<void*>(ptr_.get()); }
178 179
    virtual std::type_index type() const { return type_; }
    virtual void set_type(std::type_index type) { type_ = type; }
Y
Yi Wang 已提交
180

L
liaogang 已提交
181
    /*! the pointer of memory block. */
182
    std::unique_ptr<uint8_t, memory::PODDeleter<uint8_t, Place>> ptr_;
L
liaogang 已提交
183 184 185 186 187 188

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

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

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

L
liaogang 已提交
194 195 196
  /*! holds the memory block if allocated. */
  std::shared_ptr<Placeholder> holder_;

197 198 199 200 201 202
  /**
   * @brief points to elements dimensions.
   *
   * @note dims_ do not indicate the memory block size.
   */

203
  DDim dims_;
L
liaogang 已提交
204 205 206 207 208 209 210 211

  /**
   * @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 已提交
212
  size_t offset_;
213
};
Y
Yi Wang 已提交
214 215 216

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

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