dense_tensor.h 7.5 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 18 19 20
#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/stream.h"
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_meta.h"
21

22
/* @jim19930609: Move to MKLDNN_Tensor in the future
23
 */
24 25 26 27
#ifdef PADDLE_WITH_MKLDNN
#include "dnnl.hpp"
#endif

28
namespace phi {
29

30
class DenseTensorUtils;
31

32 33 34 35 36 37 38 39 40 41 42
/// \brief The Dense tensor store values in a contiguous sequential block
/// 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.
43
  DenseTensor(Allocator* a, const DenseTensorMeta& meta);
44 45 46 47

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

50
  DenseTensor(const std::shared_ptr<phi::Allocation>& holder,
51
              const DenseTensorMeta& meta);
52 53 54 55

  /// \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.
56
  // DenseTensor() = default;
57 58 59 60 61

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

62 63
  /// \brief DenseTensor shallow copy constructor.
  DenseTensor(const DenseTensor& other);
64

65 66 67
  /// \brief DenseTensor shallow copy assignment.
  DenseTensor& operator=(const DenseTensor& other);

68 69
  DenseTensor& operator=(DenseTensor&& other);

70 71
  DenseTensor();

72 73 74 75 76 77 78 79 80 81
  /// \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.
82
  int64_t numel() const override;
83 84 85

  /// \brief Returns the dims of the tensor.
  /// \return The dims of the tensor.
86
  const DDim& dims() const noexcept override { return meta_.dims; }
87 88 89

  /// \brief Returns the lod of the tensor.
  /// \return The lod of the tensor.
90
  const LoD& lod() const noexcept { return meta_.lod; }
91 92 93

  /// \brief Returns the data type of the tensor.
  /// \return The data type of the tensor.
94
  DataType dtype() const noexcept override { return meta_.dtype; }
95 96 97

  /// \brief Returns the data layout of the tensor.
  /// \return The data layout of the tensor.
98
  DataLayout layout() const noexcept override { return meta_.layout; }
99 100 101

  /// \brief Returns the data place of the tensor.
  /// \return The data place of the tensor.
102
  const Place& place() const override;
103 104 105 106 107

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

108 109 110 111 112
  /// \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);

113 114
  void set_meta(const DenseTensorMeta& meta);

115 116
  /// \brief Test whether the metadata is valid.
  /// \return Whether the metadata is valid.
117
  bool valid() const noexcept override { return meta_.valid(); }
118

119 120
  /// \brief Test whether the allocation is allocated.
  /// return Whether the allocation is allocated.
121
  bool initialized() const override { return holder_ && holder_->ptr(); }
122

123 124 125 126 127 128
  /// \brief Allocate memory with requested size from allocator.
  /// \return The mutable data pointer value of type T.
  void* AllocateFrom(Allocator* allocator,
                     DataType dtype,
                     size_t requested_size = 0) override;

129 130
  /// \brief Check if allocation is shared with other objects.
  /// \return Whether the allocation is shared with other objects.
131 132
  bool IsSharedWith(const DenseTensor& b) const;

133
  /// \brief Change the shape information in the metadata. If the new size is
134
  /// larger than the original value, the allocation area will be reallocated.
135
  /// \param dims The new dims of the dense tensor.
136
  /// \param lod The new lod of the dense tensor.
137
  // void Resize(const DDim& dims);
138 139 140
  void ResizeAndAllocate(const DDim& dims);

  DenseTensor& Resize(const DDim& dims);
141 142 143 144

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

146 147
  /// \brief Returns the actual allocation size occupied by tensor, may be
  /// larger
148
  /// than its shape dims.
149
  /// \return The actual allocation size occupied by tensor.
150
  size_t capacity() const { return holder_->size(); }
151 152 153 154 155 156 157 158 159 160

  /// \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;

161 162 163 164 165
  template <typename T>
  T* data();

  void* data();

166
 private:
167
  friend class DenseTensorUtils;
168

169
 protected:
170
  DenseTensorMeta meta_;
171
  std::shared_ptr<phi::Allocation> holder_;
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
 public:
  /* Temporarily put InplaceVersion inside DenseTensor.
  Will move to AutogradMeta as soon as we switch to Eager Dygraph.
  */
  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:
  std::shared_ptr<InplaceVersion> inplace_version_counter_{
      std::make_shared<InplaceVersion>()};

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
/* @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
  /**
   * @brief the detail format of memory block which have layout as kMKLDNN
   *
   * @note MKLDNN lib support various memory format like nchw, nhwc, nChw8C,
   *       nChw16c, etc. For a MKLDNN memory block, layout will be set as
   *       DataLayout::kMKLDNN meanwhile detail memory format will be kept in
   *       this field.
   */
  dnnl::memory::format_tag format_ = dnnl::memory::format_tag::undef;
209 210 211

  /// \brief memory descriptor of tensor which have layout set as kMKLDNN
  dnnl::memory::desc mem_desc_;
212 213
#endif

214
#ifndef PADDLE_WITH_CUSTOM_KERNEL
215
#include "paddle/phi/core/dense_tensor.inl"
216
#endif
217
};
218

219
}  // namespace phi