diff --git a/paddle/fluid/eager/tests/data_structure_tests/accumulation_node_test.cc b/paddle/fluid/eager/tests/data_structure_tests/accumulation_node_test.cc index f249d2099f24cc3dd9c57c233113a398ff5389cc..cdc970100951362eaa30692142b674a74a19064f 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/accumulation_node_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/accumulation_node_test.cc @@ -32,15 +32,17 @@ TEST(AccumulationNode, EagerTensor) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT16, paddle::framework::make_ddim({1, 1})); std::shared_ptr dt0 = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); dt0->mutable_data()[0] = 10.0; EagerTensor et0 = EagerTensor(dt0); std::shared_ptr dt1 = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); dt1->mutable_data()[0] = 20.0; @@ -48,8 +50,9 @@ TEST(AccumulationNode, EagerTensor) { std::shared_ptr grad_dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); EagerTensor grad_et = EagerTensor(grad_dt); diff --git a/paddle/fluid/eager/tests/data_structure_tests/autograd_meta_test.cc b/paddle/fluid/eager/tests/data_structure_tests/autograd_meta_test.cc index 96845569ca0c5c5dfee6e377e2fca90c1bc697b4..3d45dc831d41124351cfa74507dd7c1371546186 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/autograd_meta_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/autograd_meta_test.cc @@ -42,8 +42,9 @@ TEST(AutogradMeta, MemberFunction) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); dt_ptr[0] = 5.0f; diff --git a/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc b/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc index 84daf4eac4ce694a89ab2508597a8cde1548f604..a483ddb6a98f6ddf0262a0c56cc5051872865047 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/eager_tensor_test.cc @@ -36,8 +36,9 @@ TEST(EagerTensor, Constructor) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); dt_ptr[0] = 5.0f; @@ -65,8 +66,9 @@ TEST(EagerTensor, MemberFunction) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); dt_ptr[0] = 5.0f; diff --git a/paddle/fluid/eager/tests/data_structure_tests/grad_node_info_test.cc b/paddle/fluid/eager/tests/data_structure_tests/grad_node_info_test.cc index a89fb019d5b37ccff090374c7dc406471470f392..7f6609b88a5275379e26f71e2bdc706e5cdef345 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/grad_node_info_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/grad_node_info_test.cc @@ -41,8 +41,9 @@ TEST(GradNodeInfo, GradNodeBase) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); dt_ptr[0] = 5.0f; @@ -97,8 +98,9 @@ TEST(GradNodeInfo, GradNodeBase) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); dt_ptr[0] = 6.0f; diff --git a/paddle/fluid/eager/tests/data_structure_tests/grad_node_test.h b/paddle/fluid/eager/tests/data_structure_tests/grad_node_test.h index 2870bfa8b0c943b7f0dfae5b10c80035fb8f8f21..433a00e27be0e90800f1dffedf19b358c8fc9f56 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/grad_node_test.h +++ b/paddle/fluid/eager/tests/data_structure_tests/grad_node_test.h @@ -37,8 +37,9 @@ class GradTestNode : public egr::GradNodeBase { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); dt_ptr[0] = 6.0f; diff --git a/paddle/fluid/eager/tests/data_structure_tests/grad_tensor_holder_test.cc b/paddle/fluid/eager/tests/data_structure_tests/grad_tensor_holder_test.cc index 3581ef59cd5bee409ef27035954de6319d65a680..c88a5f5fdcef5701ca007d8a67682239922ee59a 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/grad_tensor_holder_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/grad_tensor_holder_test.cc @@ -36,8 +36,9 @@ TEST(GradTensorHolder, Constructor) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({2, 2})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); EagerTensor et = EagerTensor(dt); @@ -52,15 +53,17 @@ TEST(GradTensorHolder, Interfaces) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1})); std::shared_ptr dt0 = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); dt0->mutable_data()[0] = 10.0; EagerTensor et0 = EagerTensor(dt0); std::shared_ptr dt1 = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); dt1->mutable_data()[0] = 20.0; EagerTensor et1 = EagerTensor(dt1); diff --git a/paddle/fluid/eager/tests/data_structure_tests/tensor_wrapper_test.cc b/paddle/fluid/eager/tests/data_structure_tests/tensor_wrapper_test.cc index 6d78cf42d0c48a25ae3e3d0ea9016e193de2a203..8bc739d455a958c43a581dde19c4c3be850a7caa 100644 --- a/paddle/fluid/eager/tests/data_structure_tests/tensor_wrapper_test.cc +++ b/paddle/fluid/eager/tests/data_structure_tests/tensor_wrapper_test.cc @@ -25,8 +25,9 @@ TEST(TensorWrapper, Basic) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2})); std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); dt_ptr[0] = 5.0f; @@ -51,8 +52,9 @@ TEST(TensorWrapper, Basic) { pten::DenseTensorMeta meta2 = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 2})); std::shared_ptr dt2 = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta2); auto* dt_ptr2 = dt->mutable_data(); dt_ptr2[0] = 6.0f; diff --git a/paddle/fluid/eager/tests/task_tests/eager_utils_test.cc b/paddle/fluid/eager/tests/task_tests/eager_utils_test.cc index ea9aae83ff1891341471c9c6316d83b209a7c714..1b2f1287b069d9ef905f0e46336cddaf5a7d551b 100644 --- a/paddle/fluid/eager/tests/task_tests/eager_utils_test.cc +++ b/paddle/fluid/eager/tests/task_tests/eager_utils_test.cc @@ -31,15 +31,17 @@ TEST(EagerUtils, AutoGradMeta) { pten::DenseTensorMeta meta = pten::DenseTensorMeta( pten::DataType::FLOAT32, paddle::framework::make_ddim({1, 1})); std::shared_ptr dt0 = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); dt0->mutable_data()[0] = 10.0; EagerTensor et0 = EagerTensor(dt0); std::shared_ptr dt1 = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); dt1->mutable_data()[0] = 20.0; EagerTensor et1 = EagerTensor(dt1); @@ -106,8 +108,9 @@ egr::EagerTensor CreateTestCPUTensor(T val, pten::DenseTensorMeta(pten::DataType::FLOAT32, ddim); egr::EagerTensor tensor; std::shared_ptr dt = std::make_shared( - std::make_shared( - paddle::platform::CPUPlace()), + std::make_unique( + paddle::platform::CPUPlace()) + .get(), meta); auto* dt_ptr = dt->mutable_data(); for (int64_t i = 0; i < dt->numel(); i++) { diff --git a/paddle/pten/api/lib/utils/allocator.h b/paddle/pten/api/lib/utils/allocator.h index a8c05b76516892786a118f55763bc43e3d7f8742..acdba822ac4bb1c9b956249eb9d85a8a92f20b5e 100644 --- a/paddle/pten/api/lib/utils/allocator.h +++ b/paddle/pten/api/lib/utils/allocator.h @@ -22,25 +22,15 @@ limitations under the License. */ namespace paddle { namespace experimental { -class DefaultAllocator : public pten::deprecated::Allocator { +class DefaultAllocator : public pten::Allocator { public: - using Allocation = pten::deprecated::Allocation; explicit DefaultAllocator(const paddle::platform::Place& place) : place_(place) {} - static void Delete(Allocation* allocation) { - paddle::memory::allocation::Allocator::AllocationDeleter( - allocation->CastContextWithoutCheck()); + AllocationPtr Allocate(size_t bytes_size) override { + return memory::Alloc(place_, bytes_size); } - Allocation Allocate(size_t bytes_size) override { - paddle::memory::AllocationPtr a = memory::Alloc(place_, bytes_size); - void* ptr = a->ptr(); - return Allocation(ptr, a.release(), &Delete, place_); - } - - const paddle::platform::Place& place() override { return place_; } - private: paddle::platform::Place place_; }; diff --git a/paddle/pten/core/dense_tensor.cc b/paddle/pten/core/dense_tensor.cc index eb6f834d7277901b41ad797625a583f4212e98c0..716e1ac3d30bbaa94671ac00d395d99fce8c3fd3 100644 --- a/paddle/pten/core/dense_tensor.cc +++ b/paddle/pten/core/dense_tensor.cc @@ -24,13 +24,11 @@ limitations under the License. */ namespace pten { -DenseTensor::DenseTensor(const std::shared_ptr& a, - const DenseTensorMeta& meta) +DenseTensor::DenseTensor(Allocator* a, const DenseTensorMeta& meta) : meta_(meta), storage_(make_intrusive(a, SizeOf(dtype()) * numel())) {} -DenseTensor::DenseTensor(const std::shared_ptr& a, - DenseTensorMeta&& meta) +DenseTensor::DenseTensor(Allocator* a, DenseTensorMeta&& meta) : meta_(std::move(meta)), storage_(make_intrusive(a, SizeOf(dtype()) * numel())) {} diff --git a/paddle/pten/core/dense_tensor.h b/paddle/pten/core/dense_tensor.h index 4f25fc296724c86cd844d21c262681c9d24037b2..db8d7a2a39c908e2e439d8dcb5b07f49bfddd796 100644 --- a/paddle/pten/core/dense_tensor.h +++ b/paddle/pten/core/dense_tensor.h @@ -60,17 +60,15 @@ class TensorInplaceVersion { class DenseTensor : public TensorBase, public TypeInfoTraits { public: - using Allocator = deprecated::Allocator; - /// \brief Construct a dense tensor and allocate space. /// \param a The allocator used to allocate space. /// \param meta The meta data of dense tensor. - DenseTensor(const std::shared_ptr& a, const DenseTensorMeta& meta); + DenseTensor(Allocator* a, const DenseTensorMeta& meta); /// \brief Construct a dense tensor and allocate space. /// \param a The allocator used to allocate space. /// \param meta The meta data of dense tensor. - DenseTensor(const std::shared_ptr& a, DenseTensorMeta&& meta); + DenseTensor(Allocator* a, DenseTensorMeta&& meta); /// \brief Use existing storage space to create dense tensor. This interface /// can be used to deliberately create an uninitialized dense tensor. diff --git a/paddle/pten/core/storage.cc b/paddle/pten/core/storage.cc index f7c7f6873410179575050ccdc88e7002aeb936c7..aacae7be883493fa3cf837dc772e3ef2891e69fc 100644 --- a/paddle/pten/core/storage.cc +++ b/paddle/pten/core/storage.cc @@ -18,7 +18,7 @@ namespace pten { void TensorStorage::Realloc(size_t size) { this->Clear(); - data_ = paddle::memory::AllocShared(alloc_->place(), size); + data_ = alloc_->Allocate(size); size_ = size; } diff --git a/paddle/pten/core/storage.h b/paddle/pten/core/storage.h index cf18dd913093a1f9e46ac67d78e03ed9c0a1b78b..97d7f8d0f110534c9279c5c5717e0151314a60c9 100644 --- a/paddle/pten/core/storage.h +++ b/paddle/pten/core/storage.h @@ -91,12 +91,11 @@ class Storage : public intrusive_ref_counter { class TensorStorage : public Storage { public: using Place = paddle::platform::Place; - using Allocator = deprecated::Allocator; - explicit TensorStorage(const std::shared_ptr& a) : alloc_(a) {} + explicit TensorStorage(Allocator* a) : alloc_(a) {} - TensorStorage(const std::shared_ptr& a, size_t size) - : Storage(paddle::memory::AllocShared(a->place(), size)), alloc_(a) { + TensorStorage(Allocator* a, size_t size) + : Storage(a->Allocate(size)), alloc_(a) { size_ = data_->size(); } @@ -114,24 +113,18 @@ class TensorStorage : public Storage { size_t size() const noexcept override { return size_; } const Place& place() const override { - if (!data_ && !alloc_) { + if (!data_) { PADDLE_THROW(paddle::platform::errors::Unimplemented( "Unable to visit place: either data_ or alloc_ has to be initialized " "first.")); } - if (data_) { - return data_->place(); - } - return alloc_->place(); + return data_->place(); } bool OwnsMemory() const noexcept override { return true; } - const std::shared_ptr& allocator() const noexcept { - return alloc_; - } private: - const std::shared_ptr alloc_; + Allocator* alloc_; int64_t size_{0}; }; diff --git a/paddle/pten/tests/api/CMakeLists.txt b/paddle/pten/tests/api/CMakeLists.txt index ffbc551843148631e82b89f82fb7ae0e8e305eac..79d9a3d82e69ed8c403fadc797e6397bca3dc30f 100644 --- a/paddle/pten/tests/api/CMakeLists.txt +++ b/paddle/pten/tests/api/CMakeLists.txt @@ -5,8 +5,6 @@ else() endif() cc_test(test_pten_exception SRCS test_pten_exception.cc DEPS gtest) -cc_test(test_framework_storage SRCS test_storage.cc DEPS pten_api_utils) -cc_test(test_framework_tensor_utils SRCS test_tensor_utils.cc DEPS pten_api_utils) cc_test(test_mean_api SRCS test_mean_api.cc DEPS pten_tensor pten_api pten_api_utils) cc_test(test_dot_api SRCS test_dot_api.cc DEPS pten_tensor pten_api pten_api_utils) diff --git a/paddle/pten/tests/api/test_cast_api.cc b/paddle/pten/tests/api/test_cast_api.cc index c2660a1f800196f596799168dede9bcecf57e6a4..6608d1ed08cab5b8c2db9a0197bdd9a10ba96d4c 100644 --- a/paddle/pten/tests/api/test_cast_api.cc +++ b/paddle/pten/tests/api/test_cast_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, cast) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_conj_api.cc b/paddle/pten/tests/api/test_conj_api.cc index 928f8e414fda0f7216dd05e3270347d1a7a2ec98..50d190257a16dc4d7a605f0e4d3662b5f45a6dfd 100644 --- a/paddle/pten/tests/api/test_conj_api.cc +++ b/paddle/pten/tests/api/test_conj_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, conj) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::COMPLEX64, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_dot_api.cc b/paddle/pten/tests/api/test_dot_api.cc index 41c03f8f262019a7e969a8716cf8cb4cd22a6174..40e709b960334b034f3aa46ef72ddeb02436b5fb 100644 --- a/paddle/pten/tests/api/test_dot_api.cc +++ b/paddle/pten/tests/api/test_dot_api.cc @@ -30,17 +30,17 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, dot) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x->mutable_data(); auto dense_y = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_elementwise_api.cc b/paddle/pten/tests/api/test_elementwise_api.cc index e5971aae5513fd22eb7a6658ed0a5ad47a0cec3a..69af32eb457a6ea7ef5bbbb80c33fd2276febb7b 100644 --- a/paddle/pten/tests/api/test_elementwise_api.cc +++ b/paddle/pten/tests/api/test_elementwise_api.cc @@ -30,17 +30,17 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, add) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x->mutable_data(); auto dense_y = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); @@ -84,17 +84,17 @@ TEST(API, add) { // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, subtract) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x->mutable_data(); auto dense_y = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); @@ -138,17 +138,17 @@ TEST(API, subtract) { // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, divide) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x->mutable_data(); auto dense_y = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); @@ -192,17 +192,17 @@ TEST(API, divide) { TEST(API, multiply) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x->mutable_data(); auto dense_y = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_empty_api.cc b/paddle/pten/tests/api/test_empty_api.cc index fcc01ad8a71720b22cd82931e6219008b45c39d7..f4e3f472c7990de4829017f50fb0530aeb4a62db 100644 --- a/paddle/pten/tests/api/test_empty_api.cc +++ b/paddle/pten/tests/api/test_empty_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, empty_like) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2}), pten::DataLayout::NCHW)); @@ -55,11 +55,11 @@ TEST(API, empty_like) { TEST(API, empty1) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_shape = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::INT64, framework::make_ddim({2}), pten::DataLayout::NCHW)); @@ -83,11 +83,11 @@ TEST(API, empty1) { } TEST(API, empty2) { - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_scalar = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::INT32, framework::make_ddim({1}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_fill_api.cc b/paddle/pten/tests/api/test_fill_api.cc index e87d094eec9d34af9928a50b0ddc62af0555e5d8..0d823765680e8a05c0e054a9b0a44deedd1b58c0 100644 --- a/paddle/pten/tests/api/test_fill_api.cc +++ b/paddle/pten/tests/api/test_fill_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, full_like) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2}), pten::DataLayout::NCHW)); @@ -65,10 +65,10 @@ TEST(API, full_like) { TEST(API, zeros_like) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2}), pten::DataLayout::NCHW)); @@ -98,10 +98,10 @@ TEST(API, zeros_like) { TEST(API, ones_like) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::INT32, framework::make_ddim({3, 2}), pten::DataLayout::NCHW)); @@ -131,11 +131,11 @@ TEST(API, ones_like) { TEST(API, full1) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_shape = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::INT64, framework::make_ddim({2}), pten::DataLayout::NCHW)); @@ -144,7 +144,7 @@ TEST(API, full1) { shape_data[1] = 3; auto dense_scalar = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({1}), pten::DataLayout::NCHW)); @@ -177,11 +177,11 @@ TEST(API, full1) { } TEST(API, full2) { - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_scalar = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::INT32, framework::make_ddim({1}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_flatten_api.cc b/paddle/pten/tests/api/test_flatten_api.cc index 93c8a50f02a7827f20c89a2245fd924b83b92497..6c082b9653e6f9b0ab0b09016938b9b3bfe65159 100644 --- a/paddle/pten/tests/api/test_flatten_api.cc +++ b/paddle/pten/tests/api/test_flatten_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, flatten) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2, 2, 3}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_matmul_api.cc b/paddle/pten/tests/api/test_matmul_api.cc index bef0e2af4cf920ba6f2b90309b13ab6ccc03bdf6..03f686f1c3f5e68e421b6068baab5d320763c198 100644 --- a/paddle/pten/tests/api/test_matmul_api.cc +++ b/paddle/pten/tests/api/test_matmul_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; TEST(API, matmul_cpu) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); @@ -41,7 +41,7 @@ TEST(API, matmul_cpu) { auto* dense_x_data = dense_x->mutable_data(); auto dense_y = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); @@ -79,10 +79,10 @@ TEST(API, matmul_cpu) { TEST(API, matmul_cuda) { // Prepare CPU Dense Tensor const auto alloc_cpu = - std::make_shared( + std::make_unique( paddle::platform::CPUPlace()); auto ref_x = std::make_shared( - alloc_cpu, + alloc_cpu.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); @@ -90,7 +90,7 @@ TEST(API, matmul_cuda) { auto* ref_x_data = ref_x->mutable_data(); auto ref_y = std::make_shared( - alloc_cpu, + alloc_cpu.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); @@ -104,16 +104,16 @@ TEST(API, matmul_cuda) { // 1. create tensor const auto alloc_cuda = - std::make_shared( + std::make_unique( paddle::platform::CUDAPlace()); auto dense_x = std::make_shared( - alloc_cuda, + alloc_cuda.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); auto dense_y = std::make_shared( - alloc_cuda, + alloc_cuda.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); @@ -143,7 +143,7 @@ TEST(API, matmul_cuda) { auto dense_out = std::dynamic_pointer_cast(out.impl()); auto ref_out = std::make_shared( - alloc_cpu, + alloc_cpu.get(), pten::DenseTensorMeta( pten::DataType::FLOAT32, out.dims(), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_mean_api.cc b/paddle/pten/tests/api/test_mean_api.cc index a8c4c5306dced7c63a823d9e2b463f65a2a31b01..9d90e58101cbd8c4f5d710728ffc1c660226822d 100644 --- a/paddle/pten/tests/api/test_mean_api.cc +++ b/paddle/pten/tests/api/test_mean_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, mean) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_reshape_api.cc b/paddle/pten/tests/api/test_reshape_api.cc index 227dcc6e9568d7c44730eeab70dca27cace1f482..59e9e9fab1122ce4a448281937a9f9a944cba37c 100644 --- a/paddle/pten/tests/api/test_reshape_api.cc +++ b/paddle/pten/tests/api/test_reshape_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, reshape) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2, 2, 3}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_storage.cc b/paddle/pten/tests/api/test_storage.cc deleted file mode 100644 index 1a5d95f9419c5b8068448184abfce72582032b53..0000000000000000000000000000000000000000 --- a/paddle/pten/tests/api/test_storage.cc +++ /dev/null @@ -1,65 +0,0 @@ -/* 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 - -#include "gtest/gtest.h" - -#include "paddle/pten/api/lib/utils/allocator.h" -#include "paddle/pten/api/lib/utils/storage.h" - -namespace paddle { -namespace tests { - -TEST(host_storage, external_stroage) { - const size_t size{100}; - const auto a = std::make_shared( - paddle::platform::CPUPlace()); - pten::intrusive_ptr in_storage = - pten::make_intrusive(a, size); - char* data = static_cast(in_storage->data()); - for (size_t i = 0; i < size; ++i) { - data[i] = i; - } - const size_t delta{1}; - const size_t n{10}; - auto ex_storage = - pten::make_intrusive(in_storage, delta, n); - CHECK_EQ(ex_storage->size(), n); - CHECK(paddle::platform::is_cpu_place(ex_storage->place())); - CHECK(!ex_storage->OwnsMemory()); - for (size_t i = delta; i < delta + n; ++i) { - CHECK_EQ(data[i], static_cast(i)); - } -} - -TEST(host_storage, external_vector) { - std::vector data(100); - for (size_t i = 0; i < data.size(); ++i) { - data[i] = i; - } - const size_t delta{1}; - const size_t n{10}; - auto ex_storage = pten::make_intrusive( - data.data(), n, paddle::platform::CPUPlace()); - CHECK_EQ(ex_storage->size(), n); - CHECK(paddle::platform::is_cpu_place(ex_storage->place())); - CHECK(!ex_storage->OwnsMemory()); - for (size_t i = delta; i < delta + n; ++i) { - CHECK_EQ(data[i], static_cast(i)); - } -} - -} // namespace tests -} // namespace paddle diff --git a/paddle/pten/tests/api/test_sum_api.cc b/paddle/pten/tests/api/test_sum_api.cc index ff1609d3d4051b92a713aad5464a9cac1a446452..5a7c9840e11143f71ffae811c9dbfd94306d5f73 100644 --- a/paddle/pten/tests/api/test_sum_api.cc +++ b/paddle/pten/tests/api/test_sum_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(API, sum) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/api/test_tensor_utils.cc b/paddle/pten/tests/api/test_tensor_utils.cc deleted file mode 100644 index 041bd28ad892a32c4a33ad031711b79f8358ec9b..0000000000000000000000000000000000000000 --- a/paddle/pten/tests/api/test_tensor_utils.cc +++ /dev/null @@ -1,124 +0,0 @@ -/* 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 "gtest/gtest.h" - -#include "paddle/pten/api/lib/utils/tensor_utils.h" -#include "paddle/pten/core/tensor_meta.h" - -namespace paddle { -namespace tests { - -using DDim = paddle::framework::DDim; -using DataType = paddle::experimental::DataType; -using DataLayout = paddle::experimental::DataLayout; - -using DenseTensor = pten::DenseTensor; -using DenseTensorMeta = pten::DenseTensorMeta; - -TEST(tensor_utils, dense_tensor_to_lod_tensor) { - const DDim dims({2, 1}); - const DataType dtype{DataType::FLOAT32}; - const DataLayout layout{DataLayout::NCHW}; - const pten::LoD lod{{0, 2}}; - DenseTensorMeta meta(dtype, dims, layout, lod); - - auto alloc = - std::make_shared(platform::CPUPlace()); - - DenseTensor dense_tensor(alloc, meta); - float* data = dense_tensor.mutable_data(); - data[0] = 1.0f; - data[1] = 2.1f; - - framework::LoDTensor lod_tensor; - experimental::MovesStorage(&dense_tensor, &lod_tensor); - - CHECK(dense_tensor.lod().size() == lod_tensor.lod().size()); - CHECK(dense_tensor.lod()[0] == - static_cast>((lod_tensor.lod()[0]))); - CHECK(dense_tensor.dtype() == pten::TransToPtenDataType(lod_tensor.type())); - CHECK(dense_tensor.layout() == lod_tensor.layout()); - CHECK(platform::is_cpu_place(lod_tensor.place())); - - CHECK(lod_tensor.data()[0] == 1.0f); - CHECK(lod_tensor.data()[1] == 2.1f); - - auto dense_tensor_1 = experimental::MakePtenDenseTensor(lod_tensor); - CHECK(dense_tensor_1->dims() == dims); - CHECK(dense_tensor_1->dtype() == dtype); - CHECK(dense_tensor_1->layout() == layout); - CHECK(dense_tensor_1->lod().size() == lod.size()); - CHECK(dense_tensor_1->lod()[0] == lod[0]); - const float* data_1 = dense_tensor_1->data(); - CHECK(data_1[0] == 1.0f); - CHECK(data_1[1] == 2.1f); -} - -TEST(tensor_utils, dense_tensor_to_tensor) { - const DDim dims({2, 1}); - const DataType dtype{DataType::FLOAT32}; - const DataLayout layout{DataLayout::NCHW}; - DenseTensorMeta meta(dtype, dims, layout); - - auto alloc = - std::make_shared(platform::CPUPlace()); - - DenseTensor dense_tensor(alloc, meta); - float* data = dense_tensor.mutable_data(); - data[0] = 1.0f; - data[1] = 2.1f; - - framework::Tensor tensor; - experimental::MovesStorage(&dense_tensor, &tensor); - - CHECK(dense_tensor.dtype() == pten::TransToPtenDataType(tensor.type())); - CHECK(dense_tensor.layout() == tensor.layout()); - CHECK(platform::is_cpu_place(tensor.place())); - - CHECK(tensor.data()[0] == 1.0f); - CHECK(tensor.data()[1] == 2.1f); - - auto dense_tensor_1 = experimental::MakePtenDenseTensor(tensor); - CHECK(dense_tensor_1->dims() == dims); - CHECK(dense_tensor_1->dtype() == dtype); - CHECK(dense_tensor_1->layout() == layout); - const float* data_1 = dense_tensor_1->data(); - CHECK(data_1[0] == 1.0f); - CHECK(data_1[1] == 2.1f); -} - -TEST(PtenUtils, VarToPtTensor) { - // 1. create Variable - paddle::framework::Variable v; - auto selected_rows = v.GetMutable(); - paddle::framework::Tensor* value = selected_rows->mutable_value(); - auto* data = value->mutable_data(paddle::framework::make_ddim({1, 1}), - paddle::platform::CPUPlace()); - data[0] = 123; - pten::Backend expect_backend = pten::Backend::CPU; - -#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) - expect_backend = pten::Backend::GPU; -#endif - auto tensor_def = pten::TensorArgDef( - expect_backend, pten::DataLayout::NCHW, pten::DataType::INT32); - // 2. test API - auto tensor_x = experimental::MakePtenTensorBaseFromVar(v, tensor_def); - // 3. check result - ASSERT_EQ(tensor_x->dtype(), pten::DataType::INT32); -} - -} // namespace tests -} // namespace paddle diff --git a/paddle/pten/tests/api/test_to_api.cc b/paddle/pten/tests/api/test_to_api.cc index 47e8ff7c2c87ed2ad9ad880b27522aa9e93a2560..9aef716029a692d9c281674a5ef9f188ef3e5c74 100644 --- a/paddle/pten/tests/api/test_to_api.cc +++ b/paddle/pten/tests/api/test_to_api.cc @@ -28,10 +28,10 @@ namespace framework = paddle::framework; using DDim = paddle::framework::DDim; paddle::experimental::Tensor CreateInputTensor() { - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_x = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::INT64, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/core/CMakeLists.txt b/paddle/pten/tests/core/CMakeLists.txt index 9a5cfecc2917b8e40d4b941a7339935400e8484a..07554f02d999222dd6cf41c6462c14e9a924b4db 100644 --- a/paddle/pten/tests/core/CMakeLists.txt +++ b/paddle/pten/tests/core/CMakeLists.txt @@ -1,5 +1,3 @@ -cc_test(test_allocator SRCS test_allocator.cc DEPS tensor_base) -cc_test(test_storage SRCS test_storage.cc DEPS tensor_base) cc_test(test_dense_tensor SRCS test_dense_tensor.cc DEPS dense_tensor) cc_test(test_intrusive_ptr SRCS test_intrusive_ptr.cc) cc_test(test_type_info SRCS test_type_info.cc) diff --git a/paddle/pten/tests/core/allocator.h b/paddle/pten/tests/core/allocator.h index c2c74e1aacf1f2cb253e434385f8c64aed52d89d..e78f288e8e54529bef180576d5e2789104f5a82b 100644 --- a/paddle/pten/tests/core/allocator.h +++ b/paddle/pten/tests/core/allocator.h @@ -21,76 +21,19 @@ limitations under the License. */ namespace pten { namespace tests { -class HostAllocatorSample : public pten::deprecated::RawAllocator { +class FancyAllocator : public pten::Allocator { public: - using Place = paddle::platform::Place; - void* Allocate(size_t bytes_size) override { - return ::operator new(bytes_size); - } - void Deallocate(void* ptr, size_t bytes_size) override { - return ::operator delete(ptr); - } - const Place& place() const override { return place_; } - - private: - Place place_{paddle::platform::CPUPlace()}; -}; - -class FancyAllocator : public pten::deprecated::Allocator { - public: - using Allocation = pten::deprecated::Allocation; static void Delete(Allocation* allocation) { ::operator delete(allocation->ptr()); } - Allocation Allocate(size_t bytes_size) override { + AllocationPtr Allocate(size_t bytes_size) override { void* data = ::operator new(bytes_size); - return Allocation(data, data, &Delete, place()); - } - - const paddle::platform::Place& place() override { return place_; } - - paddle::platform::Place place_ = paddle::platform::CPUPlace(); -}; - -template -struct CustomAllocator { - using value_type = T; - using Allocator = pten::deprecated::RawAllocator; - - explicit CustomAllocator(const std::shared_ptr& a) noexcept - : alloc_(a) {} - - CustomAllocator(const CustomAllocator&) noexcept = default; - T* allocate(std::size_t n) { - return static_cast(alloc_->Allocate(n * sizeof(T))); - } - void deallocate(T* p, std::size_t n) { - return alloc_->Deallocate(p, sizeof(T) * n); + auto* allocation = + new pten::Allocation(data, bytes_size, paddle::platform::CPUPlace()); + return AllocationPtr(allocation, Delete); } - - template - friend bool operator==(const CustomAllocator&, - const CustomAllocator&) noexcept; - template - friend bool operator!=(const CustomAllocator&, - const CustomAllocator&) noexcept; - - private: - std::shared_ptr alloc_; }; -template -inline bool operator==(const CustomAllocator& lhs, - const CustomAllocator& rhs) noexcept { - return &lhs.alloc_ == &rhs.alloc_; -} - -template -inline bool operator!=(const CustomAllocator& lhs, - const CustomAllocator& rhs) noexcept { - return &lhs.alloc_ != &rhs.alloc_; -} - } // namespace tests } // namespace pten diff --git a/paddle/pten/tests/core/test_allocator.cc b/paddle/pten/tests/core/test_allocator.cc deleted file mode 100644 index 94ba9a1e1b9a23711caf3d6e40db0cdce00dec93..0000000000000000000000000000000000000000 --- a/paddle/pten/tests/core/test_allocator.cc +++ /dev/null @@ -1,95 +0,0 @@ -/* 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 -#include - -#include "gtest/gtest.h" -#include "paddle/fluid/framework/generator.h" -#include "paddle/pten/tests/core/allocator.h" -#include "paddle/pten/tests/core/random.h" -#include "paddle/pten/tests/core/timer.h" - -namespace pten { -namespace tests { - -using RawAllocator = pten::deprecated::RawAllocator; -using Allocator = pten::deprecated::Allocator; -using Allocation = pten::deprecated::Allocation; - -template -bool host_allocator_test(size_t vector_size) { - std::vector src(vector_size); - std::generate(src.begin(), src.end(), make_generator(src)); - std::vector> dst( - src.begin(), - src.end(), - CustomAllocator(std::make_shared())); - return std::equal(src.begin(), src.end(), dst.begin()); -} - -TEST(raw_allocator, host) { - CHECK(host_allocator_test(1000)); - CHECK(host_allocator_test(1000)); - CHECK(host_allocator_test(1000)); -} - -class StorageRawAlloc { - public: - StorageRawAlloc(const std::shared_ptr& a, size_t size) - : alloc_(a) { - data_ = alloc_->Allocate(size); - } - ~StorageRawAlloc() { alloc_->Deallocate(data_, size); } - - private: - void* data_; - size_t size; - std::shared_ptr alloc_; -}; - -class StorageFancyAlloc { - public: - StorageFancyAlloc(const std::shared_ptr& a, size_t size) - : alloc_(a), allocation_(a->Allocate(size)) {} - - private: - std::shared_ptr alloc_; - Allocation allocation_; -}; - -TEST(benchmark, allocator) { - std::shared_ptr raw_allocator(new HostAllocatorSample); - std::shared_ptr fancy_allocator(new FancyAllocator); - const size_t cycles = 100; - Timer timer; - double t1{}, t2{}; - for (size_t i = 0; i < cycles; ++i) { - timer.tic(); - for (size_t i = 0; i < cycles; ++i) { - StorageRawAlloc(raw_allocator, i * 100); - } - t1 += timer.toc(); - timer.tic(); - for (size_t i = 0; i < cycles; ++i) { - StorageFancyAlloc(fancy_allocator, i * 100); - } - t2 += timer.toc(); - } - std::cout << "The cost of raw alloc is " << t1 << "ms.\n"; - std::cout << "The cost of fancy alloc with place is " << t2 << "ms.\n"; -} - -} // namespace tests -} // namespace pten diff --git a/paddle/pten/tests/core/test_dense_tensor.cc b/paddle/pten/tests/core/test_dense_tensor.cc index 8277c0d8dadb78edfe5b296d87f479fbae37dd2f..8564969796c7ec5ae71495e3da6f00c312db1a7e 100644 --- a/paddle/pten/tests/core/test_dense_tensor.cc +++ b/paddle/pten/tests/core/test_dense_tensor.cc @@ -75,7 +75,8 @@ TEST(dense_tensor, ctor) { const LoD lod{}; DenseTensorMeta meta(dtype, dims, layout, lod); - auto alloc = std::make_shared(); + auto fancy_allocator = std::unique_ptr(new FancyAllocator); + auto* alloc = fancy_allocator.get(); auto check_dense_tensor = [](const DenseTensor& t, const DenseTensorMeta& m) -> bool { @@ -95,10 +96,6 @@ TEST(dense_tensor, ctor) { DenseTensor tensor_1(alloc, DenseTensorMeta(meta)); check_dense_tensor(tensor_0, meta); - - DenseTensor tensor_2(make_intrusive(alloc), meta); - CHECK_NOTNULL(tensor_2.mutable_data()); - check_dense_tensor(tensor_2, meta); } TEST(dense_tensor, resize) { @@ -108,7 +105,8 @@ TEST(dense_tensor, resize) { const LoD lod{}; DenseTensorMeta meta(dtype, dims, layout, lod); - auto alloc = std::make_shared(); + auto fancy_allocator = std::unique_ptr(new FancyAllocator); + auto* alloc = fancy_allocator.get(); DenseTensor tensor_0(alloc, meta); CHECK_EQ(tensor_0.capacity(), 2u); @@ -125,7 +123,8 @@ TEST(dense_tensor, shallow_copy) { const LoD lod{}; DenseTensorMeta meta(dtype, dims, layout, lod); - auto alloc = std::make_shared(); + auto fancy_allocator = std::unique_ptr(new FancyAllocator); + auto* alloc = fancy_allocator.get(); DenseTensor tensor_0(alloc, meta); DenseTensor tensor_1(tensor_0); diff --git a/paddle/pten/tests/core/test_storage.cc b/paddle/pten/tests/core/test_storage.cc deleted file mode 100644 index 69d1eae668c5859526193631d6f19c945ddb6271..0000000000000000000000000000000000000000 --- a/paddle/pten/tests/core/test_storage.cc +++ /dev/null @@ -1,40 +0,0 @@ -/* 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 - -#include "gtest/gtest.h" - -#include "paddle/pten/core/storage.h" -#include "paddle/pten/tests/core/allocator.h" - -namespace pten { -namespace tests { - -TEST(host_storage, internal) { - // TODO(Shixiaowei02): Here we need to consider the case - // where the size is zero. - const size_t size{100}; - const auto a = std::make_shared(); - TensorStorage storage(a, size); - CHECK_EQ(storage.size(), size); - CHECK(paddle::platform::is_cpu_place(storage.place())); - CHECK(storage.OwnsMemory()); - CHECK(storage.allocator() == a); - storage.Realloc(size + 100); - CHECK_EQ(storage.size(), size + 100); -} - -} // namespace tests -} // namespace pten diff --git a/paddle/pten/tests/kernels/test_cast_dev_api.cc b/paddle/pten/tests/kernels/test_cast_dev_api.cc index cb45d827e3be909e538b381bf410537ef2cdc23f..90624adeb344e73e5423bc42dc3957354233e81f 100644 --- a/paddle/pten/tests/kernels/test_cast_dev_api.cc +++ b/paddle/pten/tests/kernels/test_cast_dev_api.cc @@ -31,9 +31,9 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, cast) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_conj_dev_api.cc b/paddle/pten/tests/kernels/test_conj_dev_api.cc index 3392626dc2ad36baaa0504237456ea76e82fcd80..789d55491f368bc4dd09c5b641805181ccd9f2c4 100644 --- a/paddle/pten/tests/kernels/test_conj_dev_api.cc +++ b/paddle/pten/tests/kernels/test_conj_dev_api.cc @@ -29,9 +29,9 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, conj) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::COMPLEX64, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_copy_dev_api.cc b/paddle/pten/tests/kernels/test_copy_dev_api.cc index 3095c83d97c9801c49d073a873fd8db496d63952..c4d8c37eb9e0fa26cc191143d1e626d6660e2dbe 100644 --- a/paddle/pten/tests/kernels/test_copy_dev_api.cc +++ b/paddle/pten/tests/kernels/test_copy_dev_api.cc @@ -31,17 +31,17 @@ using DDim = paddle::framework::DDim; // in 'paddle/api' TEST(DEV_API, copy) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); auto dense_src = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({2, 3}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_src->mutable_data(); auto dense_dst = std::make_shared( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({2, 3}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_creation_dev_api.cc b/paddle/pten/tests/kernels/test_creation_dev_api.cc index 4d753f7d09b8e0cf9c4e485426911cead82c2cd9..169a77cf3436b0ec99f9b499804d1ba01242045f 100644 --- a/paddle/pten/tests/kernels/test_creation_dev_api.cc +++ b/paddle/pten/tests/kernels/test_creation_dev_api.cc @@ -50,9 +50,9 @@ TEST(DEV_API, empty) { TEST(DEV_API, empty_like) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2}), pten::DataLayout::NCHW)); @@ -105,9 +105,9 @@ TEST(DEV_API, full) { TEST(DEV_API, full_like) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_dot_dev_api.cc b/paddle/pten/tests/kernels/test_dot_dev_api.cc index 6e2166cb673bd48114cffa564f71e0011ebfb52b..a5773b8aa9690d02b11a9570556f2d16f0fc5f48 100644 --- a/paddle/pten/tests/kernels/test_dot_dev_api.cc +++ b/paddle/pten/tests/kernels/test_dot_dev_api.cc @@ -29,15 +29,15 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, dot) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(); - pten::DenseTensor dense_y(alloc, + pten::DenseTensor dense_y(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_elementwise_dev_api.cc b/paddle/pten/tests/kernels/test_elementwise_dev_api.cc index bd09ecb770a5d59b114e2ba883d3a8e6107121cd..40998a8d57caa2cc8b5e19d13d3bc7b38b340674 100644 --- a/paddle/pten/tests/kernels/test_elementwise_dev_api.cc +++ b/paddle/pten/tests/kernels/test_elementwise_dev_api.cc @@ -29,15 +29,15 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, add) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(); - pten::DenseTensor dense_y(alloc, + pten::DenseTensor dense_y(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); @@ -82,15 +82,15 @@ TEST(DEV_API, add) { TEST(DEV_API, subtract) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(); - pten::DenseTensor dense_y(alloc, + pten::DenseTensor dense_y(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); @@ -135,15 +135,15 @@ TEST(DEV_API, subtract) { TEST(DEV_API, divide) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(); - pten::DenseTensor dense_y(alloc, + pten::DenseTensor dense_y(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); @@ -188,15 +188,15 @@ TEST(DEV_API, divide) { TEST(DEV_API, multiply) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 10}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(); - pten::DenseTensor dense_y(alloc, + pten::DenseTensor dense_y(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({10}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_flatten_dev_api.cc b/paddle/pten/tests/kernels/test_flatten_dev_api.cc index f18e5c050ba708c54c4e559de1a7ecd43f99b717..d66ff468fcf48cf1ef2b95d58504908a956496dd 100644 --- a/paddle/pten/tests/kernels/test_flatten_dev_api.cc +++ b/paddle/pten/tests/kernels/test_flatten_dev_api.cc @@ -39,10 +39,10 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, flatten) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); pten::DenseTensor dense_x( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2, 2, 3}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_matmul_dev_api.cc b/paddle/pten/tests/kernels/test_matmul_dev_api.cc index 7ac3d19554581f31d95bb13155beb30d1786c8c9..0c1338f195563842471f3d6e10b35d2fa28df59d 100644 --- a/paddle/pten/tests/kernels/test_matmul_dev_api.cc +++ b/paddle/pten/tests/kernels/test_matmul_dev_api.cc @@ -29,16 +29,16 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, dot) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - DenseTensor dense_x(alloc, + DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); auto* dense_x_data = dense_x.mutable_data(); - DenseTensor dense_y(alloc, + DenseTensor dense_y(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 3}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_mean_dev_api.cc b/paddle/pten/tests/kernels/test_mean_dev_api.cc index 4b254e7e6c1ac31920b7e6bf881e39505a3c121f..98782fd5dae0b7b539f738d1e23e95583da1443b 100644 --- a/paddle/pten/tests/kernels/test_mean_dev_api.cc +++ b/paddle/pten/tests/kernels/test_mean_dev_api.cc @@ -29,9 +29,9 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, mean) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_reshape_dev_api.cc b/paddle/pten/tests/kernels/test_reshape_dev_api.cc index 0196e1c2110043361937a908aa5e62b57f72c6c7..02139d02de17e1948d4ff5c091328966da580f7d 100644 --- a/paddle/pten/tests/kernels/test_reshape_dev_api.cc +++ b/paddle/pten/tests/kernels/test_reshape_dev_api.cc @@ -30,10 +30,10 @@ using DDim = paddle::framework::DDim; // TODO(chenweihang): Remove this test after the API is used in the dygraph TEST(DEV_API, reshape) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); pten::DenseTensor dense_x( - alloc, + alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 2, 2, 3}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_scale_dev_api.cc b/paddle/pten/tests/kernels/test_scale_dev_api.cc index fe26f56552b05fc0d27d7af4e9de36a029f0a740..02f324deb4cec874e984022bd316e3ec3a7c078a 100644 --- a/paddle/pten/tests/kernels/test_scale_dev_api.cc +++ b/paddle/pten/tests/kernels/test_scale_dev_api.cc @@ -29,9 +29,9 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, scale) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); @@ -69,9 +69,9 @@ TEST(DEV_API, scale) { TEST(DEV_API, scale_host) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW)); @@ -79,9 +79,8 @@ TEST(DEV_API, scale_host) { for (size_t i = 0; i < 12; ++i) { dense_x_data[i] = i * 1.0; } - const auto alloc2 = std::make_shared( - paddle::platform::CPUPlace()); - pten::DenseTensor scale(alloc2, + + pten::DenseTensor scale(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({1}), pten::DataLayout::NCHW)); diff --git a/paddle/pten/tests/kernels/test_sum_dev_api.cc b/paddle/pten/tests/kernels/test_sum_dev_api.cc index afaf9030637812b2099c9f5276eb2259cce20b62..312a6ce6100bb9f3b5f146f1b64b4a611d68579d 100644 --- a/paddle/pten/tests/kernels/test_sum_dev_api.cc +++ b/paddle/pten/tests/kernels/test_sum_dev_api.cc @@ -29,9 +29,9 @@ using DDim = paddle::framework::DDim; TEST(DEV_API, sum) { // 1. create tensor - const auto alloc = std::make_shared( + const auto alloc = std::make_unique( paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, + pten::DenseTensor dense_x(alloc.get(), pten::DenseTensorMeta(pten::DataType::FLOAT32, framework::make_ddim({3, 4}), pten::DataLayout::NCHW));