dense_tensor.inl 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.

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

/* --------------------------- */
/*   From framework::Tensor    */
/* --------------------------- */
/* The following members & interfaces were copied from framework::Tensor,
    so as to facilitate the unification of different Tensors

    Will be adjusted/removed/moved in the near future
*/
public:
/* @jim19930609: Remove dependency on protobuf after Tensor Unification.
*/
26
explicit DenseTensor(paddle::experimental::DataType dtype);
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

/// \brief Use existing storage space to create dense tensor. This interface
/// can be used to deliberately create an uninitialized dense tensor.
/// \param storage The existing storage.
/// \param meta The meta data of dense tensor.
DenseTensor(intrusive_ptr<Storage> storage, const DenseTensorMeta& meta);

/// \brief Use existing storage space to create dense tensor. This interface
/// can be used to deliberately create an uninitialized dense tensor.
/// \param storage The existing storage.
/// \param meta The meta data of dense tensor.
DenseTensor(intrusive_ptr<Storage> storage, DenseTensorMeta&& meta);

inline bool IsInitialized() const { return holder_ != nullptr; }

template <typename T>
43
T* mutable_data(const phi::Place& place,
44 45 46 47
                size_t requested_size = 0);

template <typename T>
T* mutable_data(const DDim& dims,
48
                const phi::Place& place,
49 50
                size_t requested_size = 0);

51
void* mutable_data(const phi::Place& place,
52
                    paddle::experimental::DataType type,
53 54
                    size_t requested_size = 0);

55
void* mutable_data(const phi::Place& place,
56 57
                    size_t requested_size = 0);

58
void* mutable_data(const phi::Place& place,
59
                    paddle::experimental::DataType type,
60
                    const phi::Stream& stream);
61 62 63

/* @jim19930609: Remove dependency on protobuf after Tensor Unification.
*/
64
paddle::experimental::DataType type() const;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

// memory size returns the holding memory size in byte.
size_t memory_size() const;

void check_memory_size() const;

void set_layout(const paddle::framework::DataLayout layout);

void clear() {
  holder_.reset();
  meta_.offset = 0;
}

void ShareBufferWith(const DenseTensor& tensor);

void ShareDataTypeWith(const DenseTensor& tensor) {
  meta_.dtype = tensor.meta().dtype;
}

bool IsSharedBufferWith(const DenseTensor& src) const {
  return holder_ && holder_ == src.Holder();
}

88
const std::shared_ptr<phi::Allocation>& Holder() const { return holder_; }
89 90 91 92

void set_offset(size_t offset) { meta_.offset = offset; }
size_t offset() const { return meta_.offset; }

93
std::shared_ptr<phi::Allocation> MoveMemoryHolder() {
94 95 96
  return std::move(holder_);
}

97
void ResetHolder(const std::shared_ptr<phi::Allocation>& holder);
98

99
void ResetHolderWithType(const std::shared_ptr<phi::Allocation>& holder,
100
                        paddle::experimental::DataType type);
101

102
void set_type(paddle::experimental::DataType type);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

InplaceVersion& InplaceVersionCounter() {
  return *inplace_version_counter_;
}

/*! The internal of two tensors share the same memory block. */
DenseTensor& ShareDataWith(const DenseTensor& src);

/*! The internal of two tensors share the same inplace version counter. */
DenseTensor& ShareInplaceVersionCounterWith(const DenseTensor& src);

DenseTensor Slice(int64_t begin_idx, int64_t end_idx) const;

std::vector<DenseTensor> Split(int64_t split_size, int64_t axis) const;

std::vector<DenseTensor> Chunk(int64_t chunks, int64_t axis) const;

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

public:
inline dnnl::memory::format_tag format() const { return format_; }

inline void set_format(const dnnl::memory::format_tag format) {
  format_ = format;
}

#endif

/* ------------------------------ */
/*   From framework::LoDTensor    */
/* ------------------------------ */
/* The following members & interfaces were copied from framework::Tensor,
    so as to facilitate the unification of different Tensors

    Will be adjusted/removed/moved in the near future
*/
public:
explicit DenseTensor(const LoD& lod);

void set_lod(const LoD& lod);

LoD* mutable_lod();

/*
* Get the start offset and end offset of an  element from LoD.
*/
std::pair<size_t, size_t> lod_element(size_t level, size_t elem) const;

size_t NumLevels() const;

size_t NumElements(size_t level = 0) const;