From 8f2d14adaff7155de76e572eb6019890825b507b 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: Thu, 17 Feb 2022 15:09:52 +0800 Subject: [PATCH] change classes to pten, test=develop (#39643) --- paddle/infrt/CMakeLists.txt | 6 +- paddle/infrt/backends/host/pten_allocator.h | 33 ++++++++ paddle/infrt/backends/host/pten_context.h | 26 ++++++ paddle/infrt/dialect/pten/CMakeLists.txt | 1 + .../infrt/dialect/pten/infrt_pten_kernel.td | 26 ++++++ paddle/infrt/dialect/pten/infrt_pten_tensor.h | 1 + .../infrt/dialect/pten/infrt_pten_tensor.td | 82 ++++--------------- paddle/infrt/dialect/pten/pten_base.cc | 28 +++++-- paddle/infrt/host_context/kernel_registry.h | 3 +- .../host_context/kernel_registry_test.cc | 2 +- paddle/infrt/host_context/mlir_exec.cc | 6 ++ paddle/infrt/host_context/value.cc | 34 ++++---- paddle/infrt/host_context/value.h | 37 +++++++-- paddle/infrt/kernel/CMakeLists.txt | 2 + paddle/infrt/kernel/pten/CMakeLists.txt | 19 +++++ paddle/infrt/kernel/pten/allocator_kernels.cc | 25 ++++++ .../pten/allocator_kernels.h} | 19 ++--- paddle/infrt/kernel/pten/context_kernels.cc | 25 ++++++ paddle/infrt/kernel/pten/context_kernels.h | 28 +++++++ .../infrt/kernel/pten/dense_tensor_kernels.cc | 38 +++++++++ .../dense_tensor_kernels.h} | 29 +++---- .../pten}/infershaped/elementwise_add.h | 36 ++++---- .../infershaped/infershape_launchers_test.cc | 36 ++++---- .../infershaped_kernel_launcher.cc | 19 +++-- .../infershaped/infershaped_kernel_launcher.h | 8 +- .../infershaped_kernel_launchers.cc | 36 ++++++++ .../infershaped_kernel_launchers.h | 10 +-- .../pten}/infershaped/infershaped_utils.h | 6 +- paddle/infrt/kernel/pten/registry.cc | 61 ++++++++++++++ .../{pten_kernels.h => pten/registry.h} | 0 paddle/infrt/naive/CMakeLists.txt | 8 -- .../infershaped_kernel_launchers.cc | 34 -------- .../naive/infershaped/infershaped_registry.cc | 55 ------------- .../naive/infershaped/infershaped_registry.h | 56 ------------- paddle/infrt/naive/meta_tensor.h | 47 ----------- .../tests/dialect/pten/dense_tensor.mlir | 9 +- paddle/pten/backends/cpu/cpu_context.cc | 4 + paddle/pten/backends/cpu/cpu_context.h | 2 + paddle/pten/core/device_context.cc | 2 + paddle/pten/core/device_context.h | 5 ++ 40 files changed, 518 insertions(+), 386 deletions(-) create mode 100644 paddle/infrt/backends/host/pten_allocator.h create mode 100644 paddle/infrt/backends/host/pten_context.h create mode 100644 paddle/infrt/dialect/pten/infrt_pten_kernel.td create mode 100644 paddle/infrt/kernel/pten/CMakeLists.txt create mode 100644 paddle/infrt/kernel/pten/allocator_kernels.cc rename paddle/infrt/{naive/meta_tensor.cc => kernel/pten/allocator_kernels.h} (64%) create mode 100644 paddle/infrt/kernel/pten/context_kernels.cc create mode 100644 paddle/infrt/kernel/pten/context_kernels.h create mode 100644 paddle/infrt/kernel/pten/dense_tensor_kernels.cc rename paddle/infrt/kernel/{pten_kernels.cc => pten/dense_tensor_kernels.h} (55%) rename paddle/infrt/{naive => kernel/pten}/infershaped/elementwise_add.h (67%) rename paddle/infrt/{naive => kernel/pten}/infershaped/infershape_launchers_test.cc (56%) rename paddle/infrt/{naive => kernel/pten}/infershaped/infershaped_kernel_launcher.cc (74%) rename paddle/infrt/{naive => kernel/pten}/infershaped/infershaped_kernel_launcher.h (90%) create mode 100644 paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.cc rename paddle/infrt/{naive => kernel/pten}/infershaped/infershaped_kernel_launchers.h (79%) rename paddle/infrt/{naive => kernel/pten}/infershaped/infershaped_utils.h (95%) create mode 100644 paddle/infrt/kernel/pten/registry.cc rename paddle/infrt/kernel/{pten_kernels.h => pten/registry.h} (100%) delete mode 100644 paddle/infrt/naive/CMakeLists.txt delete mode 100644 paddle/infrt/naive/infershaped/infershaped_kernel_launchers.cc delete mode 100644 paddle/infrt/naive/infershaped/infershaped_registry.cc delete mode 100644 paddle/infrt/naive/infershaped/infershaped_registry.h delete mode 100644 paddle/infrt/naive/meta_tensor.h diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index c8253effe8..2486c54d5a 100644 --- a/paddle/infrt/CMakeLists.txt +++ b/paddle/infrt/CMakeLists.txt @@ -82,7 +82,6 @@ add_subdirectory(tensor) add_subdirectory(support) add_subdirectory(external_kernels) add_subdirectory(paddle) -add_subdirectory(naive) add_subdirectory(tests) @@ -99,14 +98,15 @@ set(infrt_mlir_incs trt_ops_inc ) if (INFRT_WITH_PTEN) + set(pten_libs pten) set(infrt_mlir_incs ${infrt_mlir_incs} MLIRinfrt_pten_tensorIncGen MLIRinfrt_pten_baseIncGen ) endif() -cc_library(infrt SHARED SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto infrt_naive) -cc_library(infrt_static SRCS ${infrt_src} DEPS glog boost ${mlir_libs} paddle_framework_proto) +cc_library(infrt SHARED SRCS ${infrt_src} DEPS glog boost ${mlir_libs} ${pten_libs} paddle_framework_proto infrt_naive) +cc_library(infrt_static SRCS ${infrt_src} DEPS glog boost ${mlir_libs} ${pten_libs} paddle_framework_proto) add_dependencies(infrt ${infrt_mlir_incs} mlir-headers) add_custom_target(test_infrt_exec DEPENDS ${INFRT_TEST_TARGETS}) diff --git a/paddle/infrt/backends/host/pten_allocator.h b/paddle/infrt/backends/host/pten_allocator.h new file mode 100644 index 0000000000..172a808afb --- /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 CpuPtenAllocator : 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.h b/paddle/infrt/backends/host/pten_context.h new file mode 100644 index 0000000000..1f5efeb272 --- /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 CpuPtenContext : public pten::CPUContext { + public: + using Base = pten::CPUContext; + using pten::CPUContext::SetEigenDevice; +}; + +} // namespace backends +} // namespace infrt diff --git a/paddle/infrt/dialect/pten/CMakeLists.txt b/paddle/infrt/dialect/pten/CMakeLists.txt index 0fb268952d..b4ed5cdc1d 100644 --- a/paddle/infrt/dialect/pten/CMakeLists.txt +++ b/paddle/infrt/dialect/pten/CMakeLists.txt @@ -5,6 +5,7 @@ endif() #mlir_tablegen_on(infrt_pten_base DIALECT pten) add_mlir_dialect(infrt_pten_base pten) add_mlir_dialect(infrt_pten_tensor pten_dt) +add_mlir_dialect(infrt_pten_kernel pten_kernel) #mlir_tablegen_on(infrt_pten_tensor) gather_srcs(infrt_src SRCS diff --git a/paddle/infrt/dialect/pten/infrt_pten_kernel.td b/paddle/infrt/dialect/pten/infrt_pten_kernel.td new file mode 100644 index 0000000000..a3a1609d99 --- /dev/null +++ b/paddle/infrt/dialect/pten/infrt_pten_kernel.td @@ -0,0 +1,26 @@ +#ifndef PTEN_KERNEL +#define PTEN_KERNEL + +include "paddle/infrt/dialect/pten/infrt_pten_tensor.td" + +def PTEN_KernelDialect : Dialect { + let name = "pten_kernel"; + + let description = [{ + The PTEN Kernel dialect. + }]; + + let cppNamespace = "::infrt::pten"; +} + +// PTEN Kernel related ops. +class PDT_Kernel traits = []> : Op { +} + +def FakeKernelOp : PDT_Kernel<"pten.matmul.host.fp32"> { + let arguments = (ins CPU_Context:$dev_ctx, TensorType:$x, TensorType:$y, BoolAttr:$transpose_x, BoolAttr:$transpose_y); + let results = (outs TensorType:$output); +} + +#endif + diff --git a/paddle/infrt/dialect/pten/infrt_pten_tensor.h b/paddle/infrt/dialect/pten/infrt_pten_tensor.h index 24ac2d851f..5fe259300d 100644 --- a/paddle/infrt/dialect/pten/infrt_pten_tensor.h +++ b/paddle/infrt/dialect/pten/infrt_pten_tensor.h @@ -33,6 +33,7 @@ #include "paddle/infrt/dialect/pten/infrt_pten_tensorTypes.h.inc" #include "paddle/infrt/dialect/dense_tensor.h" +#include "paddle/infrt/dialect/pten/pten_base.h" // NOLINT #define GET_OP_CLASSES #include "paddle/infrt/dialect/pten/infrt_pten_tensor.h.inc" diff --git a/paddle/infrt/dialect/pten/infrt_pten_tensor.td b/paddle/infrt/dialect/pten/infrt_pten_tensor.td index 040c8ec3d3..528f0f9196 100644 --- a/paddle/infrt/dialect/pten/infrt_pten_tensor.td +++ b/paddle/infrt/dialect/pten/infrt_pten_tensor.td @@ -21,84 +21,36 @@ def PTEN_DenseTensorDialect : Dialect { class PDT_Op traits = []> : Op { } -class CreateUninitTensorOp - : PDT_Op<"create_uninit_tensor." # dtype, [NoSideEffect]> { - let summary = "pdt.create_uninit_tensor operation"; - - let description = [{ - An operation that creates an uninitialized tensor. - }]; - - let arguments = (ins I64ArrayAttr:$shape); - let results = (outs TensorType:$output); -} - -class CreateInitedTensorOp - : PDT_Op<"create_inited_tensor." #dtype, [NoSideEffect]> { - let summary = "pdt.create_inited_tensor operation"; - - let description = [{ - An operation that creates an tensor with shape and values assigned. - }]; - - let arguments = (ins I64ArrayAttr:$shape, array_attr:$values); +class CreateDenseTensorOp + : PDT_Op<"create_dense_tensor." # place # "." # dtype # "." # layout, [NoSideEffect]> { + let arguments = (ins CPU_Allocator:$allocator, I64ArrayAttr:$dims, I64ArrayAttr:$lod); let results = (outs TensorType:$output); } -def PrintTensorOp : PDT_Op<"print_tensor"> { - let summary = "pdt.print_tensor operation"; - - let description = [{ - An operation that prints a tensor. - }]; - - let arguments = (ins TensorType:$input); - let results = (outs); - let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; -} - -class FillTensor : - PDT_Op<"fill_tensor." # dtype> { - let summary = "dt.fill_tensor operation"; - - let description = [{ - An operation that fills an input tensor with a values. - }]; - +class FillDenseTensorOp : + PDT_Op<"fill_dense_tensor." # dtype> { let arguments = (ins TensorType:$input, attr_type:$value ); let results = (outs); - - let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; } -class FillTensorWithConstantOp : - PDT_Op<"fill_tensor_with_constant." # dtype> { - let summary = "dt.fill_tensor_with_constant operation"; - - let description = [{ - An operation that fills an input tensor with a single value. - }]; - - let arguments = (ins - TensorType:$input, - AnyAttr:$value - ); - let results = (outs); - - let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; +class CreateCPUAllocatorOp + : PDT_Op<"create_allocator." # "cpu", [NoSideEffect]> { + let arguments = (ins); + let results = (outs CPU_Allocator:$output); } -foreach dtype = ["ui8", "ui16", "ui32", "ui64", "i32", "f32", "f64", "i64"] in { - def PDT_CreateUninitTensorOp_#dtype : CreateUninitTensorOp; - def PDT_FillTensorWithConstantOp_#dtype : FillTensorWithConstantOp; +class CreateCPUContextOp + : PDT_Op<"create_context." # "cpu", [NoSideEffect]> { + let arguments = (ins); + let results = (outs CPU_Context:$output); } -def PDT_FillTensor_f32: FillTensor<"f32", F32ArrayAttr>; -def PDT_FillTensor_i32: FillTensor<"i32", I32ArrayAttr>; -def PDT_CreateInitedTensorOp_f32 : CreateInitedTensorOp<"f32", F32ArrayAttr>; -def PDT_CreateInitedTensorOp_i32 : CreateInitedTensorOp<"i32", I32ArrayAttr>; +def PDT_CreateDenseTensorOp_cpu_f32_nchw : CreateDenseTensorOp<"cpu", "f32", "nchw">; +def PDT_FillDenseTensorOp_f32 : FillDenseTensorOp; +def PDT_CreateAllocatorOp_cpu : CreateCPUAllocatorOp; +def PDT_CreateContextOp_cpu : CreateCPUContextOp; #endif diff --git a/paddle/infrt/dialect/pten/pten_base.cc b/paddle/infrt/dialect/pten/pten_base.cc index ac23d44248..ba87787dd7 100644 --- a/paddle/infrt/dialect/pten/pten_base.cc +++ b/paddle/infrt/dialect/pten/pten_base.cc @@ -29,7 +29,23 @@ namespace pten { void PTENDialect::printType(::mlir::Type type, mlir::DialectAsmPrinter& os) const { - Dialect::printType(type, os); + if (type.isa()) { + os << "CPU_Allocator"; + return; + } + if (type.isa()) { + os << "GPU_Allocator"; + return; + } + if (type.isa()) { + os << "CPU_Context"; + return; + } + if (type.isa()) { + os << "GPU_Context"; + return; + } + llvm_unreachable("unexpected 'allocator/context' type kind"); } void PTENDialect::initialize() { @@ -46,14 +62,16 @@ void PTENDialect::initialize() { mlir::Type PTENDialect::parseType(mlir::DialectAsmParser& parser) const { llvm::StringRef keyword; if (parser.parseKeyword(&keyword)) return mlir::Type(); - if (keyword == "allocator_CPU") { + if (keyword == "CPU_allocator") { return CPUAllocatorType::get(parser.getContext()); - } else if (keyword == "allocator_GPU") { + } else if (keyword == "GPU_allocator") { return GPUAllocatorType::get(parser.getContext()); - } else if (keyword == "context_CPU") { + } else if (keyword == "CPU_context") { return CPUContextType::get(parser.getContext()); - } else if (keyword == "context_GPU") { + } else if (keyword == "GPU_context") { return GPUContextType::get(parser.getContext()); + } else { + llvm_unreachable("unexpected 'allocator/context' type kind"); } return mlir::Type(); diff --git a/paddle/infrt/host_context/kernel_registry.h b/paddle/infrt/host_context/kernel_registry.h index d65969999f..a813f690ef 100644 --- a/paddle/infrt/host_context/kernel_registry.h +++ b/paddle/infrt/host_context/kernel_registry.h @@ -14,6 +14,7 @@ #pragma once +#include #include #include #include @@ -23,7 +24,7 @@ namespace host_context { class KernelFrame; -using KernelImplementation = void (*)(KernelFrame *frame); +using KernelImplementation = std::function; /** * Hold the kernels registered in the system. diff --git a/paddle/infrt/host_context/kernel_registry_test.cc b/paddle/infrt/host_context/kernel_registry_test.cc index 7fca563430..fd2aecb3e6 100644 --- a/paddle/infrt/host_context/kernel_registry_test.cc +++ b/paddle/infrt/host_context/kernel_registry_test.cc @@ -28,7 +28,7 @@ TEST(KernelRegistry, basic) { std::string key = "infrt.test.add.i32"; registry.AddKernel(key, INFRT_KERNEL(add_i32)); - auto* kernel_impl = registry.GetKernel(key); + const auto& kernel_impl = registry.GetKernel(key); ASSERT_TRUE(kernel_impl); ValueRef a(1); diff --git a/paddle/infrt/host_context/mlir_exec.cc b/paddle/infrt/host_context/mlir_exec.cc index b0d70af5ef..62c907bc91 100644 --- a/paddle/infrt/host_context/mlir_exec.cc +++ b/paddle/infrt/host_context/mlir_exec.cc @@ -28,6 +28,9 @@ #include "paddle/infrt/kernel/tensor_kernels.h" #include "paddle/infrt/kernel/tensor_shape_kernels.h" #include "paddle/infrt/kernel/test_kernels.h" +#ifdef INFRT_WITH_PTEN +#include "paddle/infrt/kernel/pten/registry.h" +#endif static llvm::cl::list cl_shared_libs( // NOLINT "shared_libs", @@ -53,6 +56,9 @@ int main(int argc, char** argv) { kernel::RegisterTensorShapeKernels(®istry); kernel::RegisterTensorKernels(®istry); kernel::RegisterControlFlowKernels(®istry); +#ifdef INFRT_WITH_PTEN + kernel::RegisterPtenKernels(®istry); +#endif // load extra shared library for (const auto& lib_path : cl_shared_libs) { diff --git a/paddle/infrt/host_context/value.cc b/paddle/infrt/host_context/value.cc index 1c5a577092..e8b904efb7 100644 --- a/paddle/infrt/host_context/value.cc +++ b/paddle/infrt/host_context/value.cc @@ -24,7 +24,13 @@ ValueRef::ValueRef(int64_t val) : Shared(new Value(val)) {} ValueRef::ValueRef(float val) : Shared(new Value(val)) {} ValueRef::ValueRef(double val) : Shared(new Value(val)) {} ValueRef::ValueRef(bool val) : Shared(new Value(val)) {} -ValueRef::ValueRef(naive::MetaTensor&& val) +ValueRef::ValueRef(backends::CpuPtenContext&& val) + : Shared(new Value(std::move(val))) {} +ValueRef::ValueRef(::pten::CPUContext&& val) + : Shared(new Value(std::move(val))) {} +ValueRef::ValueRef(::pten::DenseTensor&& val) + : Shared(new Value(std::move(val))) {} +ValueRef::ValueRef(::pten::MetaTensor&& val) : Shared(new Value(std::move(val))) {} const char* Value::type_info() const { return __type_info__; } @@ -36,31 +42,31 @@ void CopyTo(const Value& from, Value* to) { [&](auto&& arg) { using T = std::decay_t; if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else if (std::is_same>::value) - to->data = arg; + to->data = reinterpret_cast const&>(arg); else if (std::is_same>::value) - to->data = arg; + to->data = reinterpret_cast const&>(arg); else if (std::is_same::value) - to->data = arg; + to->data = reinterpret_cast(arg); else LOG(FATAL) << "Not supported Value copy: " << typeid(T).name(); }, diff --git a/paddle/infrt/host_context/value.h b/paddle/infrt/host_context/value.h index 904e51f928..f623e14151 100644 --- a/paddle/infrt/host_context/value.h +++ b/paddle/infrt/host_context/value.h @@ -23,15 +23,19 @@ #include "paddle/infrt/common/object.h" #include "paddle/infrt/common/shared.h" #include "paddle/infrt/host_context/function.h" -#include "paddle/infrt/naive/meta_tensor.h" #include "paddle/infrt/support/variant.h" #include "paddle/infrt/tensor/dense_host_tensor.h" #include "paddle/infrt/tensor/dense_tensor_view.h" #include "paddle/infrt/tensor/tensor_map.h" #include "paddle/infrt/tensor/tensor_shape.h" -// Disabled temporarily for failed compile, will enable latter. -// #include "paddle/pten/backends/cpu/cpu_context.h" -// #include "paddle/pten/core/dense_tensor.h" +#include "paddle/pten/core/meta_tensor.h" + +#ifdef INFRT_WITH_PTEN +#include "paddle/infrt/backends/host/pten_allocator.h" +#include "paddle/infrt/backends/host/pten_context.h" +#include "paddle/pten/backends/cpu/cpu_context.h" +#include "paddle/pten/core/dense_tensor.h" +#endif namespace infrt { namespace host_context { @@ -44,14 +48,20 @@ using ValueVariantType = Variant, std::vector, std::vector, @@ -84,7 +94,13 @@ class Value : public common::Object { explicit Value(tensor::TensorShape&& x) : data(std::move(x)) {} explicit Value(tensor::DenseHostTensor&& x) : data(std::move(x)) {} explicit Value(MlirFunctionExecutable* x) : data(x) {} - explicit Value(naive::MetaTensor&& x) : data(std::move(x)) {} +#ifdef INFRT_WITH_PTEN + explicit Value(backends::CpuPtenContext&& x) : data(std::move(x)) {} + explicit Value(::pten::CPUContext&& x) : data(std::move(x)) {} + explicit Value(::pten::DenseTensor&& x) : data(std::move(x)) {} + explicit Value(::pten::MetaTensor&& x) : data(std::move(x)) {} + explicit Value(backends::CpuPtenAllocator&& x) : data(std::move(x)) {} +#endif template const T& get() const { @@ -142,7 +158,10 @@ class ValueRef : common::Shared { explicit ValueRef(float val); explicit ValueRef(double val); explicit ValueRef(bool val); - explicit ValueRef(naive::MetaTensor&& val); + explicit ValueRef(::pten::MetaTensor&& val); + explicit ValueRef(backends::CpuPtenContext&& x); + explicit ValueRef(::pten::CPUContext&& x); + explicit ValueRef(::pten::DenseTensor&& x); using common::Shared::get; using common::Shared::Reset; diff --git a/paddle/infrt/kernel/CMakeLists.txt b/paddle/infrt/kernel/CMakeLists.txt index b7ef5691e4..402665119a 100644 --- a/paddle/infrt/kernel/CMakeLists.txt +++ b/paddle/infrt/kernel/CMakeLists.txt @@ -1,3 +1,5 @@ +add_subdirectory(pten) + core_gather_headers() gather_srcs(infrt_src SRCS diff --git a/paddle/infrt/kernel/pten/CMakeLists.txt b/paddle/infrt/kernel/pten/CMakeLists.txt new file mode 100644 index 0000000000..65c10b0b15 --- /dev/null +++ b/paddle/infrt/kernel/pten/CMakeLists.txt @@ -0,0 +1,19 @@ +if (NOT INFRT_WITH_PTEN) + return() +endif() + +core_gather_headers() + +gather_srcs(infrt_src SRCS + registry.cc + dense_tensor_kernels.cc + context_kernels.cc + allocator_kernels.cc +) + +cc_library(infrt_naive SRCS infershaped/infershaped_kernel_launcher.cc + infershaped/infershaped_kernel_launchers.cc + ) + +cc_test_tiny(test_infrt_infershape_launchers SRCS +infershaped/infershape_launchers_test.cc DEPS infrt) diff --git a/paddle/infrt/kernel/pten/allocator_kernels.cc b/paddle/infrt/kernel/pten/allocator_kernels.cc new file mode 100644 index 0000000000..d3ecbed15d --- /dev/null +++ b/paddle/infrt/kernel/pten/allocator_kernels.cc @@ -0,0 +1,25 @@ +// 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/kernel/pten/allocator_kernels.h" + +namespace infrt { +namespace kernel { +namespace pten { + +backends::CpuPtenAllocator CreateCpuAllocator() { return {}; } + +} // namespace pten +} // namespace kernel +} // namespace infrt diff --git a/paddle/infrt/naive/meta_tensor.cc b/paddle/infrt/kernel/pten/allocator_kernels.h similarity index 64% rename from paddle/infrt/naive/meta_tensor.cc rename to paddle/infrt/kernel/pten/allocator_kernels.h index 2f7ee3a69e..3312771119 100644 --- a/paddle/infrt/naive/meta_tensor.cc +++ b/paddle/infrt/kernel/pten/allocator_kernels.h @@ -12,20 +12,17 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/infrt/naive/meta_tensor.h" +#pragma once -#include "paddle/infrt/tensor/dense_host_tensor.h" -#include "paddle/infrt/tensor/tensor_shape.h" +#include "paddle/infrt/backends/host/pten_allocator.h" +#include "paddle/pten/core/dense_tensor.h" namespace infrt { -namespace naive { +namespace kernel { +namespace pten { -const tensor::TensorShape& MetaTensor::shape() const { - return mutable_tensor_->shape(); -} -tensor::TensorShape* MetaTensor::mutable_shape() { - return mutable_tensor_->mutable_shape(); -} +backends::CpuPtenAllocator CreateCpuAllocator(); -} // namespace naive +} // namespace pten +} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/kernel/pten/context_kernels.cc b/paddle/infrt/kernel/pten/context_kernels.cc new file mode 100644 index 0000000000..0c5e532121 --- /dev/null +++ b/paddle/infrt/kernel/pten/context_kernels.cc @@ -0,0 +1,25 @@ +// 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/kernel/pten/context_kernels.h" + +namespace infrt { +namespace kernel { +namespace pten { + +backends::CpuPtenContext CreateCpuContext() { return {}; } + +} // namespace pten +} // namespace kernel +} // namespace infrt diff --git a/paddle/infrt/kernel/pten/context_kernels.h b/paddle/infrt/kernel/pten/context_kernels.h new file mode 100644 index 0000000000..14a151d9d1 --- /dev/null +++ b/paddle/infrt/kernel/pten/context_kernels.h @@ -0,0 +1,28 @@ +// 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/infrt/backends/host/pten_context.h" +#include "paddle/pten/core/dense_tensor.h" + +namespace infrt { +namespace kernel { +namespace pten { + +backends::CpuPtenContext CreateCpuContext(); + +} // namespace pten +} // namespace kernel +} // namespace infrt diff --git a/paddle/infrt/kernel/pten/dense_tensor_kernels.cc b/paddle/infrt/kernel/pten/dense_tensor_kernels.cc new file mode 100644 index 0000000000..2db5f4a3c1 --- /dev/null +++ b/paddle/infrt/kernel/pten/dense_tensor_kernels.cc @@ -0,0 +1,38 @@ +// 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/kernel/pten/dense_tensor_kernels.h" + +namespace infrt { +namespace kernel { +namespace pten { + +::pten::DenseTensor CreateDenseTensorCpuF32Nchw( + backends::CpuPtenAllocator* allocator, + host_context::Attribute> dims, + host_context::Attribute> lod) { + return ::pten::DenseTensor( + allocator, + ::pten::DenseTensorMeta(::pten::DataType::FLOAT32, + ::pten::framework::make_ddim(dims.get()), + ::pten::DataLayout::NCHW, + {})); +} + +void FillDenseTensorF32(::pten::DenseTensor* dense_tensor, + host_context::Attribute> values) {} + +} // namespace pten +} // namespace kernel +} // namespace infrt diff --git a/paddle/infrt/kernel/pten_kernels.cc b/paddle/infrt/kernel/pten/dense_tensor_kernels.h similarity index 55% rename from paddle/infrt/kernel/pten_kernels.cc rename to paddle/infrt/kernel/pten/dense_tensor_kernels.h index 62e2db659a..f60525707c 100644 --- a/paddle/infrt/kernel/pten_kernels.cc +++ b/paddle/infrt/kernel/pten/dense_tensor_kernels.h @@ -12,29 +12,24 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/infrt/kernel/pten_kernels.h" +#pragma once -#include -#include - -#include "paddle/infrt/host_context/kernel_registry.h" +#include "paddle/infrt/backends/host/pten_allocator.h" #include "paddle/infrt/host_context/kernel_utils.h" - -// Disable temporarily. -// #include "paddle/pten/backends/cpu/cpu_context.h" -// #include "paddle/pten/kernels/math_kernel.h" - -using infrt::host_context::Attribute; +#include "paddle/pten/core/dense_tensor.h" namespace infrt { namespace kernel { +namespace pten { + +::pten::DenseTensor CreateDenseTensorCpuF32Nchw( + backends::CpuPtenAllocator* allocator, + host_context::Attribute> dims, + host_context::Attribute> lod); -void RegisterPtenKernels(host_context::KernelRegistry* registry) { - registry->AddKernel("pd_cpu.add.float32", - INFRT_KERNEL(pten::AddKernel)); - registry->AddKernel("pd_cpu.add.int32", - INFRT_KERNEL(pten::AddKernel)); -} +void FillDenseTensorF32(::pten::DenseTensor* dense_tensor, + host_context::Attribute> values); +} // namespace pten } // namespace kernel } // namespace infrt diff --git a/paddle/infrt/naive/infershaped/elementwise_add.h b/paddle/infrt/kernel/pten/infershaped/elementwise_add.h similarity index 67% rename from paddle/infrt/naive/infershaped/elementwise_add.h rename to paddle/infrt/kernel/pten/infershaped/elementwise_add.h index ee044e38da..1d9d0106da 100644 --- a/paddle/infrt/naive/infershaped/elementwise_add.h +++ b/paddle/infrt/kernel/pten/infershaped/elementwise_add.h @@ -16,27 +16,23 @@ #include #include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/naive/infershaped/infershaped_kernel_launcher.h" -#include "paddle/infrt/naive/infershaped/infershaped_utils.h" +#include "paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.h" +#include "paddle/infrt/kernel/pten/infershaped/infershaped_utils.h" // This file contains a example of the infershape ElementwiseAdd kernel. // Some of the following code should be generated from PTEN by script. namespace infrt { -namespace naive { +namespace kernel { -static void ElementwiseAddInferShape(const MetaTensor& a, - const MetaTensor& b, - MetaTensor* c) { - CHECK(a.shape() == b.shape()) - << "ElementwiseAdd, but shapes of a b are not match"; - *c->mutable_shape() = a.shape(); -} +static void ElementwiseAddInferShape(const ::pten::MetaTensor& a, + const ::pten::MetaTensor& b, + ::pten::MetaTensor* c) {} -static void ElementwiseAdd(tensor::DenseHostTensor* /*Context*/, - const tensor::DenseHostTensor& a, - const tensor::DenseHostTensor& b, - tensor::DenseHostTensor* c) {} +static void ElementwiseAdd(const ::pten::CPUContext& /*Context*/, + const ::pten::DenseTensor& a, + const ::pten::DenseTensor& b, + ::pten::DenseTensor* c) {} template +void KernelLauncherFunc( + KernelLauncher launcher, + host_context::KernelFrame* frame) { + launcher.Invoke(frame); +} + +} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/naive/infershaped/infershape_launchers_test.cc b/paddle/infrt/kernel/pten/infershaped/infershape_launchers_test.cc similarity index 56% rename from paddle/infrt/naive/infershaped/infershape_launchers_test.cc rename to paddle/infrt/kernel/pten/infershaped/infershape_launchers_test.cc index ba6fdbdd57..64b99110d9 100644 --- a/paddle/infrt/naive/infershaped/infershape_launchers_test.cc +++ b/paddle/infrt/kernel/pten/infershaped/infershape_launchers_test.cc @@ -14,19 +14,17 @@ #include -#include "paddle/infrt/naive/infershaped/infershaped_kernel_launcher.h" -#include "paddle/infrt/naive/infershaped/infershaped_kernel_launchers.h" -#include "paddle/infrt/naive/infershaped/infershaped_registry.h" -#include "paddle/infrt/naive/infershaped/infershaped_utils.h" -#include "paddle/infrt/tensor/dense_host_tensor.h" +#include "paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.h" +#include "paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.h" +#include "paddle/infrt/kernel/pten/infershaped/infershaped_utils.h" namespace infrt { -namespace naive { +namespace kernel { namespace { -static void ElementwiseAddTest(const tensor::DenseHostTensor& a, - const tensor::DenseHostTensor& b, - tensor::DenseHostTensor* c); +static void ElementwiseAddTest(const ::pten::DenseTensor& a, + const ::pten::DenseTensor& b, + ::pten::DenseTensor* c); } TEST(utils, registry) { @@ -35,26 +33,24 @@ TEST(utils, registry) { CHECK_EQ(count, 2U); } -TEST(ElementwiseAdd, registry) { - InferShapedKernelRegistry registry; +TEST(ElementwiseAdd, launcher_registry) { + host_context::KernelRegistry registry; RegisterInferShapeLaunchers(®istry); ASSERT_EQ(registry.size(), 1UL); auto creator = registry.GetKernel("elementwise_add"); - auto infershape_launcher_handle = creator(); - // fake some tensors - tensor::DenseHostTensor a({2, 8}, GetDType()); - tensor::DenseHostTensor b({2, 8}, GetDType()); - tensor::DenseHostTensor c({2, 8}, GetDType()); + ::pten::CPUContext ctx{}; + ::pten::DenseTensor a{}; + ::pten::DenseTensor b{}; + ::pten::DenseTensor c{}; host_context::KernelFrameBuilder kernel_frame_builder; - kernel_frame_builder.AddArgument(new host_context::Value(0)); + kernel_frame_builder.AddArgument(new host_context::Value(std::move(ctx))); kernel_frame_builder.AddArgument(new host_context::Value(std::move(a))); kernel_frame_builder.AddArgument(new host_context::Value(std::move(b))); kernel_frame_builder.SetResults({new host_context::Value(std::move(c))}); - - infershape_launcher_handle->Invoke(&kernel_frame_builder); + creator(&kernel_frame_builder); } -} // namespace naive +} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/naive/infershaped/infershaped_kernel_launcher.cc b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.cc similarity index 74% rename from paddle/infrt/naive/infershaped/infershaped_kernel_launcher.cc rename to paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.cc index 6a2c4a51ec..80f8bae401 100644 --- a/paddle/infrt/naive/infershaped/infershaped_kernel_launcher.cc +++ b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.cc @@ -12,18 +12,19 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/infrt/naive/infershaped/infershaped_kernel_launcher.h" +#include "paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.h" namespace infrt { -namespace naive { +namespace kernel { void InferShapedKernelLauncher::CreateKernelFrameForInferShape( host_context::KernelFrame* frame) { for (host_context::Value* value : frame->GetValues(1, frame->GetNumElements() - 1)) { // TODO(Superjomn) To extend this. - if (value->is_type()) { - values.emplace_back(MetaTensor{&value->get()}); + if (value->is_type<::pten::DenseTensor>()) { + values.emplace_back( + ::pten::MetaTensor{&value->get<::pten::DenseTensor>()}); infershape_kernel_frame_builder.AddArgument(values.back().get()); } else { infershape_kernel_frame_builder.AddArgument(value); @@ -35,8 +36,9 @@ void InferShapedKernelLauncher::BuildInferShapeCache( const uint16_t num_inputs) { tensor_shape_cache.resize(num_inputs); for (uint16_t i = 0; i < num_inputs; i++) { - tensor_shape_cache[i] = - infershape_kernel_frame_builder.GetArgAt(i)->get().shape(); + tensor_shape_cache[i] = infershape_kernel_frame_builder.GetArgAt(i) + ->get<::pten::MetaTensor>() + .dims(); } } @@ -49,10 +51,11 @@ bool InferShapedKernelLauncher::IsShapeChanged( for (uint16_t i = 0; i < num_inputs && !changed; i++) { changed = changed || (tensor_shape_cache[i] != - infershape_kernel_frame_builder.GetArgAt(i).shape()); + infershape_kernel_frame_builder.GetArgAt<::pten::MetaTensor>(i) + .dims()); } return changed; } -} // namespace naive +} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/naive/infershaped/infershaped_kernel_launcher.h b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.h similarity index 90% rename from paddle/infrt/naive/infershaped/infershaped_kernel_launcher.h rename to paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.h index 890a779ed2..9348bf8d05 100644 --- a/paddle/infrt/naive/infershaped/infershaped_kernel_launcher.h +++ b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.h @@ -17,11 +17,9 @@ #include "paddle/infrt/host_context/kernel_frame.h" #include "paddle/infrt/host_context/value.h" -#include "paddle/infrt/naive/meta_tensor.h" -#include "paddle/infrt/tensor/dense_host_tensor.h" namespace infrt { -namespace naive { +namespace kernel { struct InferShapedKernelLauncher { virtual void Invoke(host_context::KernelFrame* frame) = 0; @@ -46,9 +44,9 @@ struct InferShapedKernelLauncher { // values to hold the TensorMeta. llvm::SmallVector values; - llvm::SmallVector tensor_shape_cache; + llvm::SmallVector<::pten::DDim, 3> tensor_shape_cache; host_context::KernelFrameBuilder infershape_kernel_frame_builder; }; -} // namespace naive +} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.cc b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.cc new file mode 100644 index 0000000000..23d4f919af --- /dev/null +++ b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.cc @@ -0,0 +1,36 @@ +// 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/kernel/pten/infershaped/infershaped_kernel_launchers.h" +#include "paddle/infrt/kernel/pten/infershaped/elementwise_add.h" + +namespace infrt { +namespace kernel { + +void RegisterInferShapeLaunchers(host_context::KernelRegistry* registry) { + registry->AddKernel( + "elementwise_add", + std::bind(&KernelLauncherFunc, + KernelLauncher(), + std::placeholders::_1)); +} + +} // namespace kernel +} // namespace infrt diff --git a/paddle/infrt/naive/infershaped/infershaped_kernel_launchers.h b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.h similarity index 79% rename from paddle/infrt/naive/infershaped/infershaped_kernel_launchers.h rename to paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.h index 3e83b690bb..ba25f06876 100644 --- a/paddle/infrt/naive/infershaped/infershaped_kernel_launchers.h +++ b/paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launchers.h @@ -14,12 +14,12 @@ #pragma once -namespace infrt { -namespace naive { +#include "paddle/infrt/host_context/kernel_registry.h" -struct InferShapedKernelRegistry; +namespace infrt { +namespace kernel { -void RegisterInferShapeLaunchers(InferShapedKernelRegistry* registry); +void RegisterInferShapeLaunchers(host_context::KernelRegistry* registry); -} // namespace naive +} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/naive/infershaped/infershaped_utils.h b/paddle/infrt/kernel/pten/infershaped/infershaped_utils.h similarity index 95% rename from paddle/infrt/naive/infershaped/infershaped_utils.h rename to paddle/infrt/kernel/pten/infershaped/infershaped_utils.h index 8155d87231..aa5e900b8b 100644 --- a/paddle/infrt/naive/infershaped/infershaped_utils.h +++ b/paddle/infrt/kernel/pten/infershaped/infershaped_utils.h @@ -18,10 +18,10 @@ #include "paddle/infrt/tensor/dense_host_tensor.h" namespace infrt { -namespace naive { +namespace kernel { namespace infershaped { -using KeyType = const tensor::DenseHostTensor&; +using KeyType = const ::pten::DenseTensor&; using CountType = uint8_t; constexpr CountType value(std::true_type) { return 1; } @@ -73,5 +73,5 @@ struct InferShapeHelper { static constexpr int count = infershaped::count(); }; -} // namespace naive +} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/kernel/pten/registry.cc b/paddle/infrt/kernel/pten/registry.cc new file mode 100644 index 0000000000..888992c47d --- /dev/null +++ b/paddle/infrt/kernel/pten/registry.cc @@ -0,0 +1,61 @@ +// 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/kernel/pten/registry.h" + +#include +#include + +#include "paddle/infrt/host_context/kernel_registry.h" +#include "paddle/infrt/host_context/kernel_utils.h" +#include "paddle/infrt/kernel/pten/allocator_kernels.h" +#include "paddle/infrt/kernel/pten/context_kernels.h" +#include "paddle/infrt/kernel/pten/dense_tensor_kernels.h" +#include "paddle/infrt/kernel/pten/infershaped/elementwise_add.h" +#include "paddle/pten/include/infermeta.h" +#include "paddle/pten/include/kernels.h" +#include "paddle/pten/kernels/matmul_kernel.h" + +using infrt::host_context::Attribute; + +namespace infrt { +namespace kernel { + +void RegisterPtenKernels(host_context::KernelRegistry* registry) { + registry->AddKernel("pten_dt.create_allocator.cpu", + INFRT_KERNEL(infrt::kernel::pten::CreateCpuAllocator)); + registry->AddKernel("pten_dt.create_context.cpu", + INFRT_KERNEL(infrt::kernel::pten::CreateCpuContext)); + registry->AddKernel( + "pten_dt.create_dense_tensor.cpu.f32.nchw", + INFRT_KERNEL(infrt::kernel::pten::CreateDenseTensorCpuF32Nchw)); + registry->AddKernel("pten_dt.fill_dense_tensor.f32", + INFRT_KERNEL(infrt::kernel::pten::FillDenseTensorF32)); + registry->AddKernel( + "pten.matmul.host.fp32", + std::bind(&kernel::KernelLauncherFunc< + decltype(&::pten::MatmulKernel), + &::pten::MatmulKernel, + decltype(&::pten::MatmulInferMeta), + &::pten::MatmulInferMeta>, + kernel::KernelLauncher< + decltype(&::pten::MatmulKernel), + &::pten::MatmulKernel, + decltype(&::pten::MatmulInferMeta), + &::pten::MatmulInferMeta>(), + std::placeholders::_1)); +} + +} // namespace kernel +} // namespace infrt diff --git a/paddle/infrt/kernel/pten_kernels.h b/paddle/infrt/kernel/pten/registry.h similarity index 100% rename from paddle/infrt/kernel/pten_kernels.h rename to paddle/infrt/kernel/pten/registry.h diff --git a/paddle/infrt/naive/CMakeLists.txt b/paddle/infrt/naive/CMakeLists.txt deleted file mode 100644 index c90c6e7ba7..0000000000 --- a/paddle/infrt/naive/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -cc_library(infrt_naive SRCS meta_tensor.cc - infershaped/infershaped_kernel_launcher.cc - infershaped/infershaped_registry.cc - infershaped/infershaped_kernel_launchers.cc - ) - -cc_test_tiny(test_infrt_infershape_launchers SRCS -infershaped/infershape_launchers_test.cc DEPS infrt) diff --git a/paddle/infrt/naive/infershaped/infershaped_kernel_launchers.cc b/paddle/infrt/naive/infershaped/infershaped_kernel_launchers.cc deleted file mode 100644 index e570b3521b..0000000000 --- a/paddle/infrt/naive/infershaped/infershaped_kernel_launchers.cc +++ /dev/null @@ -1,34 +0,0 @@ -// 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/naive/infershaped/infershaped_kernel_launchers.h" -#include "paddle/infrt/naive/infershaped/elementwise_add.h" -#include "paddle/infrt/naive/infershaped/infershaped_registry.h" - -namespace infrt { -namespace naive { - -using ElementwiseAddLauncher = - KernelLauncher; - -void RegisterInferShapeLaunchers(InferShapedKernelRegistry* registry) { - registry->AddKernel("elementwise_add", - INFERSHAPED_KERNEL_CREATOR(ElementwiseAddLauncher)); -} - -} // namespace naive -} // namespace infrt diff --git a/paddle/infrt/naive/infershaped/infershaped_registry.cc b/paddle/infrt/naive/infershaped/infershaped_registry.cc deleted file mode 100644 index 94218a9a6f..0000000000 --- a/paddle/infrt/naive/infershaped/infershaped_registry.cc +++ /dev/null @@ -1,55 +0,0 @@ -// 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/naive/infershaped/infershaped_registry.h" - -#include - -#include "paddle/infrt/naive/infershaped/infershaped_kernel_launcher.h" - -namespace infrt { -namespace naive { - -struct InferShapedKernelRegistry::Impl { - std::unordered_map data; -}; - -InferShapedKernelRegistry::InferShapedKernelRegistry() - : impl_(std::make_unique()) {} - -void InferShapedKernelRegistry::AddKernel( - const std::string& key, - InferShapedKernelRegistry::InferShapeLauncherCreator&& creator) { - CHECK(!impl_->data.count(key)) << "Item called " << key << " duplicates"; - impl_->data.emplace(key, std::move(creator)); -} - -const InferShapedKernelRegistry::InferShapeLauncherCreator& -InferShapedKernelRegistry::GetKernel(const std::string& key) const { - auto it = impl_->data.find(key); - CHECK(it != impl_->data.end()) << "No item called " << key << " exists"; - return it->second; -} - -size_t InferShapedKernelRegistry::size() const { return impl_->data.size(); } - -InferShapedKernelRegistry* GetInferShapeRegistry() { - static auto registry = std::make_unique(); - return registry.get(); -} - -InferShapedKernelRegistry::~InferShapedKernelRegistry() {} - -} // namespace naive -} // namespace infrt diff --git a/paddle/infrt/naive/infershaped/infershaped_registry.h b/paddle/infrt/naive/infershaped/infershaped_registry.h deleted file mode 100644 index e0e56a148f..0000000000 --- a/paddle/infrt/naive/infershaped/infershaped_registry.h +++ /dev/null @@ -1,56 +0,0 @@ -// 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 -#include -#include - -namespace infrt { -namespace naive { - -struct InferShapedKernelLauncher; - -class InferShapedKernelRegistry { - public: - using InferShapeLauncherHandle = std::unique_ptr; - using InferShapeLauncherCreator = std::function; - - InferShapedKernelRegistry(); - - void AddKernel(const std::string& key, InferShapeLauncherCreator&& creator); - - const InferShapeLauncherCreator& GetKernel(const std::string& key) const; - - size_t size() const; - - ~InferShapedKernelRegistry(); - - private: - struct Impl; - - std::unique_ptr impl_; -}; - -//! The global infershape registry. -InferShapedKernelRegistry* GetInferShapeRegistry(); - -} // namespace naive -} // namespace infrt - -#define INFERSHAPED_KERNEL_CREATOR(infershape_launcher_class_) \ - []() \ - -> ::infrt::naive::InferShapedKernelRegistry::InferShapeLauncherHandle { \ - return std::make_unique(); \ - } diff --git a/paddle/infrt/naive/meta_tensor.h b/paddle/infrt/naive/meta_tensor.h deleted file mode 100644 index 4b62f3021a..0000000000 --- a/paddle/infrt/naive/meta_tensor.h +++ /dev/null @@ -1,47 +0,0 @@ -// 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. - -// A naive implementation of MetaTensor -#pragma once -#include "paddle/infrt/common/common.h" - -namespace infrt { -namespace tensor { -struct DenseHostTensor; -struct TensorShape; -} // namespace tensor - -namespace naive { - -class MetaTensor { - public: - MetaTensor() = default; - explicit MetaTensor(tensor::DenseHostTensor* tensor) - : mutable_tensor_(tensor) {} - explicit MetaTensor(const tensor::DenseHostTensor* tensor) - : mutable_tensor_(&Reference(tensor)) {} - explicit MetaTensor(MetaTensor&& other) - : mutable_tensor_(other.mutable_tensor_) {} - explicit MetaTensor(const MetaTensor& other) - : mutable_tensor_(other.mutable_tensor_) {} - - const tensor::TensorShape& shape() const; - tensor::TensorShape* mutable_shape(); - - private: - tensor::DenseHostTensor* mutable_tensor_{}; -}; - -} // namespace naive -} // namespace infrt diff --git a/paddle/infrt/tests/dialect/pten/dense_tensor.mlir b/paddle/infrt/tests/dialect/pten/dense_tensor.mlir index 109fa2d6fa..88f5b289fd 100644 --- a/paddle/infrt/tests/dialect/pten/dense_tensor.mlir +++ b/paddle/infrt/tests/dialect/pten/dense_tensor.mlir @@ -1,10 +1,11 @@ // RUN: infrtopt %s | FileCheck %s -// CHECK-LABEL: basic_tensor +// CHECK-LABEL: @basic_tensor func @basic_tensor() { - %a = "pten_dt.create_uninit_tensor.f32" () { shape=[12:i64, 23:i64] } : () -> !infrt.tensor - %b = "pten_dt.create_inited_tensor.f32" () { shape=[2:i64, 2:i64], values=[0.1:f32, 0.2:f32, 0.3:f32, 0.4:f32] } : () -> !infrt.tensor - "pten_dt.fill_tensor_with_constant.f32" (%a) { value=0.1:f32 } : (!infrt.tensor) -> () + %a = "pten_dt.create_allocator.cpu" (): () -> !pten.CPU_allocator + %b = "pten_dt.create_context.cpu" (): () -> !pten.CPU_context + %c = "pten_dt.create_dense_tensor.cpu.f32.nchw" (%a) {dims=[1:i64], lod=[1:i64]}: (!pten.CPU_allocator) -> (!infrt.tensor) + // "pten_dt.fill_dense_tensor.f32" (%c) {value=[1.0:f32]} : (!infrt.tensor) -> () infrt.return } diff --git a/paddle/pten/backends/cpu/cpu_context.cc b/paddle/pten/backends/cpu/cpu_context.cc index 4029c286a5..5eb89c2dc6 100644 --- a/paddle/pten/backends/cpu/cpu_context.cc +++ b/paddle/pten/backends/cpu/cpu_context.cc @@ -58,6 +58,10 @@ CPUContext::CPUContext(const Place& place) CPUContext::~CPUContext() = default; +CPUContext::CPUContext(CPUContext&&) = default; + +CPUContext& CPUContext::operator=(CPUContext&&) = default; + void CPUContext::Init() { impl_->Init(); } Eigen::DefaultDevice* CPUContext::eigen_device() const { diff --git a/paddle/pten/backends/cpu/cpu_context.h b/paddle/pten/backends/cpu/cpu_context.h index dca87a786b..1e4109d3ee 100644 --- a/paddle/pten/backends/cpu/cpu_context.h +++ b/paddle/pten/backends/cpu/cpu_context.h @@ -27,6 +27,8 @@ namespace pten { class CPUContext : public DeviceContext { public: CPUContext(); + CPUContext(CPUContext&&); + CPUContext& operator=(CPUContext&&); explicit CPUContext(const Place&); virtual ~CPUContext(); Eigen::DefaultDevice* eigen_device() const; diff --git a/paddle/pten/core/device_context.cc b/paddle/pten/core/device_context.cc index 70d71b5c76..bc9d7fc7d2 100644 --- a/paddle/pten/core/device_context.cc +++ b/paddle/pten/core/device_context.cc @@ -149,6 +149,8 @@ DeviceContext::DeviceContext(DeviceContext&& other) { impl_ = std::move(other.impl_); } +DeviceContext& DeviceContext::operator=(DeviceContext&&) = default; + DeviceContext::~DeviceContext() = default; void DeviceContext::SetAllocator(const Allocator* allocator) { diff --git a/paddle/pten/core/device_context.h b/paddle/pten/core/device_context.h index d627f19b55..05753b531f 100644 --- a/paddle/pten/core/device_context.h +++ b/paddle/pten/core/device_context.h @@ -49,6 +49,11 @@ class DeviceContext { */ DeviceContext(DeviceContext&&); + /** + * @brief Move assign operator. + */ + DeviceContext& operator=(DeviceContext&&); + /** * @brief Default destruct. */ -- GitLab