From ab9152b73d92182c8cf091576a22045bba199446 Mon Sep 17 00:00:00 2001 From: Superjomn Date: Mon, 21 Feb 2022 02:07:31 +0000 Subject: [PATCH] fix fake kernel execute bug --- paddle/infrt/host_context/kernel_frame.cc | 2 + paddle/infrt/host_context/kernel_frame.h | 15 +++++--- paddle/infrt/host_context/kernel_utils.h | 2 +- .../host_context/mlir_to_runtime_translate.cc | 38 +++++++++---------- paddle/infrt/host_context/value.h | 4 +- .../pten/infershaped/pten_kernel_launcher.h | 9 ++++- paddle/infrt/support/variant.h | 2 +- .../tests/dialect/pten/dense_tensor.mlir | 5 ++- 8 files changed, 45 insertions(+), 32 deletions(-) diff --git a/paddle/infrt/host_context/kernel_frame.cc b/paddle/infrt/host_context/kernel_frame.cc index 24acb54c88bc8..24be2bbfdddac 100644 --- a/paddle/infrt/host_context/kernel_frame.cc +++ b/paddle/infrt/host_context/kernel_frame.cc @@ -46,6 +46,8 @@ std::string KernelFrame::DumpArgTypes() const { ss << "pten::CPUContext,"; } else if (value->is_type()) { ss << "none,"; + } else if (value->is_type()) { + ss << "CpuPtenContext,"; } else { ss << "unk,"; } diff --git a/paddle/infrt/host_context/kernel_frame.h b/paddle/infrt/host_context/kernel_frame.h index 879ee38fddc04..7cef05c9c26d5 100644 --- a/paddle/infrt/host_context/kernel_frame.h +++ b/paddle/infrt/host_context/kernel_frame.h @@ -44,6 +44,11 @@ class KernelFrame { return value_or_attrs_[index]->template get_or_default(); } + Value* GetElementAt(int index) { + CHECK_LT(static_cast(index), GetNumElements()); + return value_or_attrs_[index]; + } + // Get number of elements, either input, attributes or results. size_t GetNumElements() const { return value_or_attrs_.size(); } @@ -79,11 +84,11 @@ class KernelFrame { CHECK_EQ(num_results_, -1) << "Must call SetNumResults after calling AddAttribute"; value_or_attrs_.emplace_back(v); - if (num_attrs_ = -1) { - num_attrs_ = 1; - } else { - num_attrs_++; - } + if (num_attrs_ == -1) num_attrs_ = 0; + num_attrs_++; + + CHECK_EQ(value_or_attrs_.size(), + static_cast(num_arguments_ + num_attrs_)); } template diff --git a/paddle/infrt/host_context/kernel_utils.h b/paddle/infrt/host_context/kernel_utils.h index 31d411006d237..7973325ea9fa6 100644 --- a/paddle/infrt/host_context/kernel_utils.h +++ b/paddle/infrt/host_context/kernel_utils.h @@ -265,7 +265,7 @@ struct KernelImpl { static_assert(const_idx == 0, "Arguments and results should appear before attributes."); - auto* value = frame->GetArgAt(in_idx); + auto* value = frame->GetElementAt(in_idx); auto&& arg = value->get(); KernelCallHelper< diff --git a/paddle/infrt/host_context/mlir_to_runtime_translate.cc b/paddle/infrt/host_context/mlir_to_runtime_translate.cc index 0853a4eaff117..dc792a8f9a50e 100644 --- a/paddle/infrt/host_context/mlir_to_runtime_translate.cc +++ b/paddle/infrt/host_context/mlir_to_runtime_translate.cc @@ -274,25 +274,6 @@ bool MlirToRuntimeTranslator::EmitGeneralOp(mlir::Operation* op) { << GetValue(operand) << " vs " << arg_value; } - // process results - llvm::SmallVector res_values; - for (int i = 0, e = op->getNumResults(); i < e; i++) { - auto res = op->getResult(i); - res_values.push_back(AddValue(res)); - - VLOG(3) << "* op mlir res: " << DumpToString(res) << " " << GetValue(res); - } - impl_->cur_op->SetResults(res_values); - -#ifdef INFRT_DEBUG - { - VLOG(3) << "check result"; - for (int i = 0; i < impl_->cur_op->frame().GetNumResults(); i++) { - VLOG(3) << "+ res value: " << impl_->cur_op->frame().GetResults()[i]; - } - } -#endif - // process attributes auto attrs = op->getAttrs(); @@ -325,6 +306,25 @@ bool MlirToRuntimeTranslator::EmitGeneralOp(mlir::Operation* op) { } } + // process results + llvm::SmallVector res_values; + for (int i = 0, e = op->getNumResults(); i < e; i++) { + auto res = op->getResult(i); + res_values.push_back(AddValue(res)); + + VLOG(3) << "* op mlir res: " << DumpToString(res) << " " << GetValue(res); + } + impl_->cur_op->SetResults(res_values); + +#ifdef INFRT_DEBUG + { + VLOG(3) << "check result"; + for (int i = 0; i < impl_->cur_op->frame().GetNumResults(); i++) { + VLOG(3) << "+ res value: " << impl_->cur_op->frame().GetResults()[i]; + } + } +#endif + // process regions, we treat regions as attribute. auto num_regions = op->getNumRegions(); if (num_regions > 0) { diff --git a/paddle/infrt/host_context/value.h b/paddle/infrt/host_context/value.h index 5f5cd0ed081a9..015577b4094f7 100644 --- a/paddle/infrt/host_context/value.h +++ b/paddle/infrt/host_context/value.h @@ -127,8 +127,8 @@ class Value : public common::Object { template T& get() { - LOG(INFO) << data.index(); - CHECK(data.template is()); + CHECK(data.template is()) << "typeid: " << data.index() + << " != " << ValueVariantType::IndexOf; return data.get(); } diff --git a/paddle/infrt/kernel/pten/infershaped/pten_kernel_launcher.h b/paddle/infrt/kernel/pten/infershaped/pten_kernel_launcher.h index ffa377a4b9bbf..ab01e2d6227c5 100644 --- a/paddle/infrt/kernel/pten/infershaped/pten_kernel_launcher.h +++ b/paddle/infrt/kernel/pten/infershaped/pten_kernel_launcher.h @@ -15,6 +15,9 @@ #include +#include + +#include "paddle/infrt/backends/host/pten_context.h" #include "paddle/infrt/host_context/kernel_utils.h" #include "paddle/infrt/kernel/pten/infershaped/infershaped_kernel_launcher.h" #include "paddle/infrt/kernel/pten/infershaped/infershaped_utils.h" @@ -27,11 +30,13 @@ static void FakePtenInferShape(const ::pten::MetaTensor& a, bool arg_0, ::pten::MetaTensor* c) {} -static void FakePtenKernel(const ::pten::CPUContext& /*Context*/, +static void FakePtenKernel(const backends::CpuPtenContext& /*Context*/, const ::pten::DenseTensor& a, const ::pten::DenseTensor& b, bool arg_0, - ::pten::DenseTensor* c) {} + ::pten::DenseTensor* c) { + std::cout << "@FakePtenKernel@" << std::endl; +} template static constexpr size_t IndexOf = TupleIndexOf::value; + private: static constexpr size_t kStorageSize = std::max({sizeof(Ts)...}); static constexpr size_t kAlignment = std::max({alignof(Ts)...}); diff --git a/paddle/infrt/tests/dialect/pten/dense_tensor.mlir b/paddle/infrt/tests/dialect/pten/dense_tensor.mlir index ec449c60936d7..c089b5f3c2be4 100644 --- a/paddle/infrt/tests/dialect/pten/dense_tensor.mlir +++ b/paddle/infrt/tests/dialect/pten/dense_tensor.mlir @@ -1,11 +1,12 @@ // RUN: infrtexec %s | FileCheck %s -// CHECK-LABEL: @basic_tensor -func @basic_tensor() { +// CHECK-LABEL: @fake_pten_kernel_execute +func @fake_pten_kernel_execute() { %allocator = "pten_dt.create_allocator.cpu" (): () -> !pten.CPU_allocator %ctx = "pten_dt.create_context.cpu" (): () -> !pten.CPU_context %t = "pten_dt.create_dense_tensor.cpu.f32.nchw" (%allocator) {dims=[1:i64], lod=[1:i64]}: (!pten.CPU_allocator) -> (!infrt.tensor) + // CHECK: @FakePtenKernel@ %d = "pten_dt.fake_pten_kernel" (%ctx, %t, %t) {transpose_x=false} : (!pten.CPU_context, !infrt.tensor, !infrt.tensor) -> (!infrt.tensor) infrt.return }