未验证 提交 fa6b3c9a 编写于 作者: Z zyfncg 提交者: GitHub

[Phi] Remove Storage (#42872)

* remove storage

* add glog include

* add glog include

* add glog include
上级 9aed8327
......@@ -34,7 +34,6 @@ limitations under the License. */
#include "paddle/fluid/framework/python_headers.h"
#include "paddle/fluid/pybind/eager_op_function_impl.h"
#include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/core/string_tensor.h"
namespace paddle {
......
......@@ -45,7 +45,6 @@ typedef SSIZE_T ssize_t;
#include "paddle/fluid/pybind/tensor_py.h"
#include "paddle/phi/api/ext/op_meta_info.h"
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/api/lib/utils/tensor_utils.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/core/compat/convert_utils.h"
......
......@@ -13,6 +13,8 @@
// limitations under the License.
#include "paddle/infrt/tensor/phi/tensor_map.h"
#include "glog/logging.h"
#include "llvm/Support/ErrorHandling.h"
namespace infrt {
......
......@@ -18,7 +18,6 @@ limitations under the License. */
#include "paddle/phi/api/lib/data_transform.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/tensor_copy.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/common/type_traits.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/kernel_registry.h"
......
......@@ -15,7 +15,6 @@ limitations under the License. */
#pragma once
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/meta_tensor.h"
......
......@@ -18,6 +18,7 @@ limitations under the License. */
#include <unordered_map>
#include <vector>
#include "glog/logging.h"
#include "paddle/fluid/framework/custom_operator.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/enforce.h"
......
......@@ -17,7 +17,6 @@ limitations under the License. */
#include <memory>
#include "glog/logging.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/kernel_registry.h"
namespace paddle {
......
......@@ -15,7 +15,6 @@ limitations under the License. */
#include "paddle/phi/api/lib/tensor_copy.h"
#include "paddle/phi/api/lib/api_gen_utils.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/core/meta_tensor.h"
......
cc_library(phi_api_utils SRCS storage.cc tensor_utils.cc DEPS
cc_library(phi_api_utils SRCS tensor_utils.cc DEPS
tensor_base convert_utils dense_tensor lod_tensor selected_rows_utils place var_type_traits string_tensor int_array scalar)
......@@ -17,7 +17,6 @@ limitations under the License. */
#include "paddle/fluid/memory/allocation/allocator.h"
#include "paddle/fluid/memory/malloc.h"
#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/storage.h"
namespace paddle {
namespace experimental {
......
/* Copyright (c) 2021 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. */
#include "paddle/phi/api/lib/utils/storage.h"
namespace paddle {
namespace experimental {
ExternalStorage::ExternalStorage(void* ptr,
size_t size,
const phi::Place& place)
: phi::Storage(std::make_shared<phi::Allocation>(ptr, size, place)),
size_(size) {}
ExternalStorage::ExternalStorage(const phi::intrusive_ptr<phi::Storage>& root,
size_t delta,
size_t size)
: Storage(std::make_shared<phi::Allocation>(
static_cast<uint8_t*>(root->data()) + delta, size, root->place())),
size_(size) {
PADDLE_ENFORCE_LE(
static_cast<size_t>(delta + size),
root->size(),
phi::errors::InvalidArgument("The size of the external storage does "
"not meet the metadata requirements."));
}
} // namespace experimental
} // namespace paddle
/* Copyright (c) 2021 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. */
#pragma once
#include "paddle/fluid/memory/malloc.h"
#include "paddle/phi/core/storage.h"
namespace paddle {
namespace experimental {
class ExternalStorage : public phi::Storage {
public:
ExternalStorage(void* ptr, size_t size, const phi::Place& place);
ExternalStorage(const phi::intrusive_ptr<phi::Storage>& root,
size_t delta,
size_t size);
static const char* name() { return "ExternalStorage"; }
void Realloc(size_t n) override {
PADDLE_THROW(phi::errors::Unavailable(
"The external shared storage cannot be reallocated."));
}
void Clear() override {
data_ = nullptr;
size_ = 0;
}
void set_data_shared(
const std::shared_ptr<paddle::memory::Allocation>& holder) override {
CHECK(holder);
data_ = holder;
size_ = holder->size();
}
std::shared_ptr<paddle::memory::Allocation>&& move_data_shared() override {
size_ = 0;
return std::move(data_);
}
size_t size() const noexcept override { return size_; }
const phi::Place& place() const override {
PADDLE_ENFORCE_NOT_NULL(
data_,
phi::errors::Unavailable(
"Unable to visit place as data_ has not been initialized yet."));
return data_->place();
}
bool OwnsMemory() const noexcept override { return false; }
private:
int64_t size_{0};
};
class TensorStorage : public paddle::memory::allocation::Allocation {
public:
explicit TensorStorage(phi::intrusive_ptr<phi::Storage> storage)
: paddle::memory::allocation::Allocation(
storage->data(), storage->size(), storage->place()),
storage_(std::move(storage)) {}
private:
phi::intrusive_ptr<phi::Storage> storage_;
};
} // namespace experimental
} // namespace paddle
......@@ -20,7 +20,6 @@ limitations under the License. */
#include "paddle/fluid/framework/variable.h"
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/compat/convert_utils.h"
......
......@@ -15,7 +15,6 @@ limitations under the License. */
#pragma once
#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/storage.h"
#include "paddle/phi/core/stream.h"
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_meta.h"
......
......@@ -26,18 +26,6 @@ public:
*/
explicit DenseTensor(paddle::experimental::DataType dtype);
/// \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>
......
......@@ -18,9 +18,10 @@ limitations under the License. */
#include "paddle/phi/common/complex.h"
#include "paddle/phi/common/float16.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/compat/convert_utils.h"
#include "paddle/fluid/memory/malloc.h"
#ifdef PADDLE_WITH_MKLDNN
#include "paddle/fluid/platform/mkldnn_utils.h"
#endif
......@@ -211,13 +212,6 @@ LEGACY_DATA_MEMBER_FUNC_INSTANTIATION(::phi::dtype::complex<double>)
/* From framework::LoDTensor */
/* ------------------------------ */
DenseTensor::DenseTensor(intrusive_ptr<Storage> storage,
const DenseTensorMeta& meta)
: meta_(meta), holder_(storage->move_data_shared()) {}
DenseTensor::DenseTensor(intrusive_ptr<Storage> storage, DenseTensorMeta&& meta)
: meta_(std::move(meta)), holder_(storage->move_data_shared()) {}
DenseTensor::DenseTensor(const LoD& lod) : DenseTensor() { meta_.lod = lod; }
void DenseTensor::set_lod(const LoD& lod) { meta_.lod = lod; }
......
......@@ -21,6 +21,8 @@ limitations under the License. */
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_meta.h"
#include "glog/logging.h"
namespace phi {
// TODO(chenweihang): add other flags if needed
......
/* Copyright (c) 2021 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. */
#include "paddle/phi/core/storage.h"
namespace phi {
void TensorStorage::Realloc(size_t size) {
this->Clear();
data_ = alloc_->Allocate(size);
size_ = size;
}
} // namespace phi
/* Copyright (c) 2021 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. */
#pragma once
#include <cstddef>
#include "boost/intrusive_ptr.hpp"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/utils/intrusive_ptr.h"
#include "paddle/phi/core/utils/intrusive_ref_counter.h"
#include "paddle/phi/core/utils/type_info.h"
namespace phi {
/// \brief The interface of contiguous storage used for the dense tensor.
/// It should be used in conjunction with the intrusive pointer. We prohibit
/// all default copy operations to ensure the integrity of the package.
class Storage : public intrusive_ref_counter<Storage> {
public:
Storage() = default;
Storage(const Storage&) = delete;
/* @jim19930609: Following interfaces will be modified/replaced/removed
as soon as the new Allocation - Allocator design get
finalized.
*/
/* --------- shared_ptr<Allocation> -------- */
// Initialize a Storage with unique Allocation
explicit Storage(std::shared_ptr<phi::Allocation>&& data)
: data_(std::move(data)) {}
// Initialize a Storage shareing Allocation with another storage
explicit Storage(const std::shared_ptr<phi::Allocation>& data)
: data_(data) {}
void* data() const {
return data_ ? reinterpret_cast<void*>(
reinterpret_cast<uintptr_t>(data_->ptr()))
: nullptr;
}
const std::shared_ptr<phi::Allocation>& data_shared() const { return data_; }
virtual void set_data_shared(
const std::shared_ptr<phi::Allocation>& holder) = 0;
virtual std::shared_ptr<phi::Allocation>&& move_data_shared() = 0;
virtual void ReallocShared(size_t n) {
PADDLE_THROW(phi::errors::Unimplemented(
"ReallocShared has not been overrided by the current Storage"));
}
/* --------- shared_ptr<Allocation> -------- */
virtual ~Storage() = default;
virtual void Clear() = 0;
virtual size_t size() const = 0;
virtual const Place& place() const = 0;
virtual bool OwnsMemory() const = 0;
virtual void Realloc(size_t n) = 0;
protected:
std::shared_ptr<phi::Allocation> data_;
};
class TensorStorage : public Storage {
public:
explicit TensorStorage(Allocator* a) : alloc_(a) {}
TensorStorage(Allocator* a, size_t size)
: Storage(a->Allocate(size)), alloc_(a) {
size_ = data_->size();
}
void Clear() override {
data_ = nullptr;
size_ = 0;
}
void Realloc(size_t size) override;
~TensorStorage() = default;
static const char* name() { return "TensorStorage"; }
size_t size() const noexcept override { return size_; }
const Place& place() const override {
if (!data_) {
PADDLE_THROW(phi::errors::Unimplemented(
"Unable to visit place: either data_ or alloc_ has to be initialized "
"first."));
}
return data_->place();
}
bool OwnsMemory() const noexcept override { return true; }
void set_data_shared(
const std::shared_ptr<phi::Allocation>& holder) override {
CHECK(holder);
data_ = holder;
size_ = holder->size();
}
std::shared_ptr<phi::Allocation>&& move_data_shared() override {
size_ = 0;
return std::move(data_);
}
private:
Allocator* alloc_;
int64_t size_{0};
};
} // namespace phi
......@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/phi/core/string_tensor.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/fluid/memory/malloc.h"
namespace phi {
......
......@@ -16,7 +16,6 @@ limitations under the License. */
#include "paddle/phi/common/pstring.h"
#include "paddle/phi/core/allocator.h"
#include "paddle/phi/core/storage.h"
#include "paddle/phi/core/tensor_base.h"
#include "paddle/phi/core/tensor_meta.h"
......
......@@ -15,7 +15,7 @@
#include "paddle/phi/kernels/allclose_kernel.h"
#include <cmath>
#include "glog/logging.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
......
......@@ -20,7 +20,6 @@
#include "paddle/phi/core/visit_type.h"
#include "paddle/phi/kernels/cast_kernel.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
#include "paddle/phi/kernels/funcs/math_function.h"
......
......@@ -54,7 +54,6 @@ void UniformRandomRawKernel(const Context &dev_ctx,
float diag_val,
DenseTensor *out) {
out->Resize(phi::make_ddim(shape.GetData()));
VLOG(4) << out->dims();
T *data = dev_ctx.template Alloc<T>(out);
auto size = out->numel();
std::shared_ptr<std::mt19937_64> engine;
......
......@@ -14,7 +14,6 @@ limitations under the License. */
#pragma once
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/sparse_coo_tensor.h"
#include "paddle/phi/kernels/empty_kernel.h"
......
......@@ -14,7 +14,6 @@ limitations under the License. */
#pragma once
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/sparse_coo_tensor.h"
#include "paddle/phi/core/sparse_csr_tensor.h"
......
......@@ -22,6 +22,8 @@ limitations under the License. */
#include "paddle/phi/kernels/funcs/sparse/common_shape.h"
#include "paddle/phi/kernels/sparse/sparse_utils_kernel.h"
#include "paddle/fluid/platform/enforce.h"
namespace phi {
namespace sparse {
......
......@@ -14,7 +14,6 @@ limitations under the License. */
#pragma once
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/sparse_coo_tensor.h"
......
......@@ -15,6 +15,8 @@ limitations under the License. */
#include "paddle/phi/kernels/strings/strings_copy_kernel.h"
#include "paddle/phi/core/kernel_registry.h"
#include "glog/logging.h"
namespace phi {
namespace strings {
......
......@@ -15,7 +15,6 @@
#pragma once
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/core/string_tensor.h"
#include "paddle/phi/infermeta/strings/nullary.h"
......
......@@ -17,7 +17,6 @@ limitations under the License. */
#include <vector>
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/string_tensor.h"
#include "paddle/phi/infermeta/strings/unary.h"
#include "paddle/phi/kernels/strings/case_utils.h"
......
......@@ -19,7 +19,6 @@
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/kernel_registry.h"
......
......@@ -23,7 +23,6 @@ limitations under the License. */
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/phi/api/lib/utils/allocator.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/common/int_array.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/kernel_context.h"
......
......@@ -14,6 +14,7 @@ limitations under the License. */
#include "gtest/gtest.h"
#include "glog/logging.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/tests/core/allocator.h"
......
......@@ -14,6 +14,7 @@ limitations under the License. */
#include "gtest/gtest.h"
#include "glog/logging.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/sparse_coo_tensor.h"
#include "paddle/phi/tests/core/allocator.h"
......
......@@ -195,7 +195,6 @@ def source_include(header_file_path):
#include "paddle/phi/api/lib/api_gen_utils.h"
#include "paddle/phi/api/lib/data_transform.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/infermeta/binary.h"
#include "paddle/phi/infermeta/multiary.h"
......
......@@ -209,7 +209,6 @@ def source_include(header_file_path):
#include "paddle/phi/api/lib/api_gen_utils.h"
#include "paddle/phi/api/lib/data_transform.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/api/include/api.h"
#include "paddle/phi/infermeta/backward.h"
......
......@@ -44,7 +44,6 @@ def source_include(header_file_path):
#include "paddle/phi/api/lib/data_transform.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/sparse_api_custom_impl.h"
#include "paddle/phi/api/lib/utils/storage.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/infermeta/binary.h"
#include "paddle/phi/infermeta/multiary.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册