diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index f17ec328f0c2cd69954f8a446e8943a0292f995c..d5771de678e4a4c783e5282f82a2bd719d0cb652 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 0000000000000000000000000000000000000000..5a01051edf077a5c245badf14aced57be811051a --- /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 0000000000000000000000000000000000000000..bbb76fe7fdf5c3089572b20e71eeb6c3960e8277 --- /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 0000000000000000000000000000000000000000..4f64b2eda08a970f04753a317420cd6b16287a77 --- /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 0000000000000000000000000000000000000000..72c4f42ee5bde6f30b0b5f2ff713c0becb9963d6 --- /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 0000000000000000000000000000000000000000..31ac7c53e2d36ac03a0a312335178c0d564b7bfa --- /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 0000000000000000000000000000000000000000..fecdc558790a9c538c2748ac33c89bf55bb0b3cf --- /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 5ed89e78f1152d7a20ece2ae1ffaffdd3f8dcc3b..229566f59e6425d8053a2f7781543c8be9f7b11a 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 b7ef5691e47604bc176d44c68642942607685b0a..862b917906b29d31e2bd9400b31d5fedbc8070c4 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 0000000000000000000000000000000000000000..c0963b5001fa6d5e56f0da98d57cfe498b416a66 --- /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 0000000000000000000000000000000000000000..8ebe1ef237bdfa5e954caca63a91bbbb4e585557 --- /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 0000000000000000000000000000000000000000..96167022ed9dd4672fba147ab466dc7d406b4bc4 --- /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 4029c286a5b288bbd1613cd0050882fb143dae54..51c96f5b7f696066ebe17b53ee51c532d03e8aa0 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 dca87a786b961d1b3c5a0194801fc29f27aeaa47..eda904cf4f5936fc842e0f581534c1b70e2b2e77 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;