2222#include < unordered_map>
2323#include < vector>
2424
25+ #include " mlir/Pass/PassManager.h"
26+ #include " paddle/infrt/backends/host/phi_allocator.h"
2527#include " paddle/infrt/common/global.h"
2628#include " paddle/infrt/dialect/dense_tensor.h"
2729#include " paddle/infrt/dialect/infrt/ir/infrt_dialect.h"
30+ #include " paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h"
2831#include " paddle/infrt/dialect/mlir_loader.h"
32+ #include " paddle/infrt/dialect/phi/ir/phi_base.h"
33+ #include " paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h"
2934#include " paddle/infrt/host_context/core_runtime.h"
3035#include " paddle/infrt/host_context/kernel_registry.h"
3136#include " paddle/infrt/host_context/mlir_function_executable.h"
3237#include " paddle/infrt/host_context/mlir_to_runtime_translate.h"
3338#include " paddle/infrt/host_context/op_executable.h"
39+ #include " paddle/infrt/host_context/paddle_mlir.h"
3440#include " paddle/infrt/host_context/value.h"
3541#include " paddle/infrt/kernel/basic_kernels.h"
3642#include " paddle/infrt/kernel/control_flow_kernels.h"
43+ #include " paddle/infrt/kernel/phi/dense_tensor_kernels.h"
44+ #include " paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h"
45+ #include " paddle/infrt/kernel/phi/registry.h"
3746#include " paddle/infrt/kernel/tensor_kernels.h"
3847#include " paddle/infrt/kernel/tensor_shape_kernels.h"
3948#include " paddle/infrt/kernel/test_kernels.h"
@@ -84,12 +93,12 @@ class PredictExecutor : public MlirToRuntimeTranslator {
8493
8594 PredictExecutor (mlir::ModuleOp module ,
8695 KernelRegistry* registry,
87- TensorMap* map)
96+ ::infrt::phi::DenseTensorMap&& map)
8897 : MlirToRuntimeTranslator(module , &core_runtime),
8998 core_runtime (registry),
9099 registry_(registry) {
91100 CHECK (registry_);
92- Init (map);
101+ Init (std::move ( map) );
93102 }
94103
95104 void Run () {
@@ -100,18 +109,18 @@ class PredictExecutor : public MlirToRuntimeTranslator {
100109
101110 int GetInputNum () { return inputs_.size (); }
102111
103- DenseHostTensor * GetInput (int i) { return inputs_[i]; }
112+ ::phi::DenseTensor * GetInput (int i) { return inputs_[i]; }
104113
105114 int GetOutputNum () { return outputs_.size (); }
106115
107- DenseHostTensor * GetOutput (int i) { return outputs_[i]; }
116+ ::phi::DenseTensor * GetOutput (int i) { return outputs_[i]; }
108117
109118 private:
110- void Init (TensorMap* map) {
119+ void Init (::infrt::phi::DenseTensorMap&& map) {
111120 EmitFunctions ();
112121 llvm::Optional<mlir::FuncOp> predict_func_ = llvm::None;
113122 for (auto func_op : impl_->module .getOps <mlir::FuncOp>()) {
114- if (func_op.getName ().str () != " predict " ) continue ;
123+ if (func_op.getName ().str () != " main_graph " ) continue ;
115124 predict_func_ = func_op;
116125 break ;
117126 }
@@ -125,30 +134,43 @@ class PredictExecutor : public MlirToRuntimeTranslator {
125134 new MlirFunctionExecutable (predict_func, registry_, impl_->func_defs );
126135
127136 // process parammeters
137+ VLOG (3 ) << " Arguments num of predict func: "
138+ << predict_func.getNumArguments ();
128139 for (size_t i = 0 ; i < predict_func.getNumArguments (); ++i) {
129140 auto arg = predict_func.getArgument (i);
130141 auto type = arg.getType ();
131142 // this param is TensorMap
132- if (type.isa <infrt::DenseHostTensorMapType >()) {
133- auto * value = new host_context::Value (std::move (* map));
143+ if (type.isa <:: infrt::phi::DenseTensorMapType >()) {
144+ auto * value = new host_context::Value (std::move (map));
134145 arguments_.push_back (value);
135146 AddValue (predict_func.getArgument (i), value);
136- } else {
147+ } else if (type. isa <::infrt::DenseTensorType>()) {
137148 // this param is an input Tensor
138- auto dht = DenseHostTensor ();
149+ auto dht = :: phi::DenseTensor ();
139150 auto * value = new host_context::Value (std::move (dht));
140151 arguments_.push_back (value);
141- inputs_.push_back (&(value->get <DenseHostTensor>()));
152+ inputs_.push_back (&(value->get <::phi::DenseTensor>()));
153+ } else {
154+ llvm_unreachable (" The input type has not been supported by predictor." );
142155 }
143156 }
144157
145158 // process results
146159 auto & last_op = predict_func.front ().back ();
147160 if (last_op.getName ().getStringRef () == " infrt.return" ) {
148161 for (size_t i = 0 ; i < last_op.getNumOperands (); ++i) {
149- auto * value = AddValue (mlir::Value (last_op.getOperand (i)));
150- results_.push_back (ValueRef (value));
151- outputs_.push_back (&(value->get <DenseHostTensor>()));
162+ auto operand = last_op.getOperand (i);
163+ if (operand.getType ().isa <::infrt::DenseTensorType>()) {
164+ auto r = impl_->value_map .try_emplace (
165+ operand, ValueRef (new host_context::Value (::phi::DenseTensor ())));
166+ CHECK (r.second ) << " Duplicate add mlir value ["
167+ << DumpToString (operand) << " ]" ;
168+ auto * value = r.first ->second .get ();
169+ results_.push_back (ValueRef (value));
170+ outputs_.push_back (&(value->get <::phi::DenseTensor>()));
171+ } else {
172+ llvm_unreachable (" infrt.return only supports DenseTensor now." );
173+ }
152174 }
153175 }
154176 }
@@ -166,22 +188,22 @@ class PredictExecutor : public MlirToRuntimeTranslator {
166188 private:
167189 KernelRegistry* registry_{};
168190 MlirFunctionExecutable* function_executable_;
169- llvm::SmallVector<DenseHostTensor *, 1 > inputs_;
191+ llvm::SmallVector<::phi::DenseTensor *, 1 > inputs_;
170192 llvm::SmallVector<host_context::Value*, 2 > arguments_;
171- llvm::SmallVector<DenseHostTensor *, 1 > outputs_;
193+ llvm::SmallVector<::phi::DenseTensor *, 1 > outputs_;
172194 llvm::SmallVector<ValueRef, 1 > results_;
173195};
174196
175- std::shared_ptr <InfRtPredictor> CreateInfRtPredictor (
197+ std::unique_ptr <InfRtPredictor> CreateInfRtPredictor (
176198 const InfRtConfig& config) {
177- auto x = std::make_shared <InfRtPredictor>();
199+ auto x = std::make_unique <InfRtPredictor>();
178200 x->Init (config);
179201 return x;
180202}
181203
182204struct InfRtPredictor ::Impl {
183- mlir::OwningModuleRef module_ref;
184205 std::unique_ptr<PredictExecutor> executor;
206+ MLIRModelGenImpl module_gen_;
185207};
186208
187209InfRtPredictor::InfRtPredictor () : impl_(new Impl) {}
@@ -190,8 +212,7 @@ InfRtPredictor::~InfRtPredictor() {}
190212void InfRtPredictor::Run () { impl_->executor ->Run (); }
191213
192214int InfRtPredictor::Init (const InfRtConfig& config) {
193- mlir::MLIRContext* context = infrt::Global::getMLIRContext ();
194- auto module_ref = dialect::LoadMlirFile (config.mlir_path (), context);
215+ mlir::MLIRContext* context = ::infrt::Global::getMLIRContext ();
195216
196217 KernelRegistry* registry = new KernelRegistry ();
197218
@@ -200,8 +221,32 @@ int InfRtPredictor::Init(const InfRtConfig& config) {
200221 kernel::RegisterTensorShapeKernels (registry);
201222 kernel::RegisterTensorKernels (registry);
202223 kernel::RegisterControlFlowKernels (registry);
203-
204- impl_->module_ref = std::move (module_ref);
224+ #ifdef INFRT_WITH_PHI
225+ kernel::RegisterPhiKernels (registry);
226+ kernel::RegisterInferShapeLaunchers (registry);
227+ #if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT)
228+ kernel::RegisterTrtKernels (registry);
229+ #endif // INFRT_WITH_GPU && INFRT_WITH_TRT
230+ #endif
231+
232+ auto module_op = impl_->module_gen_ .ImportPaddleModel (config.model_dir (),
233+ config.param_dir ());
234+
235+ context->loadAllAvailableDialects ();
236+ ::mlir::PassManager pm (context);
237+ ::mlir::OpPassManager& phi_pass_manager = pm.nest <::mlir::FuncOp>();
238+ std::vector<::infrt::Place> valid_places = {{::infrt::TargetType::CPU,
239+ ::infrt::PrecisionType::FLOAT32,
240+ ::infrt::LayoutType::NCHW}};
241+ phi_pass_manager.addPass (::infrt::createPhiOpCvtPass (valid_places));
242+ phi_pass_manager.addPass (::infrt::createInfrtOpFusePass ());
243+ if (mlir::failed (pm.run (module_op))) {
244+ std::cout << " \n pass failed!\n " << std::endl;
245+ return 4 ;
246+ }
247+ #ifndef NDEBUG
248+ module_op.dump ();
249+ #endif // NDEBUG
205250
206251 // load extra shared library
207252 for (const std::string& lib_path : config.shared_libs ()) {
@@ -222,23 +267,24 @@ int InfRtPredictor::Init(const InfRtConfig& config) {
222267 }
223268
224269 // Load params
225- TensorMap* tensor_map = LoadParams (config.model_dir ());
270+ auto tensor_map = ::infrt::kernel::phi::LoadCombinedParameters (
271+ config.model_dir (), config.param_dir ());
226272
227273 // Create PredictExecutor
228274 impl_->executor .reset (
229- new PredictExecutor (impl_-> module_ref . get () , registry, tensor_map));
275+ new PredictExecutor (module_op , registry, std::move ( tensor_map) ));
230276 return 0 ;
231277}
232278
233279int InfRtPredictor::GetInputNum () { return impl_->executor ->GetInputNum (); }
234280
235- DenseHostTensor * InfRtPredictor::GetInput (int i) {
281+ ::phi::DenseTensor * InfRtPredictor::GetInput (int i) {
236282 return impl_->executor ->GetInput (i);
237283}
238284
239285int InfRtPredictor::GetOutputNum () { return impl_->executor ->GetOutputNum (); }
240286
241- DenseHostTensor * InfRtPredictor::GetOutput (int i) {
287+ ::phi::DenseTensor * InfRtPredictor::GetOutput (int i) {
242288 return impl_->executor ->GetOutput (i);
243289}
244290
0 commit comments