diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index 2a9a7aed480e76edbac4d5ba6d7bc3b8b2dc5006..cd52114713ba85cf5ced617a18fe901eb3c0cf86 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -77,6 +77,9 @@ bool AnalysisPredictor::Init( OptimizeInferenceProgram(); ctx_ = executor_->Prepare(*inference_program_, 0); + if (config_.use_mkldnn) { + executor_->EnableMKLDNN(*inference_program_); + } VLOG(5) << "to create variables"; PADDLE_ENFORCE(scope_.get()); diff --git a/paddle/fluid/inference/api/api_impl.cc b/paddle/fluid/inference/api/api_impl.cc index 6fe13ed027de403bdc21882c26225bcd4cc7e49a..c6cb09667e0e58962c073dbbb7affebf76cc185d 100644 --- a/paddle/fluid/inference/api/api_impl.cc +++ b/paddle/fluid/inference/api/api_impl.cc @@ -106,6 +106,9 @@ bool NativePaddlePredictor::Init( } ctx_ = executor_->Prepare(*inference_program_, 0); + if (config_.use_mkldnn) { + executor_->EnableMKLDNN(*inference_program_); + } executor_->CreateVariables(*inference_program_, sub_scope_ ? sub_scope_ : scope_.get(), 0); diff --git a/paddle/fluid/inference/api/paddle_inference_api.h b/paddle/fluid/inference/api/paddle_inference_api.h index 995da11e4a30eca72a91a53d3293aa8b033b012b..e8d51bb72cd08cb443b809d196c3e95f6050f5be 100644 --- a/paddle/fluid/inference/api/paddle_inference_api.h +++ b/paddle/fluid/inference/api/paddle_inference_api.h @@ -45,7 +45,7 @@ class PaddleBuf { PaddleBuf(void* data, size_t length) : data_(data), length_(length), memory_owned_{false} {} // Own memory. - PaddleBuf(size_t length) + explicit PaddleBuf(size_t length) : data_(new char[length]), length_(length), memory_owned_(true) {} // Resize to `length` bytes. void Resize(size_t length); @@ -121,6 +121,8 @@ struct NativeConfig : public PaddlePredictor::Config { bool use_gpu{false}; int device{0}; float fraction_of_gpu_memory{-1.f}; // Negative to notify initialization. + // MKLDNN related fields. + bool use_mkldnn{false}; // Specify the variable's name of each input. bool specify_input_name{false}; diff --git a/paddle/fluid/inference/tests/api/analyzer_vis_tester.cc b/paddle/fluid/inference/tests/api/analyzer_vis_tester.cc index 67bde723042f24326909dea1ed4305ede21dc824..135a81a85c2d86e1cb06c5b1adf02c23d91857f6 100644 --- a/paddle/fluid/inference/tests/api/analyzer_vis_tester.cc +++ b/paddle/fluid/inference/tests/api/analyzer_vis_tester.cc @@ -66,12 +66,13 @@ Record ProcessALine(const std::string &line) { * Use the native and analysis fluid engine to inference the demo. * ocr, mobilenet and se_resnext50 */ -void TestVisualPrediction() { +void TestVisualPrediction(bool use_mkldnn) { std::unique_ptr predictor; AnalysisConfig cfg; cfg.param_file = FLAGS_infer_model + "/__params__"; cfg.prog_file = FLAGS_infer_model + "/__model__"; cfg.use_gpu = false; + cfg.use_mkldnn = use_mkldnn; cfg.device = 0; cfg.enable_ir_optim = true; cfg.ir_passes.push_back("fc_gru_fuse_pass"); @@ -163,7 +164,10 @@ void TestVisualPrediction() { } } -TEST(Analyzer_vis, analysis) { TestVisualPrediction(); } +TEST(Analyzer_vis, analysis) { TestVisualPrediction(/*use_mkldnn*/ false); } +TEST(Analyzer_vis, analysis_mkldnn) { + TestVisualPrediction(/*use_mkldnn*/ true); +} } // namespace analysis } // namespace inference