From 9a53477c56db87b509f8786f67b84130d0ea9e94 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Fri, 5 Nov 2021 14:48:54 +0800 Subject: [PATCH] [PTen] Organize pten unitests and directory (#36948) * organize pten unitests * fix detail errors --- paddle/fluid/framework/CMakeLists.txt | 2 +- paddle/pten/api/CMakeLists.txt | 2 +- paddle/pten/api/lib/utils/CMakeLists.txt | 5 +- .../pten/api/lib/utils/tests/CMakeLists.txt | 2 - paddle/pten/tests/CMakeLists.txt | 17 +--- paddle/pten/tests/api/CMakeLists.txt | 7 ++ paddle/pten/tests/{ => api}/test_dot_api.cc | 54 ------------- paddle/pten/tests/{ => api}/test_fill_api.cc | 37 --------- .../pten/tests/{ => api}/test_flatten_api.cc | 46 ----------- .../pten/tests/{ => api}/test_matmul_api.cc | 0 paddle/pten/tests/{ => api}/test_mean_api.cc | 35 -------- .../utils/tests => tests/api}/test_storage.cc | 0 .../tests => tests/api}/test_tensor_utils.cc | 0 paddle/pten/tests/common/CMakeLists.txt | 3 + .../test_backend.cc} | 4 +- .../test_data_layout.cc} | 1 + .../test_data_type.cc} | 4 +- paddle/pten/tests/core/CMakeLists.txt | 3 + .../{utils => core}/test_intrusive_ptr.cc | 0 .../test_kernel_factory.cc} | 0 .../tests/{utils => core}/test_type_info.cc | 0 paddle/pten/tests/dense_tensor_test.cc | 20 ----- paddle/pten/tests/kernels/CMakeLists.txt | 6 ++ .../test_copy_dev_api.cc} | 2 +- paddle/pten/tests/kernels/test_dot_dev_api.cc | 81 +++++++++++++++++++ .../pten/tests/kernels/test_fill_dev_api.cc | 66 +++++++++++++++ .../tests/kernels/test_flatten_dev_api.cc | 75 +++++++++++++++++ .../pten/tests/kernels/test_mean_dev_api.cc | 64 +++++++++++++++ .../test_scale_dev_api.cc} | 4 +- paddle/pten/tests/utils/CMakeLists.txt | 2 - 30 files changed, 318 insertions(+), 224 deletions(-) delete mode 100644 paddle/pten/api/lib/utils/tests/CMakeLists.txt create mode 100644 paddle/pten/tests/api/CMakeLists.txt rename paddle/pten/tests/{ => api}/test_dot_api.cc (58%) rename paddle/pten/tests/{ => api}/test_fill_api.cc (78%) rename paddle/pten/tests/{ => api}/test_flatten_api.cc (62%) rename paddle/pten/tests/{ => api}/test_matmul_api.cc (100%) rename paddle/pten/tests/{ => api}/test_mean_api.cc (64%) rename paddle/pten/{api/lib/utils/tests => tests/api}/test_storage.cc (100%) rename paddle/pten/{api/lib/utils/tests => tests/api}/test_tensor_utils.cc (100%) create mode 100644 paddle/pten/tests/common/CMakeLists.txt rename paddle/pten/tests/{backend_test.cc => common/test_backend.cc} (100%) rename paddle/pten/tests/{data_layout_test.cc => common/test_data_layout.cc} (99%) rename paddle/pten/tests/{data_type_test.cc => common/test_data_type.cc} (100%) rename paddle/pten/tests/{utils => core}/test_intrusive_ptr.cc (100%) rename paddle/pten/tests/{kernel_factory_test.cc => core/test_kernel_factory.cc} (100%) rename paddle/pten/tests/{utils => core}/test_type_info.cc (100%) delete mode 100644 paddle/pten/tests/dense_tensor_test.cc create mode 100644 paddle/pten/tests/kernels/CMakeLists.txt rename paddle/pten/tests/{test_copy_api.cc => kernels/test_copy_dev_api.cc} (99%) create mode 100644 paddle/pten/tests/kernels/test_dot_dev_api.cc create mode 100644 paddle/pten/tests/kernels/test_fill_dev_api.cc create mode 100644 paddle/pten/tests/kernels/test_flatten_dev_api.cc create mode 100644 paddle/pten/tests/kernels/test_mean_dev_api.cc rename paddle/pten/tests/{test_scale_api.cc => kernels/test_scale_dev_api.cc} (98%) delete mode 100644 paddle/pten/tests/utils/CMakeLists.txt diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index d0cbd7801d..19a7b4389b 100644 --- a/paddle/fluid/framework/CMakeLists.txt +++ b/paddle/fluid/framework/CMakeLists.txt @@ -399,7 +399,7 @@ cc_library(save_load_util SRCS save_load_util.cc DEPS tensor scope layer) cc_test(save_load_util_test SRCS save_load_util_test.cc DEPS save_load_util tensor scope layer) cc_library(generator SRCS generator.cc DEPS enforce place) -cc_library(pten_utils SRCS pten_utils.cc DEPS lod_tensor selected_rows place pten var_type_traits pten_hapi_utils op_info) +cc_library(pten_utils SRCS pten_utils.cc DEPS lod_tensor selected_rows place pten var_type_traits pten_api_utils op_info) # Get the current working branch execute_process( diff --git a/paddle/pten/api/CMakeLists.txt b/paddle/pten/api/CMakeLists.txt index 4b427b3b4a..387da3bc68 100644 --- a/paddle/pten/api/CMakeLists.txt +++ b/paddle/pten/api/CMakeLists.txt @@ -1,3 +1,3 @@ add_subdirectory(lib) -cc_library(pten_hapi SRCS all.cc DEPS linalg_api math_api creation_api) +cc_library(pten_api SRCS all.cc DEPS linalg_api math_api creation_api manipulation_api) diff --git a/paddle/pten/api/lib/utils/CMakeLists.txt b/paddle/pten/api/lib/utils/CMakeLists.txt index c89ef81284..34f8da94c6 100644 --- a/paddle/pten/api/lib/utils/CMakeLists.txt +++ b/paddle/pten/api/lib/utils/CMakeLists.txt @@ -1,4 +1 @@ -add_subdirectory(tests) - -cc_library(pten_hapi_utils SRCS allocator.cc storage.cc tensor_utils.cc DEPS tensor_base convert_utils -dense_tensor lod_tensor selected_rows place var_type_traits) +cc_library(pten_api_utils SRCS allocator.cc storage.cc tensor_utils.cc DEPS tensor_base convert_utils dense_tensor lod_tensor selected_rows place var_type_traits) diff --git a/paddle/pten/api/lib/utils/tests/CMakeLists.txt b/paddle/pten/api/lib/utils/tests/CMakeLists.txt deleted file mode 100644 index 8ac30a1fa6..0000000000 --- a/paddle/pten/api/lib/utils/tests/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -cc_test(test_framework_storage SRCS test_storage.cc DEPS pten_hapi_utils) -cc_test(test_framework_tensor_utils SRCS test_tensor_utils.cc DEPS pten_hapi_utils) diff --git a/paddle/pten/tests/CMakeLists.txt b/paddle/pten/tests/CMakeLists.txt index 3d2da6a5af..9946821581 100644 --- a/paddle/pten/tests/CMakeLists.txt +++ b/paddle/pten/tests/CMakeLists.txt @@ -1,15 +1,4 @@ +add_subdirectory(api) +add_subdirectory(common) add_subdirectory(core) -add_subdirectory(utils) - -cc_test(pten_backend_test SRCS backend_test.cc DEPS gtest) -cc_test(pten_data_layout_test SRCS data_layout_test.cc DEPS gtest) -cc_test(pten_data_type_test SRCS data_type_test.cc DEPS gtest) -cc_test(dense_tensor_test SRCS dense_tensor_test.cc DEPS dense_tensor) -cc_test(kernel_factory_test SRCS kernel_factory_test.cc DEPS kernel_factory) -cc_test(test_mean_api SRCS test_mean_api.cc DEPS math_api pten_hapi_utils) -cc_test(test_dot_api SRCS test_dot_api.cc DEPS linalg_api pten_hapi_utils) -cc_test(test_matmul_api SRCS test_matmul_api.cc DEPS linalg_api pten_hapi_utils) -cc_test(test_fill_api SRCS test_fill_api.cc DEPS creation_api pten_hapi_utils) -cc_test(test_copy_api SRCS test_copy_api.cc DEPS utils_cpu pten_hapi_utils) -cc_test(test_flatten_api SRCS test_flatten_api.cc DEPS utils_cpu manipulation_api pten_hapi_utils) -cc_test(test_scale_api SRCS test_scale_api.cc DEPS math_api pten_hapi_utils) +add_subdirectory(kernels) diff --git a/paddle/pten/tests/api/CMakeLists.txt b/paddle/pten/tests/api/CMakeLists.txt new file mode 100644 index 0000000000..2c6bd9c45d --- /dev/null +++ b/paddle/pten/tests/api/CMakeLists.txt @@ -0,0 +1,7 @@ +cc_test(test_mean_api SRCS test_mean_api.cc DEPS pten_api pten_api_utils) +cc_test(test_dot_api SRCS test_dot_api.cc DEPS pten_api pten_api_utils) +cc_test(test_matmul_api SRCS test_matmul_api.cc DEPS pten_api pten_api_utils) +cc_test(test_fill_api SRCS test_fill_api.cc DEPS pten_api pten_api_utils) +cc_test(test_flatten_api SRCS test_flatten_api.cc DEPS pten_api pten_api_utils) +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) diff --git a/paddle/pten/tests/test_dot_api.cc b/paddle/pten/tests/api/test_dot_api.cc similarity index 58% rename from paddle/pten/tests/test_dot_api.cc rename to paddle/pten/tests/api/test_dot_api.cc index f79854e557..2c7b65f981 100644 --- a/paddle/pten/tests/test_dot_api.cc +++ b/paddle/pten/tests/api/test_dot_api.cc @@ -21,8 +21,6 @@ limitations under the License. */ #include "paddle/pten/core/dense_tensor.h" #include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/include/linalg.h" - PT_DECLARE_MODULE(LinalgCPU); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) @@ -84,55 +82,3 @@ TEST(API, dot) { ASSERT_NEAR(expect_result[1], actual_result1, 1e-6f); ASSERT_NEAR(expect_result[2], actual_result2, 1e-6f); } - -// TODO(YuanRisheng) This unitest should be created in other file. -// It is convenient to make compilation decoupling. -TEST(DEV_API, dot) { - // 1. create tensor - const auto alloc = std::make_shared( - paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, - 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::DenseTensorMeta(pten::DataType::FLOAT32, - framework::make_ddim({3, 10}), - pten::DataLayout::NCHW)); - auto* dense_y_data = dense_y.mutable_data(); - - float sum[3] = {0.0, 0.0, 0.0}; - for (size_t i = 0; i < 3; ++i) { - for (size_t j = 0; j < 10; ++j) { - dense_x_data[i * 10 + j] = (i * 10 + j) * 1.0; - dense_y_data[i * 10 + j] = (i * 10 + j) * 1.0; - sum[i] += (i * 10 + j) * (i * 10 + j) * 1.0; - } - } - - paddle::platform::DeviceContextPool& pool = - paddle::platform::DeviceContextPool::Instance(); - auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); - - // 2. test API - auto out = pten::Dot( - *(static_cast(dev_ctx)), - dense_x, - dense_y); - - // 3. check result - ASSERT_EQ(out.dims().size(), 2); - ASSERT_EQ(out.dims()[0], 3); - ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); - ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); - - auto expect_result = sum; - auto actual_result0 = out.data()[0]; - auto actual_result1 = out.data()[1]; - auto actual_result2 = out.data()[2]; - ASSERT_NEAR(expect_result[0], actual_result0, 1e-6f); - ASSERT_NEAR(expect_result[1], actual_result1, 1e-6f); - ASSERT_NEAR(expect_result[2], actual_result2, 1e-6f); -} diff --git a/paddle/pten/tests/test_fill_api.cc b/paddle/pten/tests/api/test_fill_api.cc similarity index 78% rename from paddle/pten/tests/test_fill_api.cc rename to paddle/pten/tests/api/test_fill_api.cc index a83ed76a6f..cbac4c541f 100644 --- a/paddle/pten/tests/test_fill_api.cc +++ b/paddle/pten/tests/api/test_fill_api.cc @@ -21,8 +21,6 @@ limitations under the License. */ #include "paddle/pten/core/dense_tensor.h" #include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/include/creation.h" - PT_DECLARE_MODULE(CreationCPU); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) @@ -133,38 +131,3 @@ TEST(API, ones_like) { ASSERT_EQ(actual_result[i], 1); } } - -TEST(DEV_API, fill_any_like) { - // 1. create tensor - const auto alloc = std::make_shared( - paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, - pten::DenseTensorMeta(pten::DataType::FLOAT32, - framework::make_ddim({3, 2}), - pten::DataLayout::NCHW)); - auto* dense_x_data = dense_x.mutable_data(); - dense_x_data[0] = 0; - float val = 1.0; - - paddle::platform::DeviceContextPool& pool = - paddle::platform::DeviceContextPool::Instance(); - auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); - - // 2. test API - auto out = pten::FillAnyLike( - *(static_cast(dev_ctx)), - dense_x, - val); - - // 3. check result - ASSERT_EQ(out.dims().size(), 2); - ASSERT_EQ(out.dims()[0], 3); - ASSERT_EQ(out.numel(), 6); - ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); - ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); - - auto* actual_result = out.data(); - for (auto i = 0; i < 6; i++) { - ASSERT_NEAR(actual_result[i], val, 1e-6f); - } -} diff --git a/paddle/pten/tests/test_flatten_api.cc b/paddle/pten/tests/api/test_flatten_api.cc similarity index 62% rename from paddle/pten/tests/test_flatten_api.cc rename to paddle/pten/tests/api/test_flatten_api.cc index ca8ca38f65..3701c358c6 100644 --- a/paddle/pten/tests/test_flatten_api.cc +++ b/paddle/pten/tests/api/test_flatten_api.cc @@ -21,8 +21,6 @@ limitations under the License. */ #include "paddle/pten/core/dense_tensor.h" #include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/include/manipulation.h" - PT_DECLARE_MODULE(ManipulationCPU); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) @@ -72,47 +70,3 @@ TEST(API, flatten) { } ASSERT_EQ(value_equal, true); } - -TEST(DEV_API, flatten) { - // 1. create tensor - const auto alloc = std::make_shared( - paddle::platform::CPUPlace()); - pten::DenseTensor dense_x( - alloc, - pten::DenseTensorMeta(pten::DataType::FLOAT32, - framework::make_ddim({3, 2, 2, 3}), - pten::DataLayout::NCHW)); - auto* dense_x_data = dense_x.mutable_data(); - - for (int i = 0; i < dense_x.numel(); i++) { - dense_x_data[i] = i; - } - int start_axis = 1, stop_axis = 2; - paddle::platform::DeviceContextPool& pool = - paddle::platform::DeviceContextPool::Instance(); - auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); - - // 2. test API - auto out = pten::Flatten( - *(static_cast(dev_ctx)), - dense_x, - start_axis, - stop_axis); - - // 3. check result - std::vector expect_shape = {3, 4, 3}; - ASSERT_EQ(out.dims()[0], expect_shape[0]); - ASSERT_EQ(out.dims()[1], expect_shape[1]); - ASSERT_EQ(out.dims()[2], expect_shape[2]); - ASSERT_EQ(out.numel(), 36); - ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); - ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); - - bool value_equal = true; - auto* dense_out_data = out.data(); - for (int i = 0; i < dense_x.numel(); i++) { - if (std::abs(dense_x_data[i] - dense_out_data[i]) > 1e-6f) - value_equal = false; - } - ASSERT_EQ(value_equal, true); -} diff --git a/paddle/pten/tests/test_matmul_api.cc b/paddle/pten/tests/api/test_matmul_api.cc similarity index 100% rename from paddle/pten/tests/test_matmul_api.cc rename to paddle/pten/tests/api/test_matmul_api.cc diff --git a/paddle/pten/tests/test_mean_api.cc b/paddle/pten/tests/api/test_mean_api.cc similarity index 64% rename from paddle/pten/tests/test_mean_api.cc rename to paddle/pten/tests/api/test_mean_api.cc index 4dd4222daa..d772e58659 100644 --- a/paddle/pten/tests/test_mean_api.cc +++ b/paddle/pten/tests/api/test_mean_api.cc @@ -21,8 +21,6 @@ limitations under the License. */ #include "paddle/pten/core/dense_tensor.h" #include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/include/math.h" - PT_DECLARE_MODULE(MathCPU); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) @@ -69,36 +67,3 @@ TEST(API, mean) { auto actual_result = dense_out->data()[0]; ASSERT_NEAR(expect_result, actual_result, 1e-6f); } - -TEST(DEV_API, mean) { - // 1. create tensor - const auto alloc = std::make_shared( - paddle::platform::CPUPlace()); - pten::DenseTensor dense_x(alloc, - pten::DenseTensorMeta(pten::DataType::FLOAT32, - framework::make_ddim({3, 4}), - pten::DataLayout::NCHW)); - auto* dense_x_data = dense_x.mutable_data(); - - float sum = 0.0; - for (size_t i = 0; i < 12; ++i) { - dense_x_data[i] = i * 1.0; - sum += i * 1.0; - } - paddle::platform::DeviceContextPool& pool = - paddle::platform::DeviceContextPool::Instance(); - auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); - // 2. test API - auto out = pten::Mean( - *(static_cast(dev_ctx)), dense_x); - - // 3. check result - ASSERT_EQ(out.dims().size(), 1); - ASSERT_EQ(out.numel(), 1); - ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); - ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); - - auto expect_result = sum / 12; - auto actual_result = out.data()[0]; - ASSERT_NEAR(expect_result, actual_result, 1e-6f); -} diff --git a/paddle/pten/api/lib/utils/tests/test_storage.cc b/paddle/pten/tests/api/test_storage.cc similarity index 100% rename from paddle/pten/api/lib/utils/tests/test_storage.cc rename to paddle/pten/tests/api/test_storage.cc diff --git a/paddle/pten/api/lib/utils/tests/test_tensor_utils.cc b/paddle/pten/tests/api/test_tensor_utils.cc similarity index 100% rename from paddle/pten/api/lib/utils/tests/test_tensor_utils.cc rename to paddle/pten/tests/api/test_tensor_utils.cc diff --git a/paddle/pten/tests/common/CMakeLists.txt b/paddle/pten/tests/common/CMakeLists.txt new file mode 100644 index 0000000000..c0a5414d53 --- /dev/null +++ b/paddle/pten/tests/common/CMakeLists.txt @@ -0,0 +1,3 @@ +cc_test(pten_test_backend SRCS test_backend.cc DEPS gtest) +cc_test(pten_test_data_layout SRCS test_data_layout.cc DEPS gtest) +cc_test(pten_test_data_type SRCS test_data_type.cc DEPS gtest) diff --git a/paddle/pten/tests/backend_test.cc b/paddle/pten/tests/common/test_backend.cc similarity index 100% rename from paddle/pten/tests/backend_test.cc rename to paddle/pten/tests/common/test_backend.cc index 2bae2cd417..1c17c881ed 100644 --- a/paddle/pten/tests/backend_test.cc +++ b/paddle/pten/tests/common/test_backend.cc @@ -12,11 +12,11 @@ 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/pten/common/backend.h" - #include #include +#include "paddle/pten/common/backend.h" + TEST(Backend, OStream) { std::ostringstream oss; oss << pten::Backend::UNDEFINED; diff --git a/paddle/pten/tests/data_layout_test.cc b/paddle/pten/tests/common/test_data_layout.cc similarity index 99% rename from paddle/pten/tests/data_layout_test.cc rename to paddle/pten/tests/common/test_data_layout.cc index efa19670f2..7fe1b6c2ff 100644 --- a/paddle/pten/tests/data_layout_test.cc +++ b/paddle/pten/tests/common/test_data_layout.cc @@ -15,6 +15,7 @@ limitations under the License. */ #include #include #include + #include "paddle/pten/common/layout.h" TEST(DataLayout, OStream) { diff --git a/paddle/pten/tests/data_type_test.cc b/paddle/pten/tests/common/test_data_type.cc similarity index 100% rename from paddle/pten/tests/data_type_test.cc rename to paddle/pten/tests/common/test_data_type.cc index bcdef84040..28d58858bb 100644 --- a/paddle/pten/tests/data_type_test.cc +++ b/paddle/pten/tests/common/test_data_type.cc @@ -12,12 +12,12 @@ 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/pten/common/data_type.h" - #include #include #include +#include "paddle/pten/common/data_type.h" + TEST(DataType, OStream) { std::ostringstream oss; oss << pten::DataType::UNDEFINED; diff --git a/paddle/pten/tests/core/CMakeLists.txt b/paddle/pten/tests/core/CMakeLists.txt index 34888200b2..b25439cfe2 100644 --- a/paddle/pten/tests/core/CMakeLists.txt +++ b/paddle/pten/tests/core/CMakeLists.txt @@ -1,3 +1,6 @@ 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) +cc_test(test_kernel_factory SRCS test_kernel_factory.cc DEPS kernel_factory) diff --git a/paddle/pten/tests/utils/test_intrusive_ptr.cc b/paddle/pten/tests/core/test_intrusive_ptr.cc similarity index 100% rename from paddle/pten/tests/utils/test_intrusive_ptr.cc rename to paddle/pten/tests/core/test_intrusive_ptr.cc diff --git a/paddle/pten/tests/kernel_factory_test.cc b/paddle/pten/tests/core/test_kernel_factory.cc similarity index 100% rename from paddle/pten/tests/kernel_factory_test.cc rename to paddle/pten/tests/core/test_kernel_factory.cc diff --git a/paddle/pten/tests/utils/test_type_info.cc b/paddle/pten/tests/core/test_type_info.cc similarity index 100% rename from paddle/pten/tests/utils/test_type_info.cc rename to paddle/pten/tests/core/test_type_info.cc diff --git a/paddle/pten/tests/dense_tensor_test.cc b/paddle/pten/tests/dense_tensor_test.cc deleted file mode 100644 index e74917263d..0000000000 --- a/paddle/pten/tests/dense_tensor_test.cc +++ /dev/null @@ -1,20 +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 "paddle/pten/core/dense_tensor.h" - -#include - -namespace framework = paddle::framework; -using DDim = paddle::framework::DDim; diff --git a/paddle/pten/tests/kernels/CMakeLists.txt b/paddle/pten/tests/kernels/CMakeLists.txt new file mode 100644 index 0000000000..b0dc29de52 --- /dev/null +++ b/paddle/pten/tests/kernels/CMakeLists.txt @@ -0,0 +1,6 @@ +cc_test(test_copy_dev_api SRCS test_copy_dev_api.cc DEPS pten pten_api_utils) +cc_test(test_dot_dev_api SRCS test_dot_dev_api.cc DEPS pten pten_api_utils) +cc_test(test_fill_dev_api SRCS test_fill_dev_api.cc DEPS pten pten_api_utils) +cc_test(test_flatten_dev_api SRCS test_flatten_dev_api.cc DEPS pten pten_api_utils) +cc_test(test_mean_dev_api SRCS test_mean_dev_api.cc DEPS pten pten_api_utils) +cc_test(test_scale_dev_api SRCS test_scale_dev_api.cc DEPS pten pten_api_utils) diff --git a/paddle/pten/tests/test_copy_api.cc b/paddle/pten/tests/kernels/test_copy_dev_api.cc similarity index 99% rename from paddle/pten/tests/test_copy_api.cc rename to paddle/pten/tests/kernels/test_copy_dev_api.cc index 4bf16e7517..dadbcf098d 100644 --- a/paddle/pten/tests/test_copy_api.cc +++ b/paddle/pten/tests/kernels/test_copy_dev_api.cc @@ -29,7 +29,7 @@ using DDim = paddle::framework::DDim; // TODO(YuanRisheng): This TEST file need to be refactored after 'copy' realized // in // 'paddle/api', -TEST(API, copy) { +TEST(DEV_API, copy) { // 1. create tensor const auto alloc = std::make_shared( paddle::platform::CPUPlace()); diff --git a/paddle/pten/tests/kernels/test_dot_dev_api.cc b/paddle/pten/tests/kernels/test_dot_dev_api.cc new file mode 100644 index 0000000000..897c49f4f2 --- /dev/null +++ b/paddle/pten/tests/kernels/test_dot_dev_api.cc @@ -0,0 +1,81 @@ +/* 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 "paddle/pten/include/linalg.h" + +#include "paddle/pten/api/lib/utils/allocator.h" +#include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/core/kernel_registry.h" + +PT_DECLARE_MODULE(LinalgCPU); + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PT_DECLARE_MODULE(LinalgCUDA); +#endif + +namespace framework = paddle::framework; +using DDim = paddle::framework::DDim; + +TEST(DEV_API, dot) { + // 1. create tensor + const auto alloc = std::make_shared( + paddle::platform::CPUPlace()); + pten::DenseTensor dense_x(alloc, + 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::DenseTensorMeta(pten::DataType::FLOAT32, + framework::make_ddim({3, 10}), + pten::DataLayout::NCHW)); + auto* dense_y_data = dense_y.mutable_data(); + + float sum[3] = {0.0, 0.0, 0.0}; + for (size_t i = 0; i < 3; ++i) { + for (size_t j = 0; j < 10; ++j) { + dense_x_data[i * 10 + j] = (i * 10 + j) * 1.0; + dense_y_data[i * 10 + j] = (i * 10 + j) * 1.0; + sum[i] += (i * 10 + j) * (i * 10 + j) * 1.0; + } + } + + paddle::platform::DeviceContextPool& pool = + paddle::platform::DeviceContextPool::Instance(); + auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); + + // 2. test API + auto out = pten::Dot( + *(static_cast(dev_ctx)), + dense_x, + dense_y); + + // 3. check result + ASSERT_EQ(out.dims().size(), 2); + ASSERT_EQ(out.dims()[0], 3); + ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); + ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); + + auto expect_result = sum; + auto actual_result0 = out.data()[0]; + auto actual_result1 = out.data()[1]; + auto actual_result2 = out.data()[2]; + ASSERT_NEAR(expect_result[0], actual_result0, 1e-6f); + ASSERT_NEAR(expect_result[1], actual_result1, 1e-6f); + ASSERT_NEAR(expect_result[2], actual_result2, 1e-6f); +} diff --git a/paddle/pten/tests/kernels/test_fill_dev_api.cc b/paddle/pten/tests/kernels/test_fill_dev_api.cc new file mode 100644 index 0000000000..8546396e18 --- /dev/null +++ b/paddle/pten/tests/kernels/test_fill_dev_api.cc @@ -0,0 +1,66 @@ +/* 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 "paddle/pten/include/creation.h" + +#include "paddle/pten/api/lib/utils/allocator.h" +#include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/core/kernel_registry.h" + +PT_DECLARE_MODULE(CreationCPU); + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PT_DECLARE_MODULE(CreationCUDA); +#endif + +namespace framework = paddle::framework; +using DDim = paddle::framework::DDim; + +TEST(DEV_API, fill_any_like) { + // 1. create tensor + const auto alloc = std::make_shared( + paddle::platform::CPUPlace()); + pten::DenseTensor dense_x(alloc, + pten::DenseTensorMeta(pten::DataType::FLOAT32, + framework::make_ddim({3, 2}), + pten::DataLayout::NCHW)); + auto* dense_x_data = dense_x.mutable_data(); + dense_x_data[0] = 0; + float val = 1.0; + + paddle::platform::DeviceContextPool& pool = + paddle::platform::DeviceContextPool::Instance(); + auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); + + // 2. test API + auto out = pten::FillAnyLike( + *(static_cast(dev_ctx)), + dense_x, + val); + + // 3. check result + ASSERT_EQ(out.dims().size(), 2); + ASSERT_EQ(out.dims()[0], 3); + ASSERT_EQ(out.numel(), 6); + ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); + ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); + + auto* actual_result = out.data(); + for (auto i = 0; i < 6; i++) { + ASSERT_NEAR(actual_result[i], val, 1e-6f); + } +} diff --git a/paddle/pten/tests/kernels/test_flatten_dev_api.cc b/paddle/pten/tests/kernels/test_flatten_dev_api.cc new file mode 100644 index 0000000000..16e6aa5acd --- /dev/null +++ b/paddle/pten/tests/kernels/test_flatten_dev_api.cc @@ -0,0 +1,75 @@ +/* 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 "paddle/pten/include/manipulation.h" + +#include "paddle/pten/api/lib/utils/allocator.h" +#include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/core/kernel_registry.h" + +PT_DECLARE_MODULE(ManipulationCPU); + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PT_DECLARE_MODULE(ManipulationCUDA); +#endif + +namespace framework = paddle::framework; +using DDim = paddle::framework::DDim; + +TEST(DEV_API, flatten) { + // 1. create tensor + const auto alloc = std::make_shared( + paddle::platform::CPUPlace()); + pten::DenseTensor dense_x( + alloc, + pten::DenseTensorMeta(pten::DataType::FLOAT32, + framework::make_ddim({3, 2, 2, 3}), + pten::DataLayout::NCHW)); + auto* dense_x_data = dense_x.mutable_data(); + + for (int i = 0; i < dense_x.numel(); i++) { + dense_x_data[i] = i; + } + int start_axis = 1, stop_axis = 2; + paddle::platform::DeviceContextPool& pool = + paddle::platform::DeviceContextPool::Instance(); + auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); + + // 2. test API + auto out = pten::Flatten( + *(static_cast(dev_ctx)), + dense_x, + start_axis, + stop_axis); + + // 3. check result + std::vector expect_shape = {3, 4, 3}; + ASSERT_EQ(out.dims()[0], expect_shape[0]); + ASSERT_EQ(out.dims()[1], expect_shape[1]); + ASSERT_EQ(out.dims()[2], expect_shape[2]); + ASSERT_EQ(out.numel(), 36); + ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); + ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); + + bool value_equal = true; + auto* dense_out_data = out.data(); + for (int i = 0; i < dense_x.numel(); i++) { + if (std::abs(dense_x_data[i] - dense_out_data[i]) > 1e-6f) + value_equal = false; + } + ASSERT_EQ(value_equal, true); +} diff --git a/paddle/pten/tests/kernels/test_mean_dev_api.cc b/paddle/pten/tests/kernels/test_mean_dev_api.cc new file mode 100644 index 0000000000..43f9371d72 --- /dev/null +++ b/paddle/pten/tests/kernels/test_mean_dev_api.cc @@ -0,0 +1,64 @@ +/* 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 "paddle/pten/include/math.h" + +#include "paddle/pten/api/lib/utils/allocator.h" +#include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/core/kernel_registry.h" + +PT_DECLARE_MODULE(MathCPU); + +#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) +PT_DECLARE_MODULE(MathCUDA); +#endif + +namespace framework = paddle::framework; +using DDim = paddle::framework::DDim; + +TEST(DEV_API, mean) { + // 1. create tensor + const auto alloc = std::make_shared( + paddle::platform::CPUPlace()); + pten::DenseTensor dense_x(alloc, + pten::DenseTensorMeta(pten::DataType::FLOAT32, + framework::make_ddim({3, 4}), + pten::DataLayout::NCHW)); + auto* dense_x_data = dense_x.mutable_data(); + + float sum = 0.0; + for (size_t i = 0; i < 12; ++i) { + dense_x_data[i] = i * 1.0; + sum += i * 1.0; + } + paddle::platform::DeviceContextPool& pool = + paddle::platform::DeviceContextPool::Instance(); + auto* dev_ctx = pool.Get(paddle::platform::CPUPlace()); + // 2. test API + auto out = pten::Mean( + *(static_cast(dev_ctx)), dense_x); + + // 3. check result + ASSERT_EQ(out.dims().size(), 1); + ASSERT_EQ(out.numel(), 1); + ASSERT_EQ(out.meta().type, pten::DataType::FLOAT32); + ASSERT_EQ(out.meta().layout, pten::DataLayout::NCHW); + + auto expect_result = sum / 12; + auto actual_result = out.data()[0]; + ASSERT_NEAR(expect_result, actual_result, 1e-6f); +} diff --git a/paddle/pten/tests/test_scale_api.cc b/paddle/pten/tests/kernels/test_scale_dev_api.cc similarity index 98% rename from paddle/pten/tests/test_scale_api.cc rename to paddle/pten/tests/kernels/test_scale_dev_api.cc index 2ecf6d199b..f1764e8032 100644 --- a/paddle/pten/tests/test_scale_api.cc +++ b/paddle/pten/tests/kernels/test_scale_dev_api.cc @@ -15,14 +15,12 @@ limitations under the License. */ #include #include -#include "paddle/pten/api/include/math.h" +#include "paddle/pten/include/math.h" #include "paddle/pten/api/lib/utils/allocator.h" #include "paddle/pten/core/dense_tensor.h" #include "paddle/pten/core/kernel_registry.h" -#include "paddle/pten/include/math.h" - PT_DECLARE_MODULE(MathCPU); #if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP) diff --git a/paddle/pten/tests/utils/CMakeLists.txt b/paddle/pten/tests/utils/CMakeLists.txt deleted file mode 100644 index e997481932..0000000000 --- a/paddle/pten/tests/utils/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -cc_test(test_intrusive_ptr SRCS test_intrusive_ptr.cc) -cc_test(test_type_info SRCS test_type_info.cc) -- GitLab