diff --git a/paddle/infrt/backends/host/phi_context.h b/paddle/infrt/backends/host/phi_context.h index 9d0e3bc4fbb3158147283c1992cf1fee70c9b90d..5713fdbbaf82b2ea2190d2ee1b1dc5d944f2c262 100644 --- a/paddle/infrt/backends/host/phi_context.h +++ b/paddle/infrt/backends/host/phi_context.h @@ -11,6 +11,7 @@ limitations under the License. */ #pragma once +#include "paddle/infrt/backends/host/phi_allocator.h" #include "paddle/phi/backends/cpu/cpu_context.h" namespace infrt { @@ -20,6 +21,14 @@ class CpuPhiContext : public phi::CPUContext { public: using Base = phi::CPUContext; using phi::CPUContext::SetEigenDevice; + + CpuPhiContext() { + Init(); + SetAllocator(alloc_.get()); + } + + private: + std::unique_ptr alloc_{std::make_unique()}; }; } // namespace backends diff --git a/paddle/infrt/dialect/phi/CMakeLists.txt b/paddle/infrt/dialect/phi/CMakeLists.txt index a2677a946cb7e8377b780fd79794ee84dec8eb3c..4e73a533d99a79168b3e68b88d917f48ec811444 100644 --- a/paddle/infrt/dialect/phi/CMakeLists.txt +++ b/paddle/infrt/dialect/phi/CMakeLists.txt @@ -10,3 +10,6 @@ target_link_libraries(phi-ir-exec infrt) add_executable(phi-exec phi_exec.cc) target_link_libraries(phi-exec infrt) + +gather_srcs(infrt_src SRCS + data_type.cc) diff --git a/paddle/infrt/dialect/phi/data_type.cc b/paddle/infrt/dialect/phi/data_type.cc new file mode 100644 index 0000000000000000000000000000000000000000..5da7ec8831258e52dd157ff444ffcd6e7930e1bb --- /dev/null +++ b/paddle/infrt/dialect/phi/data_type.cc @@ -0,0 +1,125 @@ +// 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/dialect/phi/data_type.h" + +namespace infrt { + +phi::Backend cvtTarget2Phi(TargetType target) { + switch (target) { + case TargetType::CPU: + return phi::Backend::CPU; + case TargetType::GPU: + return phi::Backend::GPU; + default: + return phi::Backend::UNDEFINED; + } +} + +TargetType cvtTargetFromPhi(phi::Backend backend) { + switch (backend) { + case phi::Backend::CPU: + return TargetType::CPU; + case phi::Backend::GPU: + return TargetType::GPU; + default: + return TargetType::UNK; + } +} + +phi::DataType cvtPrecision2Phi(PrecisionType precision) { +#define CONVERT_PRECISION_TO_PHI(Precision) \ + case PrecisionType::Precision: \ + return phi::DataType::Precision; + + switch (precision) { + CONVERT_PRECISION_TO_PHI(FLOAT32) + CONVERT_PRECISION_TO_PHI(FLOAT16) + CONVERT_PRECISION_TO_PHI(FLOAT64) + CONVERT_PRECISION_TO_PHI(UINT8) + CONVERT_PRECISION_TO_PHI(INT8) + CONVERT_PRECISION_TO_PHI(INT16) + CONVERT_PRECISION_TO_PHI(INT32) + CONVERT_PRECISION_TO_PHI(INT64) + CONVERT_PRECISION_TO_PHI(COMPLEX64) + CONVERT_PRECISION_TO_PHI(COMPLEX128) + CONVERT_PRECISION_TO_PHI(BOOL) + default: + return phi::DataType::UNDEFINED; + } +#undef CONVERT_PRECISION_TO_PHI +} + +PrecisionType cvtPrecisionFromPhi(phi::DataType datatype) { +#define CONVERT_PRECISION_FROM_PHI(Precision) \ + case phi::DataType::Precision: \ + return PrecisionType::Precision; + + switch (datatype) { + CONVERT_PRECISION_FROM_PHI(FLOAT32) + CONVERT_PRECISION_FROM_PHI(FLOAT16) + CONVERT_PRECISION_FROM_PHI(FLOAT64) + CONVERT_PRECISION_FROM_PHI(UINT8) + CONVERT_PRECISION_FROM_PHI(INT8) + CONVERT_PRECISION_FROM_PHI(INT16) + CONVERT_PRECISION_FROM_PHI(INT32) + CONVERT_PRECISION_FROM_PHI(INT64) + CONVERT_PRECISION_FROM_PHI(COMPLEX64) + CONVERT_PRECISION_FROM_PHI(COMPLEX128) + CONVERT_PRECISION_FROM_PHI(BOOL) + default: + return PrecisionType::UNK; + } +#undef CONVERT_PRECISION_FROM_PHI +} + +phi::DataLayout cvtLayout2Phi(LayoutType layout) { + switch (layout) { + case LayoutType::NCHW: + return phi::DataLayout::NCHW; + case LayoutType::NHWC: + return phi::DataLayout::NHWC; + case LayoutType::ANY: + return phi::DataLayout::ANY; + default: + return phi::DataLayout::UNDEFINED; + } +} + +LayoutType cvtLayoutFromPhi(phi::DataLayout layout) { + switch (layout) { + case phi::DataLayout::NCHW: + return LayoutType::NCHW; + case phi::DataLayout::NHWC: + return LayoutType::NHWC; + case phi::DataLayout::ANY: + return LayoutType::ANY; + default: + return LayoutType::UNK; + } +} + +phi::KernelKey cvtPlace2Phi(const Place& place) { + return phi::KernelKey(cvtTarget2Phi(place.target), + cvtLayout2Phi(place.layout), + cvtPrecision2Phi(place.precision)); +} + +Place cvtPlaceFromPhi(phi::TensorArgDef tensor_arg) { + return Place(cvtTargetFromPhi(tensor_arg.backend), + cvtPrecisionFromPhi(tensor_arg.dtype), + cvtLayoutFromPhi(tensor_arg.layout)); +} + +} // namespace infrt diff --git a/paddle/infrt/kernel/phi/allocator_kernels.h b/paddle/infrt/dialect/phi/data_type.h similarity index 50% rename from paddle/infrt/kernel/phi/allocator_kernels.h rename to paddle/infrt/dialect/phi/data_type.h index d10382f5e6014c2b04dab65c8439d99e4563aaef..b618ef3861303334b697382f11bfa4fdb4a35c7a 100644 --- a/paddle/infrt/kernel/phi/allocator_kernels.h +++ b/paddle/infrt/dialect/phi/data_type.h @@ -14,15 +14,25 @@ #pragma once -#include "paddle/infrt/backends/host/phi_allocator.h" -#include "paddle/phi/core/dense_tensor.h" +#include "paddle/infrt/dialect/infrt/common_type.h" +#include "paddle/phi/common/backend.h" +#include "paddle/phi/common/data_type.h" +#include "paddle/phi/common/layout.h" +#include "paddle/phi/common/place.h" +#include "paddle/phi/core/kernel_factory.h" namespace infrt { -namespace kernel { -namespace phi { -backends::CpuPhiAllocator CreateCpuAllocator(); +phi::Backend cvtTarget2Phi(TargetType target); +TargetType cvtTargetFromPhi(phi::Backend backend); + +phi::DataType cvtPrecision2Phi(PrecisionType precision); +PrecisionType cvtPrecisionFromPhi(phi::DataType datatype); + +phi::DataLayout cvtLayout2Phi(LayoutType layout); +LayoutType cvtLayoutFromPhi(phi::DataLayout layout); + +phi::KernelKey cvtPlace2Phi(const Place& place); +Place cvtPlaceFromPhi(phi::TensorArgDef tensor_arg); -} // namespace phi -} // namespace kernel } // namespace infrt diff --git a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td b/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td index 3399c408d9b5a0ff207c6947d2d3b3b4d8ca0a96..21c4669b645fb6c7622fb01ae1c7bacaee0f5ca2 100644 --- a/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td +++ b/paddle/infrt/dialect/phi/ir/infrt_phi_tensor.td @@ -18,12 +18,13 @@ def PHI_DenseTensorDialect : Dialect { } // PHI DenseTensor related Op. -class PDT_Op traits = []> : Op { -} +class PDT_Op traits = []> : Op {} -class CreateDenseTensorOp - : PDT_Op<"create_dense_tensor." # place # "." # dtype # "." # layout, [NoSideEffect]> { - let arguments = (ins Allocator:$allocator, I64ArrayAttr:$dims, I64ArrayAttr:$lod); +class CreateDenseTensorOp + : PDT_Op<"create_dense_tensor", [NoSideEffect]> { + let arguments = (ins Context:$context, I64ArrayAttr:$dims, + LayoutAttr:$layout, I64ArrayAttr:$lod, PrecisionAttr:$precision); let results = (outs DenseTensor:$output); } @@ -44,23 +45,16 @@ class PrintDenseTensorOp: let assemblyFormat = "`(` $input `:` type($input) `)` attr-dict"; } -class CreateCPUAllocatorOp - : PDT_Op<"create_allocator." # "cpu", [NoSideEffect]> { +class CreateContextOp + : PDT_Op<"create_context." # target, [NoSideEffect]> { let arguments = (ins); - let results = (outs Allocator:$output); -} - -class CreateCPUContextOp - : PDT_Op<"create_context." # "cpu", [NoSideEffect]> { - let arguments = (ins Allocator:$input); let results = (outs Context:$output); } -def PDT_CreateDenseTensorOp_cpu_f32_nchw : CreateDenseTensorOp<"cpu", "f32", "nchw">; +def PDT_CreateDenseTensorOp : CreateDenseTensorOp; def PDT_FillDenseTensorOp_f32 : FillDenseTensorOp; -def PDT_CreateAllocatorOp_cpu : CreateCPUAllocatorOp; -def PDT_CreateContextOp_cpu : CreateCPUContextOp; -def PDT_PrintDenseTensor_cpu : PrintDenseTensorOp; +def PDT_CreateCPUContextOp : CreateContextOp<"cpu">; +def PDT_PrintDenseTensor : PrintDenseTensorOp; def FakeKernelOp : PDT_Op<"fake_phi_kernel"> { let arguments = (ins Context:$dev_ctx, DenseTensor:$x, DenseTensor:$y, BoolAttr:$transpose_x, BoolAttr:$transpose_y); diff --git a/paddle/infrt/dialect/phi/pass/kernel_op_desc.cc b/paddle/infrt/dialect/phi/pass/kernel_op_desc.cc index 12a6cfcc3e4a810d19b5023cf97a6d739d50fb1f..d1763897b4a1320179134ede14fe404aee4a6a76 100644 --- a/paddle/infrt/dialect/phi/pass/kernel_op_desc.cc +++ b/paddle/infrt/dialect/phi/pass/kernel_op_desc.cc @@ -14,119 +14,10 @@ #include "paddle/infrt/dialect/phi/pass/kernel_op_desc.h" #include -#include "paddle/phi/core/kernel_factory.h" -#include "paddle/phi/core/kernel_registry.h" +#include "paddle/infrt/dialect/phi/data_type.h" #include "paddle/phi/kernels/declarations.h" namespace infrt { -namespace { -phi::Backend cvtTarget2Phi(TargetType target) { - switch (target) { - case TargetType::CPU: - return phi::Backend::CPU; - case TargetType::GPU: - return phi::Backend::GPU; - default: - return phi::Backend::UNDEFINED; - } -} - -TargetType cvtTargetFromPhi(phi::Backend backend) { - switch (backend) { - case phi::Backend::CPU: - return TargetType::CPU; - case phi::Backend::GPU: - return TargetType::GPU; - default: - return TargetType::UNK; - } -} - -phi::DataType cvtPrecision2Phi(PrecisionType precision) { -#define CONVERT_PRECISION_TO_PHI(Precision) \ - case PrecisionType::Precision: \ - return phi::DataType::Precision; - - switch (precision) { - CONVERT_PRECISION_TO_PHI(FLOAT32) - CONVERT_PRECISION_TO_PHI(FLOAT16) - CONVERT_PRECISION_TO_PHI(FLOAT64) - CONVERT_PRECISION_TO_PHI(UINT8) - CONVERT_PRECISION_TO_PHI(INT8) - CONVERT_PRECISION_TO_PHI(INT16) - CONVERT_PRECISION_TO_PHI(INT32) - CONVERT_PRECISION_TO_PHI(INT64) - CONVERT_PRECISION_TO_PHI(COMPLEX64) - CONVERT_PRECISION_TO_PHI(COMPLEX128) - CONVERT_PRECISION_TO_PHI(BOOL) - default: - return phi::DataType::UNDEFINED; - } -#undef CONVERT_PRECISION_TO_PHI -} - -PrecisionType cvtPrecisionFromPhi(phi::DataType datatype) { -#define CONVERT_PRECISION_FROM_PHI(Precision) \ - case phi::DataType::Precision: \ - return PrecisionType::Precision; - - switch (datatype) { - CONVERT_PRECISION_FROM_PHI(FLOAT32) - CONVERT_PRECISION_FROM_PHI(FLOAT16) - CONVERT_PRECISION_FROM_PHI(FLOAT64) - CONVERT_PRECISION_FROM_PHI(UINT8) - CONVERT_PRECISION_FROM_PHI(INT8) - CONVERT_PRECISION_FROM_PHI(INT16) - CONVERT_PRECISION_FROM_PHI(INT32) - CONVERT_PRECISION_FROM_PHI(INT64) - CONVERT_PRECISION_FROM_PHI(COMPLEX64) - CONVERT_PRECISION_FROM_PHI(COMPLEX128) - CONVERT_PRECISION_FROM_PHI(BOOL) - default: - return PrecisionType::UNK; - } -#undef CONVERT_PRECISION_FROM_PHI -} - -phi::DataLayout cvtLayout2Phi(LayoutType layout) { - switch (layout) { - case LayoutType::NCHW: - return phi::DataLayout::NCHW; - case LayoutType::NHWC: - return phi::DataLayout::NHWC; - case LayoutType::ANY: - return phi::DataLayout::ANY; - default: - return phi::DataLayout::UNDEFINED; - } -} - -LayoutType cvtLayoutFromPhi(phi::DataLayout layout) { - switch (layout) { - case phi::DataLayout::NCHW: - return LayoutType::NCHW; - case phi::DataLayout::NHWC: - return LayoutType::NHWC; - case phi::DataLayout::ANY: - return LayoutType::ANY; - default: - return LayoutType::UNK; - } -} - -phi::KernelKey cvtPlace2Phi(const Place& place) { - return phi::KernelKey(cvtTarget2Phi(place.target), - cvtLayout2Phi(place.layout), - cvtPrecision2Phi(place.precision)); -} - -Place cvtPlaceFromPhi(phi::TensorArgDef tensor_arg) { - return Place(cvtTargetFromPhi(tensor_arg.backend), - cvtPrecisionFromPhi(tensor_arg.dtype), - cvtLayoutFromPhi(tensor_arg.layout)); -} - -} // namespace std::string getPhiTargetPrefix(TargetType target) { switch (target) { diff --git a/paddle/infrt/dialect/phi/pass/phi_op_cvt_pass.cc b/paddle/infrt/dialect/phi/pass/phi_op_cvt_pass.cc index 4347ec19e816660e99352902fdff3eb36d6a28ee..fb00a3de3fc0c82dce2489c0f412c64118e3101e 100644 --- a/paddle/infrt/dialect/phi/pass/phi_op_cvt_pass.cc +++ b/paddle/infrt/dialect/phi/pass/phi_op_cvt_pass.cc @@ -135,20 +135,12 @@ void phiOpCvtPass::diapatchStage() { phi_context.end()) { switch (phi_kernel_desc.kernelType.target) { case TargetType::CPU: { - auto alloctor_value = - builder - .create( - kernel_op.getLoc(), - phi::AllocatorType::get(kernel_op.getContext(), - TargetType::CPU)) - .output(); auto context_value = builder - .create( + .create( kernel_op.getLoc(), phi::ContextType::get(kernel_op.getContext(), - TargetType::CPU), - alloctor_value) + TargetType::CPU)) .output(); phi_context[TargetType::CPU] = context_value; } break; diff --git a/paddle/infrt/host_context/mlir_to_runtime_translate.cc b/paddle/infrt/host_context/mlir_to_runtime_translate.cc index 17e6f7cb563d25186f9a76de8fe67af2ddb90e7b..a901c323ec03a418a32eee3cb8ea17708e38bdb9 100644 --- a/paddle/infrt/host_context/mlir_to_runtime_translate.cc +++ b/paddle/infrt/host_context/mlir_to_runtime_translate.cc @@ -173,6 +173,36 @@ boost::optional MlirToRuntimeTranslator::EmitAttribute( return boost::none; } +template <> +boost::optional<::infrt::TargetType> MlirToRuntimeTranslator::EmitAttribute( + const mlir::Attribute& attr) { + if (!attr.isa<::infrt::TargetAttr>()) return boost::none; + if (attr.isa<::infrt::TargetAttr>()) { + return attr.cast<::infrt::TargetAttr>().getTarget(); + } + return boost::none; +} + +template <> +boost::optional<::infrt::LayoutType> MlirToRuntimeTranslator::EmitAttribute( + const mlir::Attribute& attr) { + if (!attr.isa<::infrt::LayoutAttr>()) return boost::none; + if (attr.isa<::infrt::LayoutAttr>()) { + return attr.cast<::infrt::LayoutAttr>().getLayout(); + } + return boost::none; +} + +template <> +boost::optional<::infrt::PrecisionType> MlirToRuntimeTranslator::EmitAttribute( + const mlir::Attribute& attr) { + if (!attr.isa<::infrt::PrecisionAttr>()) return boost::none; + if (attr.isa<::infrt::PrecisionAttr>()) { + return attr.cast<::infrt::PrecisionAttr>().getPrecision(); + } + return boost::none; +} + template <> boost::optional MlirToRuntimeTranslator::EmitAttribute( const mlir::Attribute& attr) { @@ -292,6 +322,13 @@ bool MlirToRuntimeTranslator::EmitGeneralOp(mlir::Operation* op) { impl_->cur_op->AppendAttribute(new Value(std::move(*v))); } else if (auto v = EmitAttribute(attr.getValue())) { impl_->cur_op->AppendAttribute(new Value(*v)); + } else if (auto v = EmitAttribute<::infrt::TargetType>(attr.getValue())) { + impl_->cur_op->AppendAttribute(new Value(*v)); + } else if (auto v = + EmitAttribute<::infrt::PrecisionType>(attr.getValue())) { + impl_->cur_op->AppendAttribute(new Value(*v)); + } else if (auto v = EmitAttribute<::infrt::LayoutType>(attr.getValue())) { + impl_->cur_op->AppendAttribute(new Value(*v)); } else if (auto v = EmitAttribute>(attr.getValue())) { impl_->cur_op->AppendAttribute(new Value(std::move(*v))); } else if (auto v = EmitAttribute>(attr.getValue())) { diff --git a/paddle/infrt/host_context/value.cc b/paddle/infrt/host_context/value.cc index 3f40490557290fcc34a188882c4d4d251f4ba16e..abf0b8a9577993bc9430eaa20b065de157202a34 100644 --- a/paddle/infrt/host_context/value.cc +++ b/paddle/infrt/host_context/value.cc @@ -24,14 +24,6 @@ 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(backends::CpuPhiContext&& val) - : Shared(new Value(std::move(val))) {} -ValueRef::ValueRef(::phi::CPUContext&& val) - : Shared(new Value(std::move(val))) {} -ValueRef::ValueRef(::phi::DenseTensor&& val) - : Shared(new Value(std::move(val))) {} -ValueRef::ValueRef(::phi::MetaTensor&& val) - : Shared(new Value(std::move(val))) {} const char* Value::type_info() const { return __type_info__; } diff --git a/paddle/infrt/host_context/value.h b/paddle/infrt/host_context/value.h index 0ae482349cd07eafda1de5f634ed31a0c7310973..86df3508cf813628b4a8ba8412ce93d6b1dfc5a2 100644 --- a/paddle/infrt/host_context/value.h +++ b/paddle/infrt/host_context/value.h @@ -22,6 +22,7 @@ #include "paddle/infrt/common/object.h" #include "paddle/infrt/common/shared.h" +#include "paddle/infrt/dialect/infrt/common_type.h" #include "paddle/infrt/host_context/function.h" #include "paddle/infrt/support/variant.h" #include "paddle/infrt/tensor/dense_host_tensor.h" @@ -64,10 +65,12 @@ using ValueVariantType = tensor::DenseHostTensor, MlirFunctionExecutable*, tensor::TensorMap, + ::infrt::PrecisionType, + ::infrt::LayoutType, + ::infrt::TargetType, #ifdef INFRT_WITH_PHI ::phi::MetaTensor, ::phi::DenseTensor, - backends::CpuPhiAllocator, backends::CpuPhiContext, ::phi::CPUContext, std::vector, @@ -101,6 +104,9 @@ class Value : public common::Object { explicit Value(float x) : data(x) {} explicit Value(double x) : data(x) {} explicit Value(bool x) : data(x) {} + explicit Value(::infrt::TargetType x) : data(x) {} + explicit Value(::infrt::LayoutType x) : data(x) {} + explicit Value(::infrt::PrecisionType x) : data(x) {} explicit Value(std::string x) : data(x) {} explicit Value(tensor::TensorMap&& x) : data(x) {} explicit Value(std::vector&& x) : data(x) {} @@ -112,11 +118,10 @@ class Value : public common::Object { explicit Value(tensor::DenseHostTensor&& x) : data(std::move(x)) {} explicit Value(MlirFunctionExecutable* x) : data(x) {} #ifdef INFRT_WITH_PHI - explicit Value(backends::CpuPhiContext&& x) : data(std::move(x)) {} explicit Value(::phi::CPUContext&& x) : data(std::move(x)) {} + explicit Value(backends::CpuPhiContext&& x) : data(std::move(x)) {} explicit Value(::phi::DenseTensor&& x) : data(std::move(x)) {} explicit Value(::phi::MetaTensor&& x) : data(std::move(x)) {} - explicit Value(backends::CpuPhiAllocator&& x) : data(std::move(x)) {} #endif template @@ -179,10 +184,6 @@ class ValueRef : common::Shared { explicit ValueRef(float val); explicit ValueRef(double val); explicit ValueRef(bool val); - explicit ValueRef(::phi::MetaTensor&& val); - explicit ValueRef(backends::CpuPhiContext&& x); - explicit ValueRef(::phi::CPUContext&& x); - explicit ValueRef(::phi::DenseTensor&& x); using common::Shared::get; using common::Shared::Reset; diff --git a/paddle/infrt/kernel/phi/CMakeLists.txt b/paddle/infrt/kernel/phi/CMakeLists.txt index 7055c0c06d5905fa738d8df72c7110fdd82a30d2..15882d23743b020c289269ef09ae5e05999201a8 100644 --- a/paddle/infrt/kernel/phi/CMakeLists.txt +++ b/paddle/infrt/kernel/phi/CMakeLists.txt @@ -8,7 +8,6 @@ gather_srcs(infrt_src SRCS registry.cc dense_tensor_kernels.cc context_kernels.cc - allocator_kernels.cc ) set(infrt_register_phi_kernels_gen_source_file ${CMAKE_SOURCE_DIR}/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.cc) diff --git a/paddle/infrt/kernel/phi/allocator_kernels.cc b/paddle/infrt/kernel/phi/allocator_kernels.cc deleted file mode 100644 index eba12e688b4ae2cf9bdd4fa46bb479be882b02fc..0000000000000000000000000000000000000000 --- a/paddle/infrt/kernel/phi/allocator_kernels.cc +++ /dev/null @@ -1,25 +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/kernel/phi/allocator_kernels.h" - -namespace infrt { -namespace kernel { -namespace phi { - -backends::CpuPhiAllocator CreateCpuAllocator() { return {}; } - -} // namespace phi -} // namespace kernel -} // namespace infrt diff --git a/paddle/infrt/kernel/phi/context_kernels.cc b/paddle/infrt/kernel/phi/context_kernels.cc index 3caaf1788e3f8ada1fdbf7a69b2bc435b89a8eeb..39ef172fadef9e0f6317dec192c251c6a1df6828 100644 --- a/paddle/infrt/kernel/phi/context_kernels.cc +++ b/paddle/infrt/kernel/phi/context_kernels.cc @@ -18,12 +18,11 @@ namespace infrt { namespace kernel { namespace phi { -::phi::CPUContext CreateCpuContext( - infrt::backends::CpuPhiAllocator* allocator) { - ::phi::CPUContext context; - context.SetAllocator(allocator); - context.Init(); - return context; +::phi::CPUContext CreateCPUContext() { + ::phi::CPUContext ctx{}; + ctx.Init(); + ctx.SetAllocator(new backends::CpuPhiAllocator{}); + return ctx; } } // namespace phi diff --git a/paddle/infrt/kernel/phi/context_kernels.h b/paddle/infrt/kernel/phi/context_kernels.h index 7f1e7ef6cd356ce2c2e1c3cb3378d990b59cde36..3e9580b91da5724b42c72224847e45715f47dbb7 100644 --- a/paddle/infrt/kernel/phi/context_kernels.h +++ b/paddle/infrt/kernel/phi/context_kernels.h @@ -16,13 +16,14 @@ #include "paddle/infrt/backends/host/phi_allocator.h" #include "paddle/infrt/backends/host/phi_context.h" +#include "paddle/infrt/host_context/kernel_utils.h" #include "paddle/phi/core/dense_tensor.h" namespace infrt { namespace kernel { namespace phi { -::phi::CPUContext CreateCpuContext(::infrt::backends::CpuPhiAllocator*); +::phi::CPUContext CreateCPUContext(); } // namespace phi } // namespace kernel diff --git a/paddle/infrt/kernel/phi/dense_tensor_kernels.cc b/paddle/infrt/kernel/phi/dense_tensor_kernels.cc index 871336e8762e87577486f3422ed4de06d3cc03ba..e89ee7cfe5d6f51b3206aecc6ca283e06c0e5561 100644 --- a/paddle/infrt/kernel/phi/dense_tensor_kernels.cc +++ b/paddle/infrt/kernel/phi/dense_tensor_kernels.cc @@ -13,20 +13,25 @@ // limitations under the License. #include "paddle/infrt/kernel/phi/dense_tensor_kernels.h" -#include +#include "paddle/infrt/dialect/phi/data_type.h" +#include "paddle/infrt/kernel/phi/context_kernels.h" + namespace infrt { namespace kernel { namespace phi { -::phi::DenseTensor CreateDenseTensorCpuF32Nchw( - backends::CpuPhiAllocator* allocator, +::phi::DenseTensor CreateDenseTensor( + const ::phi::CPUContext& context, host_context::Attribute> dims, - host_context::Attribute> lod) { - return ::phi::DenseTensor(allocator, - ::phi::DenseTensorMeta(::phi::DataType::FLOAT32, - ::phi::make_ddim(dims.get()), - ::phi::DataLayout::NCHW, - {})); + host_context::Attribute<::infrt::LayoutType> layout, + host_context::Attribute> lod, + host_context::Attribute<::infrt::PrecisionType> precision) { + return ::phi::DenseTensor( + const_cast<::phi::Allocator*>(&context.GetAllocator()), + ::phi::DenseTensorMeta(cvtPrecision2Phi(precision.get()), + ::phi::make_ddim(dims.get()), + cvtLayout2Phi(layout.get()), + {})); } void FillDenseTensorF32(::phi::DenseTensor* dense_tensor, diff --git a/paddle/infrt/kernel/phi/dense_tensor_kernels.h b/paddle/infrt/kernel/phi/dense_tensor_kernels.h index 920c0b1c8af427e2ecbfef25ab56f96c8a19daf1..187e5c64511e83556bec50f4368ae7cbe89dda90 100644 --- a/paddle/infrt/kernel/phi/dense_tensor_kernels.h +++ b/paddle/infrt/kernel/phi/dense_tensor_kernels.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/infrt/backends/host/phi_allocator.h" +#include "paddle/infrt/dialect/infrt/common_type.h" #include "paddle/infrt/host_context/kernel_utils.h" #include "paddle/phi/core/dense_tensor.h" @@ -22,10 +23,12 @@ namespace infrt { namespace kernel { namespace phi { -::phi::DenseTensor CreateDenseTensorCpuF32Nchw( - backends::CpuPhiAllocator* allocator, +::phi::DenseTensor CreateDenseTensor( + const ::phi::CPUContext& context, host_context::Attribute> dims, - host_context::Attribute> lod); + host_context::Attribute<::infrt::LayoutType> layout, + host_context::Attribute> lod, + host_context::Attribute<::infrt::PrecisionType> precision); void FillDenseTensorF32(::phi::DenseTensor* dense_tensor, host_context::Attribute> values); diff --git a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc b/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc index 165f7f7c94377f8b9c1f9c240ee1418cab922cdc..75e3ebbf00ca54ed3fb2d0ca22bb7819300d0b2b 100644 --- a/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc +++ b/paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launcher.cc @@ -24,7 +24,8 @@ void InferShapedKernelLauncher::CreateKernelFrameForInferShape( frame->GetValues(1, frame->GetNumElements() - 1)) { // TODO(Superjomn) To extend this. if (value->is_type<::phi::DenseTensor>()) { - values.emplace_back(::phi::MetaTensor{&value->get<::phi::DenseTensor>()}); + values.emplace_back(new host_context::Value{ + ::phi::MetaTensor{&value->get<::phi::DenseTensor>()}}); infershape_kernel_frame_builder.AddArgument(values.back().get()); } else { infershape_kernel_frame_builder.AddArgument(value); diff --git a/paddle/infrt/kernel/phi/registry.cc b/paddle/infrt/kernel/phi/registry.cc index cb09275c170d81332b0bc4bd71fa51e987545317..90570484179d1e555f86c55ea0e8ac4f9bc83c53 100644 --- a/paddle/infrt/kernel/phi/registry.cc +++ b/paddle/infrt/kernel/phi/registry.cc @@ -19,7 +19,6 @@ #include "paddle/infrt/host_context/kernel_registry.h" #include "paddle/infrt/host_context/kernel_utils.h" -#include "paddle/infrt/kernel/phi/allocator_kernels.h" #include "paddle/infrt/kernel/phi/context_kernels.h" #include "paddle/infrt/kernel/phi/dense_tensor_kernels.h" #include "paddle/infrt/kernel/phi/infershaped/phi_kernel_launcher.h" @@ -33,13 +32,10 @@ namespace infrt { namespace kernel { void RegisterPhiKernels(host_context::KernelRegistry* registry) { - registry->AddKernel("phi_dt.create_allocator.cpu", - INFRT_KERNEL(infrt::kernel::phi::CreateCpuAllocator)); registry->AddKernel("phi_dt.create_context.cpu", - INFRT_KERNEL(infrt::kernel::phi::CreateCpuContext)); - registry->AddKernel( - "phi_dt.create_dense_tensor.cpu.f32.nchw", - INFRT_KERNEL(infrt::kernel::phi::CreateDenseTensorCpuF32Nchw)); + INFRT_KERNEL(infrt::kernel::phi::CreateCPUContext)); + registry->AddKernel("phi_dt.create_dense_tensor", + INFRT_KERNEL(infrt::kernel::phi::CreateDenseTensor)); registry->AddKernel("phi_dt.fill_dense_tensor.f32", INFRT_KERNEL(infrt::kernel::phi::FillDenseTensorF32)); registry->AddKernel("phi_dt.print_tensor", diff --git a/paddle/infrt/tests/dialect/phi/dense_tensor.mlir b/paddle/infrt/tests/dialect/phi/dense_tensor.mlir index b2e1cc52be68292f4d068d99e6545a9d570e8385..e8f09f07c82c4003e23a54c7275f576f7916f853 100644 --- a/paddle/infrt/tests/dialect/phi/dense_tensor.mlir +++ b/paddle/infrt/tests/dialect/phi/dense_tensor.mlir @@ -2,9 +2,10 @@ // CHECK-LABEL: @sign_any_float32_execute func @sign_any_float32_execute() { - %allocator = "phi_dt.create_allocator.cpu" (): () -> !phi.allocator - %ctx = "phi_dt.create_context.cpu" (%allocator): (!phi.allocator) -> !phi.context - %t = "phi_dt.create_dense_tensor.cpu.f32.nchw" (%allocator) {dims=[1:i64], lod=[1:i64]}: (!phi.allocator) -> (!infrt.dense_tensor) + %ctx = "phi_dt.create_context.cpu" (): () -> !phi.context + %t = "phi_dt.create_dense_tensor" (%ctx) { + precision=#infrt.precision, + layout=#infrt.layout, lod=[1:i64], dims=[1:i64]}: (!phi.context) -> (!infrt.dense_tensor) "phi_dt.fill_dense_tensor.f32"(%t) {value=[3.8:f32]} : (!infrt.dense_tensor) -> () %e = "phi_cpu.sign.float32.any"(%ctx, %t) : (!phi.context, !infrt.dense_tensor) -> (!infrt.dense_tensor)