|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/pten/core/candidate/dense_tensor.h" |
| 16 | + |
| 17 | +namespace pten { |
| 18 | +namespace candidate { |
| 19 | + |
| 20 | +DenseTensorMeta::DenseTensorMeta(DataType type, const DDim& dims) |
| 21 | + : dims(dims), type(type) {} |
| 22 | +DenseTensorMeta::DenseTensorMeta(DataType type, |
| 23 | + const DDim& dims, |
| 24 | + DataLayout layout) |
| 25 | + : dims(dims), type(type), layout(layout) {} |
| 26 | +DenseTensorMeta::DenseTensorMeta(DataType type, |
| 27 | + const DDim& dims, |
| 28 | + DataLayout layout, |
| 29 | + const std::vector<std::vector<size_t>>& lod) |
| 30 | + : dims(dims), type(type), layout(layout), lod(lod) {} |
| 31 | + |
| 32 | +bool DenseTensorMeta::valid() const noexcept { |
| 33 | + bool valid{true}; |
| 34 | + valid = valid && (type != DataType::UNDEFINED); |
| 35 | + valid = valid && (layout != DataLayout::UNDEFINED); |
| 36 | + valid = valid && (is_scalar || product(dims)); |
| 37 | + return valid; |
| 38 | +} |
| 39 | + |
| 40 | +DenseTensor::DenseTensor(const std::shared_ptr<Allocator>& a, |
| 41 | + const DenseTensorMeta& meta) |
| 42 | + : meta_(meta), |
| 43 | + storage_( |
| 44 | + make_intrusive<TensorStorage>(a, SizeOf(data_type()) * numel())) {} |
| 45 | + |
| 46 | +DenseTensor::DenseTensor(const std::shared_ptr<Allocator>& a, |
| 47 | + DenseTensorMeta&& meta) |
| 48 | + : meta_(std::move(meta)), |
| 49 | + storage_( |
| 50 | + make_intrusive<TensorStorage>(a, SizeOf(data_type()) * numel())) {} |
| 51 | + |
| 52 | +DenseTensor::DenseTensor(intrusive_ptr<Storage> storage, |
| 53 | + const DenseTensorMeta& meta) |
| 54 | + : meta_(meta), storage_(std::move(storage)) {} |
| 55 | + |
| 56 | +DenseTensor::DenseTensor(intrusive_ptr<Storage> storage, DenseTensorMeta&& meta) |
| 57 | + : meta_(std::move(meta)), storage_(std::move(storage)) {} |
| 58 | + |
| 59 | +int64_t DenseTensor::numel() const { |
| 60 | + if (meta_.is_scalar) { |
| 61 | + return 1; |
| 62 | + } |
| 63 | + return product(meta_.dims); |
| 64 | +} |
| 65 | + |
| 66 | +bool DenseTensor::SharesStorageWith(const DenseTensor& b) const { |
| 67 | + return storage_.get() == b.storage_.get() && storage_.get() != nullptr; |
| 68 | +} |
| 69 | + |
| 70 | +template <typename T> |
| 71 | +T* DenseTensor::mutable_data(size_t request_bytes) { |
| 72 | + PADDLE_ENFORCE( |
| 73 | + valid(), |
| 74 | + paddle::platform::errors::PreconditionNotMet( |
| 75 | + "The meta data must be valid when call the mutable data function.")); |
| 76 | + PADDLE_ENFORCE_NOT_NULL( |
| 77 | + storage_, |
| 78 | + paddle::platform::errors::PreconditionNotMet( |
| 79 | + "The storage must be valid when call the mutable data function.")); |
| 80 | + PADDLE_ENFORCE( |
| 81 | + (data_type() == paddle::experimental::CppTypeToDataType<T>::Type()), |
| 82 | + paddle::platform::errors::PreconditionNotMet( |
| 83 | + "The type of data we are trying to retrieve does not match the " |
| 84 | + "type of data currently contained in the container.")); |
| 85 | + size_t bytes = numel() * SizeOf(data_type()); |
| 86 | + if (request_bytes) { |
| 87 | + PADDLE_ENFORCE_GE(request_bytes, |
| 88 | + bytes, |
| 89 | + paddle::platform::errors::InvalidArgument( |
| 90 | + "The reserved size %d should be enough to meet the " |
| 91 | + "volume required by metadata %d.", |
| 92 | + request_bytes, |
| 93 | + bytes)); |
| 94 | + bytes = request_bytes; |
| 95 | + } |
| 96 | + if (storage_->size() < bytes) { |
| 97 | + storage_->Realloc(bytes); |
| 98 | + } |
| 99 | + return static_cast<T*>(storage_->data()); |
| 100 | +} |
| 101 | + |
| 102 | +template <typename T> |
| 103 | +const T* DenseTensor::data() const { |
| 104 | + PADDLE_ENFORCE_NOT_NULL( |
| 105 | + storage_, |
| 106 | + paddle::platform::errors::PreconditionNotMet( |
| 107 | + "The storage must be valid when call the mutable data function.")); |
| 108 | + PADDLE_ENFORCE( |
| 109 | + (data_type() == paddle::experimental::CppTypeToDataType<T>::Type()), |
| 110 | + paddle::platform::errors::PreconditionNotMet( |
| 111 | + "The type of data we are trying to retrieve does not match the " |
| 112 | + "type of data currently contained in the container.")); |
| 113 | + return static_cast<const T*>(storage_->data()); |
| 114 | +} |
| 115 | + |
| 116 | +void DenseTensor::check_memory_size() const { |
| 117 | + size_t bytes = numel() * SizeOf(data_type()); |
| 118 | + PADDLE_ENFORCE_GE(memory_size(), |
| 119 | + bytes, |
| 120 | + paddle::platform::errors::InvalidArgument( |
| 121 | + "The memory size %d should be enough to meet the " |
| 122 | + "volume required by metadata %d.", |
| 123 | + memory_size(), |
| 124 | + bytes)); |
| 125 | +} |
| 126 | + |
| 127 | +#define DATA_MEMBER_FUNC_INSTANTIATION(dtype) \ |
| 128 | + template dtype* DenseTensor::mutable_data(size_t request_bytes); \ |
| 129 | + template const dtype* DenseTensor::data() const; |
| 130 | + |
| 131 | +DATA_MEMBER_FUNC_INSTANTIATION(int8_t); |
| 132 | +DATA_MEMBER_FUNC_INSTANTIATION(uint8_t); |
| 133 | +DATA_MEMBER_FUNC_INSTANTIATION(int16_t); |
| 134 | +DATA_MEMBER_FUNC_INSTANTIATION(uint16_t); |
| 135 | +DATA_MEMBER_FUNC_INSTANTIATION(int32_t); |
| 136 | +DATA_MEMBER_FUNC_INSTANTIATION(uint32_t); |
| 137 | +DATA_MEMBER_FUNC_INSTANTIATION(int64_t); |
| 138 | +DATA_MEMBER_FUNC_INSTANTIATION(uint64_t); |
| 139 | +DATA_MEMBER_FUNC_INSTANTIATION(float); |
| 140 | +DATA_MEMBER_FUNC_INSTANTIATION(double); |
| 141 | + |
| 142 | +#undef DATA_MEMBER_FUNC_INSTANTIATION |
| 143 | + |
| 144 | +} // namespace candidate |
| 145 | +} // namespace pten |
0 commit comments