tensor.h 5.1 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>
F
fengjiayi 已提交
21
#include "paddle/framework/ddim.h"
Q
qijun 已提交
22
#include "paddle/memory/memcpy.h"
F
fengjiayi 已提交
23
#include "paddle/memory/memory.h"
L
liaogang 已提交
24
#include "paddle/platform/device_context.h"
L
liaogang 已提交
25
#include "paddle/platform/enforce.h"
F
fengjiayi 已提交
26
#include "paddle/platform/place.h"
Q
init  
qijun 已提交
27
#include "unsupported/Eigen/CXX11/Tensor"
F
fengjiayi 已提交
28

Y
Yi Wang 已提交
29
namespace paddle {
30 31 32 33 34 35
namespace pybind {
namespace details {  // forward declare
template <bool less, size_t i, typename... args>
struct CastToPyBufferImpl;
}  // namespace details
}  // namespace pybind
L
liaogang 已提交
36

Y
Yi Wang 已提交
37 38 39
namespace framework {

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

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

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

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

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

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

L
liaogang 已提交
61 62 63 64
  /**
   * @brief   Return a pointer to mutable memory block.
   * @note    If not exist, then allocation.
   */
F
fengjiayi 已提交
65
  template <typename T>
L
liaogang 已提交
66 67 68 69 70 71 72 73 74 75 76 77
  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 已提交
78

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

  /*! Resize the dimensions of the memory block. */
  inline void Resize(const DDim& dims);
Y
Yi Wang 已提交
84

L
liaogang 已提交
85
  /*! The internal of two tensors share the same memory block. */
F
fengjiayi 已提交
86
  template <typename T>
L
liaogang 已提交
87 88 89 90 91 92 93 94 95 96
  inline void ShareDataWith(const Tensor& src);

  /**
   * @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 已提交
97
  template <typename T>
Q
qijun 已提交
98
  inline void CopyFrom(const Tensor& src, const platform::Place& dst_place);
99

L
liaogang 已提交
100 101 102 103 104 105 106
  /**
   * @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.
   */
  inline Tensor Slice(const int& begin_idx, const int& end_idx) const;
107

Y
Yi Wang 已提交
108
 private:
F
fengjiayi 已提交
109
  template <typename T>
L
liaogang 已提交
110
  inline void check_memory_size() const;
111

Q
qijun 已提交
112 113
  paddle::platform::Place place() const { return holder_->place(); }

Y
Yi Wang 已提交
114
 private:
L
liaogang 已提交
115 116 117 118
  /**
   * @note    Placeholder hides type T, so it doesn't appear as a template
   *          parameter of Variable.
   */
Y
Yi Wang 已提交
119 120
  struct Placeholder {
    virtual ~Placeholder() {}
F
fengjiayi 已提交
121 122
    virtual void* ptr() const = 0;
    virtual size_t size() const = 0;
Y
Yu Yang 已提交
123
    virtual std::type_index type() const = 0;
L
liaogang 已提交
124
    virtual platform::Place place() const = 0;
Y
Yi Wang 已提交
125 126
  };

L
liaogang 已提交
127
  template <typename T, typename Place>
Y
Yi Wang 已提交
128
  struct PlaceholderImpl : public Placeholder {
L
liaogang 已提交
129
    PlaceholderImpl(Place place, size_t size)
F
fengjiayi 已提交
130
        : ptr_(static_cast<T*>(memory::Alloc(place, size)),
L
liaogang 已提交
131
               memory::PODDeleter<T, Place>(place)),
132
          place_(place),
L
liaogang 已提交
133
          size_(size) {
L
liaogang 已提交
134 135 136
      PADDLE_ENFORCE(ptr_ != nullptr, "Insufficient %s memory to allocation.",
                     is_cpu_place(place_) ? "CPU" : "GPU");
    }
Y
Yi Wang 已提交
137

F
fengjiayi 已提交
138
    virtual size_t size() const { return size_; }
L
liaogang 已提交
139 140
    virtual platform::Place place() const { return place_; }
    virtual void* ptr() const { return static_cast<void*>(ptr_.get()); }
Y
Yu Yang 已提交
141
    virtual std::type_index type() const { return std::type_index(typeid(T)); }
Y
Yi Wang 已提交
142

L
liaogang 已提交
143
    /*! the pointer of memory block. */
L
liaogang 已提交
144
    std::unique_ptr<T, memory::PODDeleter<T, Place>> ptr_;
L
liaogang 已提交
145 146 147 148 149 150

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

    /*! the size of memory block. */
    size_t size_;
Y
Yi Wang 已提交
151 152
  };

L
liaogang 已提交
153 154 155 156
  /*! holds the memory block if allocated. */
  std::shared_ptr<Placeholder> holder_;

  /*! points to dimensions of memory block. */
157
  DDim dims_;
L
liaogang 已提交
158 159 160 161 162 163 164 165

  /**
   * @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 已提交
166
  size_t offset_;
167
};
Y
Yi Wang 已提交
168 169 170

}  // namespace framework
}  // namespace paddle
L
liaogang 已提交
171 172

#include "paddle/framework/detail/tensor-inl.h"