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

Y
Yi Wang 已提交
32
namespace framework {
33 34 35 36
namespace details {
template <bool less, size_t i, typename... args>
struct CastToPyBufferImpl;
}
Y
Yi Wang 已提交
37 38

class Tensor {
L
liaogang 已提交
39
 public:
Y
Yi Wang 已提交
40
  template <bool less, size_t i, typename... args>
41
  friend struct details::CastToPyBufferImpl;
Y
Yi Wang 已提交
42

43
  template <typename T, size_t D, int MajorType, typename IndexType>
Y
Yi Wang 已提交
44 45
  friend struct EigenTensor;

46
  template <typename T, int MajorType, typename IndexType>
F
WIP  
fengjiayi 已提交
47 48
  friend struct EigenMatrix;

49
  template <typename T, int MajorType, typename IndexType>
Y
Yi Wang 已提交
50 51
  friend struct EigenVector;

Y
Yi Wang 已提交
52
 public:
53
  Tensor() : offset_(0) {}
54

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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  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);

  /**
   * @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 已提交
80

Z
Zhuoyuan 已提交
81 82 83
  /*! Size of a single element in data() */
  inline size_t element_size() { return holder_->element_size(); }

L
liaogang 已提交
84 85 86
  /*! Return the dimensions of the memory block. */
  inline const DDim& dims() const;

87 88 89
  /*! Return the numel of the memory block. */
  inline int64_t numel() const;

L
liaogang 已提交
90
  /*! Resize the dimensions of the memory block. */
S
Superjom 已提交
91
  inline Tensor& Resize(const DDim& dims);
L
liaogang 已提交
92 93 94

  /*! The internal of two tensors share the same memory block. */
  template <typename T>
S
Superjom 已提交
95
  inline Tensor& ShareDataWith(const Tensor& src);
L
liaogang 已提交
96 97 98 99 100 101 102 103 104

  /**
   * @brief   Copy the content of external tensor to a new place.
   *
   * @param[in] src   The external tensor.
   * @param[in] ctx   The device context contains place where to store.
   *
   * @note    CopyFrom supports CPU <-> GPU, GPU <-> GPU.
   */
F
fengjiayi 已提交
105
  template <typename T>
Q
qijun 已提交
106
  inline void CopyFrom(const Tensor& src, const platform::Place& dst_place);
107

L
liaogang 已提交
108 109 110 111 112 113
  /**
   * @brief   Return the slice of the tensor.
   *
   * @param[in] begin_idx   The begin index of the slice.
   * @param[in] end_idx     The end index of the slice.
   */
F
fengjiayi 已提交
114
  template <typename T>
L
liaogang 已提交
115
  inline Tensor Slice(const int& begin_idx, const int& end_idx) const;
116

Y
Yu Yang 已提交
117 118 119 120
  platform::Place place() const {
    PADDLE_ENFORCE_NOT_NULL(holder_, "Tensor get place() must contains holder");
    return holder_->place();
  }
Q
qijun 已提交
121

Y
Yi Wang 已提交
122
 private:
L
liaogang 已提交
123 124 125 126 127 128 129 130
  template <typename T>
  inline void check_memory_size() const;

 private:
  /**
   * @note    Placeholder hides type T, so it doesn't appear as a template
   *          parameter of Variable.
   */
Y
Yi Wang 已提交
131 132
  struct Placeholder {
    virtual ~Placeholder() {}
F
fengjiayi 已提交
133 134
    virtual void* ptr() const = 0;
    virtual size_t size() const = 0;
Z
Zhuoyuan 已提交
135
    virtual size_t element_size() const = 0;
Y
Yu Yang 已提交
136
    virtual std::type_index type() const = 0;
L
liaogang 已提交
137
    virtual platform::Place place() const = 0;
Y
Yi Wang 已提交
138 139
  };

L
liaogang 已提交
140
  template <typename T, typename Place>
Y
Yi Wang 已提交
141
  struct PlaceholderImpl : public Placeholder {
L
liaogang 已提交
142
    PlaceholderImpl(Place place, size_t size)
L
liaogang 已提交
143 144 145
        : ptr_(static_cast<T*>(memory::Alloc(place, size)),
               memory::PODDeleter<T, Place>(place)),
          place_(place),
Z
Zhuoyuan 已提交
146 147
          size_(size),
          element_size_(sizeof(T)) {
Y
Yan Chunwei 已提交
148 149
      PADDLE_ENFORCE_NOT_NULL(ptr_, "Insufficient %s memory to allocation.",
                              (is_cpu_place(place_) ? "CPU" : "GPU"));
L
liaogang 已提交
150
    }
Y
Yi Wang 已提交
151

F
fengjiayi 已提交
152
    virtual size_t size() const { return size_; }
L
liaogang 已提交
153 154
    virtual platform::Place place() const { return place_; }
    virtual void* ptr() const { return static_cast<void*>(ptr_.get()); }
Y
Yu Yang 已提交
155
    virtual std::type_index type() const { return std::type_index(typeid(T)); }
Z
Zhuoyuan 已提交
156
    virtual size_t element_size() const { return element_size_; }
Y
Yi Wang 已提交
157

L
liaogang 已提交
158
    /*! the pointer of memory block. */
L
liaogang 已提交
159
    std::unique_ptr<T, memory::PODDeleter<T, Place>> ptr_;
L
liaogang 已提交
160 161 162 163 164 165

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

    /*! the size of memory block. */
    size_t size_;
Z
Zhuoyuan 已提交
166 167 168

    /*! the size of a single element */
    size_t element_size_;
Y
Yi Wang 已提交
169 170
  };

L
liaogang 已提交
171 172 173 174
  /*! holds the memory block if allocated. */
  std::shared_ptr<Placeholder> holder_;

  /*! points to dimensions of memory block. */
175
  DDim dims_;
L
liaogang 已提交
176

Q
qingqing01 已提交
177 178 179 180
  /**
   * A cache of the number of elements in a tensor.
   * Would be 0 for an uninitialized tensor.
   */
181 182
  int64_t numel_;

L
liaogang 已提交
183 184 185 186 187 188 189
  /**
   * @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 已提交
190
  size_t offset_;
191
};
Y
Yi Wang 已提交
192 193 194

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

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