diff --git a/paddle/fluid/framework/CMakeLists.txt b/paddle/fluid/framework/CMakeLists.txt index d0cbd7801ddaffd4b77e521c8e208d4a8b14ebf2..19a7b4389b7d16f1ac7646c620ebb3741a888585 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 4b427b3b4a38349f672aa8c643bd06ad7f39d360..387da3bc68f120f7ec3efb9977e1c99c859ac2bd 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 c89ef812846adb337c2abbd6b55854cec3ca0b97..34f8da94c661151494715e8819c002fb5594ecb6 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 8ac30a1fa6909a3e6cf9a404ab4a6cc9663cc487..0000000000000000000000000000000000000000 --- 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 3d2da6a5afdd1a6b780074d6b272a93346c898f8..9946821581c3af86c4f645954c0b927429665b1c 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 0000000000000000000000000000000000000000..2c6bd9c45d18a7f740376470b88278e5f3b7a2b1 --- /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 f79854e557e5a7786eeea48143848a73ceced415..2c7b65f98135be8b464badcfa0114d9c98e0d52d 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 a83ed76a6f8dfa1da0f0ccedbff993d01d6b2158..cbac4c541f519bdd1d95b07508749596987bb6d9 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 ca8ca38f65a3ee4cf68ebfaaf13e7eeacd1c31ea..3701c358c667e1c96851f5b5d18ac72101411057 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 4dd4222daabe473a499e510675225b25d20d4a46..d772e58659507a7d098fdba2b785a62fab019eac 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 0000000000000000000000000000000000000000..c0a5414d53e478bd6bea4d7eff82113681814e1c --- /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 2bae2cd4171650a3616c5dc8605eb064fe7616d2..1c17c881ed24f23148525072301c5eba3b671125 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 efa19670f25be16c6b9d597e5ebd9cef0453f8c4..7fe1b6c2ffd2be88afa2e6978f6c3e9be10745f1 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 bcdef84040523cd103a4f95d4dd9320e858303ea..28d58858bb42c8a5d778fa5ce9fa24a0a171bce4 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 34888200b2f1918ee76428c274fa8c0b9216a9ac..b25439cfe25279b3db97afe90643624fe014a62a 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 e74917263dafb5b16f7f1f50ed17c721868b23f8..0000000000000000000000000000000000000000 --- 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 0000000000000000000000000000000000000000..b0dc29de5214079157e93c5b67c25698b885af74 --- /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 4bf16e7517b7336209a887d4a6843c8bdf77fd9a..dadbcf098dd17a667a1039dcc4e184f35651b92e 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 0000000000000000000000000000000000000000..897c49f4f255e544222ea200e67603517921e851 --- /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 0000000000000000000000000000000000000000..8546396e18a9ac6a2771a6d1f61658727e336463 --- /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 0000000000000000000000000000000000000000..16e6aa5acdd987c7e2ffe63bf2c10774ba0f84f5 --- /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 0000000000000000000000000000000000000000..43f9371d72a2f64d2f32d85460ad47048179765c --- /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 2ecf6d199b55855bdb95f803d333084bcb2dc2b1..f1764e803250a7179e969c9c63ddaefa3148266c 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 e9974819322517b92c9c9b44e2fe3d00b91480d4..0000000000000000000000000000000000000000 --- 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)