From cb6caf5679f3da5cd22b59ca981352df09d112d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E6=99=93=E4=BC=9F?= <39303645+Shixiaowei02@users.noreply.github.com> Date: Mon, 7 Feb 2022 15:13:05 +0800 Subject: [PATCH] infrt kernel test, test=develop (#39361) --- paddle/infrt/CMakeLists.txt | 19 ++++++- paddle/infrt/backends/CMakeLists.txt | 1 + paddle/infrt/backends/host/CMakeLists.txt | 2 + paddle/infrt/backends/host/pten_allocator.cc | 16 ++++++ paddle/infrt/backends/host/pten_allocator.h | 33 +++++++++++ paddle/infrt/backends/host/pten_context.cc | 16 ++++++ paddle/infrt/backends/host/pten_context.h | 26 +++++++++ paddle/infrt/host_context/value.h | 10 +++- paddle/infrt/kernel/CMakeLists.txt | 10 +++- paddle/infrt/kernel/pten/CMakeLists.txt | 15 +++++ paddle/infrt/kernel/pten/tests/CMakeLists.txt | 15 +++++ .../kernel/pten/tests/reshape_kernel_test.cc | 56 +++++++++++++++++++ paddle/pten/backends/cpu/cpu_context.cc | 1 + paddle/pten/backends/cpu/cpu_context.h | 1 + 14 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 paddle/infrt/backends/CMakeLists.txt create mode 100644 paddle/infrt/backends/host/CMakeLists.txt create mode 100644 paddle/infrt/backends/host/pten_allocator.cc create mode 100644 paddle/infrt/backends/host/pten_allocator.h create mode 100644 paddle/infrt/backends/host/pten_context.cc create mode 100644 paddle/infrt/backends/host/pten_context.h create mode 100644 paddle/infrt/kernel/pten/CMakeLists.txt create mode 100644 paddle/infrt/kernel/pten/tests/CMakeLists.txt create mode 100644 paddle/infrt/kernel/pten/tests/reshape_kernel_test.cc diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index f17ec328f0c..d5771de678e 100644 --- a/paddle/infrt/CMakeLists.txt +++ b/paddle/infrt/CMakeLists.txt @@ -1,5 +1,4 @@ -#TO DO:remove fluid -include_directories(${PADDLE_SOURCE_DIR}/paddle/fluid/platform) +option(INFRT_WITH_PTEN "Compile PaddlePaddle InfRT with Paddle Tensor Library." ON) if (NOT WITH_INFRT) return() @@ -19,6 +18,15 @@ include(infrt_lib) set(infrt_src CACHE INTERNAL "" FORCE) +if (INFRT_WITH_PTEN) + add_definitions("-DINFRT_WITH_PTEN") +endif() + +if (INFRT_WITH_PTEN) + #TO DO:remove fluid + include_directories(${PADDLE_SOURCE_DIR}/paddle/fluid/platform) +endif() + # Gather headers for library publish. function(core_gather_headers) file(GLOB includes LIST_DIRECTORIES false RELATIVE ${CMAKE_SOURCE_DIR} *.h) @@ -68,6 +76,7 @@ endif() add_subdirectory(api) +add_subdirectory(backends) add_subdirectory(common) add_subdirectory(dialect) add_subdirectory(host_context) @@ -93,8 +102,14 @@ set(infrt_mlir_incs ) message(STATUS "infrt srcs:\n${infrt_src}") +if (INFRT_WITH_PTEN) +cc_library(infrt SHARED SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto pten dense_tensor) +cc_library(infrt_static SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto pten dense_tensor) +add_dependencies(infrt ${infrt_mlir_incs}) +else() cc_library(infrt SHARED SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto) cc_library(infrt_static SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto) add_dependencies(infrt ${infrt_mlir_incs}) +endif() add_custom_target(test_infrt_exec DEPENDS ${INFRT_TEST_TARGETS}) diff --git a/paddle/infrt/backends/CMakeLists.txt b/paddle/infrt/backends/CMakeLists.txt new file mode 100644 index 00000000000..5a01051edf0 --- /dev/null +++ b/paddle/infrt/backends/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(host) diff --git a/paddle/infrt/backends/host/CMakeLists.txt b/paddle/infrt/backends/host/CMakeLists.txt new file mode 100644 index 00000000000..bbb76fe7fdf --- /dev/null +++ b/paddle/infrt/backends/host/CMakeLists.txt @@ -0,0 +1,2 @@ +cc_library(pten_cpu_context SRCS pten_context.cc DEPS pten) +cc_library(pten_cpu_allocator SRCS pten_allocator.cc DEPS pten) diff --git a/paddle/infrt/backends/host/pten_allocator.cc b/paddle/infrt/backends/host/pten_allocator.cc new file mode 100644 index 00000000000..4f64b2eda08 --- /dev/null +++ b/paddle/infrt/backends/host/pten_allocator.cc @@ -0,0 +1,16 @@ +/* Copyright (c) 2022 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/infrt/backends/host/pten_allocator.h" + +namespace infrt { +namespace backends {} // namespace backends +} // namespace infrt diff --git a/paddle/infrt/backends/host/pten_allocator.h b/paddle/infrt/backends/host/pten_allocator.h new file mode 100644 index 00000000000..72c4f42ee5b --- /dev/null +++ b/paddle/infrt/backends/host/pten_allocator.h @@ -0,0 +1,33 @@ +/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#pragma once + +#include "paddle/pten/core/allocator.h" + +namespace infrt { +namespace backends { + +class HostPtenAllocator : public pten::Allocator { + public: + static void deleter(pten::Allocation* ptr) { ::operator delete(ptr); } + + AllocationPtr Allocate(size_t bytes_size) { + return AllocationPtr( + new pten::Allocation(::operator new(bytes_size), + bytes_size, + pten::Place(pten::AllocationType::CPU)), + deleter); + } +}; + +} // namespace backends +} // namespace infrt diff --git a/paddle/infrt/backends/host/pten_context.cc b/paddle/infrt/backends/host/pten_context.cc new file mode 100644 index 00000000000..31ac7c53e2d --- /dev/null +++ b/paddle/infrt/backends/host/pten_context.cc @@ -0,0 +1,16 @@ +/* Copyright (c) 2022 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/infrt/backends/host/pten_context.h" + +namespace infrt { +namespace backends {} // namespace backends +} // namespace infrt diff --git a/paddle/infrt/backends/host/pten_context.h b/paddle/infrt/backends/host/pten_context.h new file mode 100644 index 00000000000..fecdc558790 --- /dev/null +++ b/paddle/infrt/backends/host/pten_context.h @@ -0,0 +1,26 @@ +/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. */ + +#pragma once + +#include "paddle/pten/backends/cpu/cpu_context.h" + +namespace infrt { +namespace backends { + +class HostPtenContext : public pten::CPUContext { + public: + using Base = pten::CPUContext; + using pten::CPUContext::SetEigenDevice; +}; + +} // namespace backends +} // namespace infrt diff --git a/paddle/infrt/host_context/value.h b/paddle/infrt/host_context/value.h index 5ed89e78f11..229566f59e6 100644 --- a/paddle/infrt/host_context/value.h +++ b/paddle/infrt/host_context/value.h @@ -28,6 +28,10 @@ #include "paddle/infrt/tensor/dense_tensor_view.h" #include "paddle/infrt/tensor/tensor_map.h" #include "paddle/infrt/tensor/tensor_shape.h" +#ifdef INFRT_WITH_PTEN +#include "paddle/pten/backends/cpu/cpu_context.h" +#include "paddle/pten/core/dense_tensor.h" +#endif // INFRT_WITH_PTEN namespace infrt { namespace host_context { @@ -45,8 +49,10 @@ using ValueVariantType = Variant, std::vector, std::vector, diff --git a/paddle/infrt/kernel/CMakeLists.txt b/paddle/infrt/kernel/CMakeLists.txt index b7ef5691e47..862b917906b 100644 --- a/paddle/infrt/kernel/CMakeLists.txt +++ b/paddle/infrt/kernel/CMakeLists.txt @@ -1,10 +1,18 @@ core_gather_headers() +if (INFRT_WITH_PTEN) + set(pten_kernel_src pten_kernels.cc) +endif() + gather_srcs(infrt_src SRCS basic_kernels.cc - # pten_kernels.cc + ${pten_kernel_src} test_kernels.cc tensor_shape_kernels.cc tensor_kernels.cc control_flow_kernels.cc ) + +if(INFRT_WITH_PTEN) + add_subdirectory(pten) +endif() diff --git a/paddle/infrt/kernel/pten/CMakeLists.txt b/paddle/infrt/kernel/pten/CMakeLists.txt new file mode 100644 index 00000000000..c0963b5001f --- /dev/null +++ b/paddle/infrt/kernel/pten/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (c) 2022 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. + +add_subdirectory(tests) diff --git a/paddle/infrt/kernel/pten/tests/CMakeLists.txt b/paddle/infrt/kernel/pten/tests/CMakeLists.txt new file mode 100644 index 00000000000..8ebe1ef237b --- /dev/null +++ b/paddle/infrt/kernel/pten/tests/CMakeLists.txt @@ -0,0 +1,15 @@ +# Copyright (c) 2022 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. + +cc_test(reshape_kernel_test SRCS reshape_kernel_test.cc DEPS pten reshape_kernel) diff --git a/paddle/infrt/kernel/pten/tests/reshape_kernel_test.cc b/paddle/infrt/kernel/pten/tests/reshape_kernel_test.cc new file mode 100644 index 00000000000..96167022ed9 --- /dev/null +++ b/paddle/infrt/kernel/pten/tests/reshape_kernel_test.cc @@ -0,0 +1,56 @@ +/* Copyright (c) 2022 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/infrt/backends/host/pten_allocator.h" +#include "paddle/infrt/backends/host/pten_context.h" +#include "paddle/pten/kernels/reshape_kernel.h" + +namespace infrt { +namespace kernels { +namespace tests { + +TEST(pten, reshape) { + auto allocator = backends::HostPtenAllocator(); + auto context = backends::HostPtenContext(); + context.SetDeviceAllocator(&allocator); + context.SetHostAllocator(&allocator); + auto tensor_meta = + pten::DenseTensorMeta(pten::DataType::FLOAT32, + pten::framework::make_ddim({3, 2, 2, 3}), + pten::DataLayout::NCHW); + auto dense_x = pten::DenseTensor(&allocator, std::move(tensor_meta)); + auto* dense_x_data = static_cast( + dense_x.AllocateFrom(&allocator, pten::DataType::FLOAT32)); + + // The writing is cumbersome and needs to be adjusted. + auto out = pten::Reshape( + context, dense_x, {12, 3}); + std::vector expect_shape = {12, 3}; + ASSERT_EQ(out.dims()[0], expect_shape[0]); + ASSERT_EQ(out.dims()[1], expect_shape[1]); + ASSERT_EQ(out.numel(), 36); + ASSERT_EQ(out.meta().dtype, 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); +} + +} // namespace tests +} // namespace kernels +} // namespace infrt diff --git a/paddle/pten/backends/cpu/cpu_context.cc b/paddle/pten/backends/cpu/cpu_context.cc index 4029c286a5b..51c96f5b7f6 100644 --- a/paddle/pten/backends/cpu/cpu_context.cc +++ b/paddle/pten/backends/cpu/cpu_context.cc @@ -52,6 +52,7 @@ struct CPUContext::Impl { CPUContext::CPUContext() : DeviceContext(), impl_(std::make_unique()) {} +CPUContext::CPUContext(CPUContext&&) = default; CPUContext::CPUContext(const Place& place) : DeviceContext(), impl_(std::make_unique(place)) {} diff --git a/paddle/pten/backends/cpu/cpu_context.h b/paddle/pten/backends/cpu/cpu_context.h index dca87a786b9..eda904cf4f5 100644 --- a/paddle/pten/backends/cpu/cpu_context.h +++ b/paddle/pten/backends/cpu/cpu_context.h @@ -27,6 +27,7 @@ namespace pten { class CPUContext : public DeviceContext { public: CPUContext(); + CPUContext(CPUContext&&); explicit CPUContext(const Place&); virtual ~CPUContext(); Eigen::DefaultDevice* eigen_device() const; -- GitLab