dense_tensor.h 11.3 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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. */

#pragma once

17
#include "paddle/phi/core/allocator.h"
18
#include "paddle/phi/core/storage_properties.h"
19 20 21
#include "paddle/phi/core/stream.h"
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_meta.h"
22

23
/* @jim19930609: Move to MKLDNN_Tensor in the future
24
 */
25
#ifdef PADDLE_WITH_MKLDNN
L
Leo Chen 已提交
26
#include "dnnl.hpp"  // NOLINT
27 28
#endif

29
namespace phi {
30

31
class DenseTensorUtils;
L
LiYuRio 已提交
32 33 34
namespace distributed {
class DistTensor;
}  // namespace distributed
35

36
/// \brief The Dense tensor stores values in a contiguous sequential block
37 38 39 40 41 42 43 44 45 46
/// of memory where all values are represented. Tensors or multi-dimensional
/// arrays are used in math operators.
/// During the entire life cycle of a DenseTensor, its device type and key
/// metadata are set unchanged.
class DenseTensor : public TensorBase,
                    public TypeInfoTraits<TensorBase, DenseTensor> {
 public:
  /// \brief Construct a dense tensor and allocate space.
  /// \param a The allocator used to allocate space.
  /// \param meta The meta data of dense tensor.
47
  DenseTensor(Allocator* a, const DenseTensorMeta& meta);
48 49 50 51

  /// \brief Construct a dense tensor and allocate space.
  /// \param a The allocator used to allocate space.
  /// \param meta The meta data of dense tensor.
52
  DenseTensor(Allocator* a, DenseTensorMeta&& meta);
53

54
  DenseTensor(const std::shared_ptr<phi::Allocation>& holder,
55
              const DenseTensorMeta& meta);
56 57 58 59

  /// \brief Because dense tensor is a kind of container, we give a default
  /// constructor to use for stl container. But the dense tensor created with
  /// the default constructor is not practical.
60
  // DenseTensor() = default;
61 62 63 64 65

  /// \brief Because dense tensor is a resource handle, we provide a default
  /// move constructor to support move semantics.
  DenseTensor(DenseTensor&& other) = default;

66 67
  /// \brief DenseTensor shallow copy constructor.
  DenseTensor(const DenseTensor& other);
68

69 70 71
  /// \brief DenseTensor shallow copy assignment.
  DenseTensor& operator=(const DenseTensor& other);

72 73
  DenseTensor& operator=(DenseTensor&& other);

74 75
  DenseTensor();

76 77 78 79 80 81 82 83 84 85
  /// \brief Destroy the tensor object and release exclusive resources.
  virtual ~DenseTensor() = default;

 public:
  /// \brief Returns the name of the class for type traits.
  /// \return The name of the class.
  static const char* name() { return "DenseTensor"; }

  /// \brief Returns the number of elements contained in tensor.
  /// \return The number of elements contained in tensor.
86
  int64_t numel() const override;
87 88 89

  /// \brief Returns the dims of the tensor.
  /// \return The dims of the tensor.
90
  const DDim& dims() const noexcept override { return meta_.dims; }
91

W
wanghuancoder 已提交
92 93 94 95 96 97 98 99
  /// \brief Returns the stride of the tensor.
  /// \return The stride of the tensor.
  const DDim& strides() const noexcept { return meta_.strides; }

  /// \brief Sets the stride of the tensor.
  /// \param meta The stride of the tensor.
  void set_strides(const DDim& strides) { meta_.strides = strides; }

100 101
  /// \brief Returns the lod of the tensor.
  /// \return The lod of the tensor.
102
  const LoD& lod() const noexcept { return meta_.lod; }
103 104 105

  /// \brief Returns the data type of the tensor.
  /// \return The data type of the tensor.
106
  DataType dtype() const noexcept override { return meta_.dtype; }
107 108 109

  /// \brief Returns the data layout of the tensor.
  /// \return The data layout of the tensor.
110
  DataLayout layout() const noexcept override { return meta_.layout; }
111 112 113

  /// \brief Returns the data place of the tensor.
  /// \return The data place of the tensor.
114
  const Place& place() const override;
115 116 117 118 119

  /// \brief Returns the meta information of the tensor.
  /// \return The meta information of the tensor.
  const DenseTensorMeta& meta() const noexcept { return meta_; }

120 121 122 123 124
  /// \brief Sets the meta information of the tensor. Only when the original
  /// attribute of Tensor is incomplete, can it be reset.
  /// \param meta The meta information of the tensor.
  void set_meta(DenseTensorMeta&& meta);

125 126
  void set_meta(const DenseTensorMeta& meta);

127 128
  /// \brief Test whether the metadata is valid.
  /// \return Whether the metadata is valid.
129
  bool valid() const noexcept override { return meta_.valid(); }
130

131 132
  /// \brief Test whether the allocation is allocated.
  /// return Whether the allocation is allocated.
133
  bool initialized() const override { return holder_ && holder_->ptr(); }
134

135 136 137 138
  /// \brief Allocate memory with requested size from allocator.
  /// \return The mutable data pointer value of type T.
  void* AllocateFrom(Allocator* allocator,
                     DataType dtype,
139 140
                     size_t requested_size = 0,
                     bool fake_alloc = false) override;
141

142 143
  /// \brief Check if allocation is shared with other objects.
  /// \return Whether the allocation is shared with other objects.
144 145
  bool IsSharedWith(const DenseTensor& b) const;

146
  /// \brief Change the shape information in the metadata. If the new size is
147
  /// larger than the original value, the allocation area will be reallocated.
148
  /// \param dims The new dims of the dense tensor.
149
  /// \param lod The new lod of the dense tensor.
150
  // void Resize(const DDim& dims);
151 152 153
  void ResizeAndAllocate(const DDim& dims);

  DenseTensor& Resize(const DDim& dims);
154 155 156 157

  /// \brief Change the lod information in the metadata.
  /// \param lod The new lod of the dense tensor.
  void ResetLoD(const LoD& lod);
158

159 160
  /// \brief Returns the actual allocation size occupied by tensor, may be
  /// larger
161
  /// than its shape dims.
162
  /// \return The actual allocation size occupied by tensor.
163
  size_t capacity() const { return holder_->size(); }
164 165 166 167 168 169 170 171 172 173

  /// \brief Get the const data pointer value of type T.
  /// \return The const data pointer value of type T.
  template <typename T>
  const T* data() const;

  /// \brief Get the const data pointer value of raw type.
  /// \return The const data pointer value of raw type.
  const void* data() const;

174 175 176 177 178
  template <typename T>
  T* data();

  void* data();

179 180 181 182
  /// \brief Get whether the storage_properties is inited.
  /// \return The init status of storage_properties.
  bool storage_properties_initialized() const;

183 184 185 186 187 188 189 190 191 192
  /// \brief Returns the storage_properties of the tensor.
  /// \return The storage_properties of the tensor.
  template <typename DeviceT>
  const DeviceT& storage_properties() const;

  /// \brief Sets the storage_properties of the tensor.
  /// \param storage_properties The storage_properties of the tensor.
  void set_storage_properties(
      std::unique_ptr<StorageProperties>&& storage_properties);

Y
YuanRisheng 已提交
193 194 195 196 197
  void clear() {
    holder_.reset();
    meta_.offset = 0;
  }

198
 private:
199
  friend class DenseTensorUtils;
200
  friend class phi::distributed::DistTensor;
201

202
 protected:
203
  DenseTensorMeta meta_;
204
  std::shared_ptr<phi::Allocation> holder_;
205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  /** [ Why need StorageProperties? ]
   *
   * 1. Some hardware or third-party libraries add some additional storage
   * properties on top of the description of the basic DenseTensor, such as
   * memory desc of MKLDNN, storage_format and storage_layout of NPU,
   * these members are necessary for optimal performance, but if the properties
   * of each device are added to the DenseTensor with different macro isolation,
   * the memory layout of the DenseTensor will become more fragmented.
   * Under different compilation conditions, the member layout of the
   * DenseTensor is very unstable, which may introduce bugs that are difficult
   * to debug.
   *
   * 2. If the layout of DenseTensor is very different from the framework
   * itself, it is recommended to directly inherit TensorBase to implement
   * SpatialTensor.
   *
   * TODO(chenweihang): merge the dnnl::memory::desc and
   * dnnl::memory::format_tag into StorageProperties, dnnl::memory::desc is a
   * type that takes up a lot of space, original tensor members' size:
   *
   * DenseTensor size: 880
   * -------- ordered members --------:
   * DenseTensorMeta size: 128
   *  - is_scalar_ size: 1
   *  - DDim size: 80
   *  - DataType size: 4
   *  - DataLayout size: 4
   *  - LoD size: 24
   *  - offset size: 8
   *  std::shared_ptr<phi::Allocation> size: 16
   *  std::shared_ptr<InplaceVersion> size: 16 // need to be moved
   *  dnnl::memory::format_tag size: 4 // need to be moved
   *  dnnl::memory::desc size: 696 // need to be moved
   */
  std::unique_ptr<StorageProperties> storage_properties_{nullptr};

242 243 244 245
 public:
  /* Temporarily put InplaceVersion inside DenseTensor.
  Will move to AutogradMeta as soon as we switch to Eager Dygraph.
  */
L
Leo Chen 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  /*
  NOTE(liym27): [ What is TensorInplaceVersion used for? ]

  TensorInplaceVersion is a version counter and every Tensor has a version
  counter. It's used to check whether an inplace operation will result in an
  incorrect gradient calculation. Version is incremented when the data of the
  Variable is modified in place.

  - Question: In what scenarios will version counters be shared?
  - Answer: When two Variables/VarBases share the same C++ Tensor(its Allocation
  may change), both of them share the same version counter. For examples:
   1. `z = paddle.assign(input=x, output=y)`, `z` shares the same version
  counter of `y` because z and y is the same VarBase;
   2. `y = x.detach()`, `y` shares the same version counter of `x`.

  - Question: In what scenarios will version counters NOT be shared?
  - Answer: Replacing a `Variable`'s data by calling
  `Tensor::ShareDataWith(...)` or `Tensor::ShareBufferWith(...)`. Because they
264
  share the same Allocation but not phi::DenseTensor.
L
Leo Chen 已提交
265

266
  - Question: Why put the inplace_version_counter_ in phi::DenseTensor instead
L
Leo Chen 已提交
267 268 269 270 271 272 273 274 275
  of Allocation or Variable?
  - Answer:
   1. Tensor can call ResetHolder() to reset the corresponding Allocation so
  that the inplace_version_counter_ changes if it's in Allocation, which will
  lead to confusing information about inplace version.
   2. If inplace_version_counter_ is in Variable, different VariableWrappers
   should be able to share the same Variable. However, a VariableWrapper hold a
   Variable object but not a pointer.
 */
276 277 278 279 280 281 282 283 284 285 286 287
  class InplaceVersion {
   public:
    bool IsUnique() const { return inplace_version_ == 0; }
    void Bump() { ++inplace_version_; }
    uint32_t CurrentVersion() const { return inplace_version_; }
    void SetInplaceVersionToZero() { inplace_version_ = 0; }

   private:
    uint32_t inplace_version_{0};
  };

 protected:
R
risemeup1 已提交
288 289
  std::shared_ptr<InplaceVersion> inplace_version_counter_ =
      std::make_shared<InplaceVersion>();
290

291 292 293 294 295 296 297 298
/* @jim19930609: This is a hack
In general, it is badly designed to fuse MKLDNN-specific objects into a
generic Tensor.
We temporarily leave them here to unblock Tensor Unification progress.
In the final state, we should come up with a MKLDNN_Tensor and move the
following codes there.
*/
#ifdef PADDLE_WITH_MKLDNN
299 300
  /// \brief memory descriptor of tensor which have layout set as kMKLDNN
  dnnl::memory::desc mem_desc_;
301 302
#endif

303
#ifndef PADDLE_WITH_CUSTOM_KERNEL
304
#include "paddle/phi/core/dense_tensor.inl"
305
#endif
306
};
307

308
}  // namespace phi