diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index c8253effe8488946dfaa3c3bd4812c73d7f938d8..2486c54d5addc40fae2c019ab6b0db4d6121a290 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 0000000000000000000000000000000000000000..172a808afbb5bd30c9e68b5a1cd6c6719cac06b6 --- /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 0000000000000000000000000000000000000000..1f5efeb272cef01b12b78538709181c31acbfd46 --- /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 0fb268952d54f388e69f4422b4c33bcf373b3686..b4ed5cdc1d82fd4a32f8594dc41b6e32c3e52459 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 0000000000000000000000000000000000000000..a3a1609d9918aea754666b8ec0bcc467fad4d756 --- /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 24ac2d851fe866d87b41a27083b40608781b92b2..5fe259300d2aec32fade1141de2dbf8cef314687 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 040c8ec3d3695b5807a98f26e7a36b38a6ce93b9..528f0f919680d65dd9636b96686838b427459eff 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 ac23d44248982811f856152e81db3657e29433d4..ba87787dd7f7caa73a1387c687a96c44c52d26d0 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 d65969999f6ed0b4035d23d04b73b12ce9528642..a813f690efb0b3d36b7575d0889652f0868a2d85 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 7fca56343041c2827f0dce57ca98fb9158ef66f4..fd2aecb3e6c1e0432e333e5459e0082d2183c058 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 b0d70af5ef9f2a4a515bf2ab6446c5ae5868f441..62c907bc9159f4b3ee8e03878736fb30106c4616 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 1c5a57709263600d16b95773c7b060d41da6447c..e8b904efb74a1e620bd1bf6b35478b6adb9e92af 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 904e51f92838d9c5dc6cf19e7f07afeeb295d19e..f623e141512ce4777de6f8a9232db002a08facf0 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 b7ef5691e47604bc176d44c68642942607685b0a..402665119ac2dd93214b5b9733352846004c75b3 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 0000000000000000000000000000000000000000..65c10b0b15f8dc32b9b167cf24bc02189b58c084 --- /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 0000000000000000000000000000000000000000..d3ecbed15da9691514b3688006d547ae54c42db0 --- /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 2f7ee3a69e290a0e0f96303a99ae669b386cd15a..33127711193a227a6a565a8404714747566b0920 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 0000000000000000000000000000000000000000..0c5e53212113be02e3d57471be80bc1564f8f51f --- /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 0000000000000000000000000000000000000000..14a151d9d1d8e35cbe5477c62817382ae765f8e9 --- /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 0000000000000000000000000000000000000000..2db5f4a3c11794521fd137ffd86e99909700f12f --- /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 62e2db659ad4274e233a21004e317ffa2b138c62..f60525707cd77f5eedbc531b9cfb3ed08e5a58f0 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 ee044e38da03dd523e14ad4243e5ea40109e4e70..1d9d0106da539b4fd071b551dd693cdc6eaaf528 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 ba6fdbdd5783f580ca1a441dd18dbac9d5baa6e8..64b99110d94c7a5e9a25f175194ea2b11e98b5f0 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 6a2c4a51ecdb29b747618db9766cb4afad048f1a..80f8bae4018cbde3dc3a8d2c1331691041425327 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 890a779ed240322990604e5598c48a1db1500b28..9348bf8d05008b7a125e5f1ee55d16f3c49e6257 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 0000000000000000000000000000000000000000..23d4f919af0571d566d37b618711708d48b88365 --- /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 3e83b690bb8df6d931e6f0c52d3697535db42989..ba25f06876cca2af04f3a1893cd776833d649506 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 8155d87231a8f9d713a07a65a669543d7480ee0e..aa5e900b8b26a99aa0401deb61960bf3ec923e81 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 0000000000000000000000000000000000000000..888992c47d968666bf531a751b85a996a77e270d --- /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 c90c6e7ba7b88e25c4b5d5ce6579d013234a8a4f..0000000000000000000000000000000000000000 --- 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 e570b3521b795ac6b2e58d2ec229accb2611ecfb..0000000000000000000000000000000000000000 --- 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 94218a9a6f6a6aee5e27f6c85aee0b425d95acd2..0000000000000000000000000000000000000000 --- 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 e0e56a148fa3dbcb849e4956cc50e2763b400180..0000000000000000000000000000000000000000 --- 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 4b62f3021a3a614415de3ddae1770fd514b889e6..0000000000000000000000000000000000000000 --- 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 109fa2d6fa74163324005f7b31642353c293cecb..88f5b289fd9f843803fddf0cd98859839ef271de 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 4029c286a5b288bbd1613cd0050882fb143dae54..5eb89c2dc658d3787f0f179cf75c426e39112c24 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 dca87a786b961d1b3c5a0194801fc29f27aeaa47..1e4109d3eeb7f1c306baaf4f00a208f944c3abc3 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 70d71b5c767eae79ae9036a1c2b119ba0f053f62..bc9d7fc7d29b2cfa81814fc24118f77e4692f985 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 d627f19b55dbcb137b66d4e04b061129c6ba7d44..05753b531ff08e8600425aeddad5f80f8acaedcb 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. */