From f8901f556f74277c4bdf2529ffd5adef804674a3 Mon Sep 17 00:00:00 2001 From: co63oc Date: Tue, 5 Aug 2025 14:21:28 +0800 Subject: [PATCH 1/2] Fix --- paddle/fluid/framework/details/build_strategy.cc | 2 +- .../framework/ir/onednn/onednn_placement_pass.cc | 6 +++--- .../framework/ir/onednn/onednn_placement_pass.h | 6 +++--- .../ir/onednn/onednn_placement_pass_tester.cc | 10 +++++----- paddle/fluid/inference/analysis/argument.h | 12 ++++++------ paddle/fluid/inference/analysis/ir_pass_manager.cc | 4 ++-- paddle/fluid/inference/api/analysis_predictor.cc | 14 +++++++------- paddle/fluid/inference/capi/pd_config.cc | 2 +- paddle/fluid/jit/engine/interpreter_engine.cc | 2 +- .../generator/get_expected_kernel_func.cc | 6 +++--- paddle/fluid/pybind/compiled_program.cc | 9 +++++++++ .../static/quantization/quant2_int8_onednn_pass.py | 2 +- 12 files changed, 42 insertions(+), 33 deletions(-) diff --git a/paddle/fluid/framework/details/build_strategy.cc b/paddle/fluid/framework/details/build_strategy.cc index 296989f7d612a2..3455922b3066eb 100755 --- a/paddle/fluid/framework/details/build_strategy.cc +++ b/paddle/fluid/framework/details/build_strategy.cc @@ -321,7 +321,7 @@ ir::Graph *BuildStrategy::Apply(ir::Graph *graph, continue; } } else if (pass->Type() == "onednn_placement_pass") { - pass->Set("mkldnn_enabled_op_types", + pass->Set("onednn_enabled_op_types", new std::unordered_set(onednn_enabled_op_types_)); } VLOG(1) << "Start Apply Pass " << pass->Type(); diff --git a/paddle/fluid/framework/ir/onednn/onednn_placement_pass.cc b/paddle/fluid/framework/ir/onednn/onednn_placement_pass.cc index 62748541683476..d1c73836ac90d9 100644 --- a/paddle/fluid/framework/ir/onednn/onednn_placement_pass.cc +++ b/paddle/fluid/framework/ir/onednn/onednn_placement_pass.cc @@ -64,7 +64,7 @@ inline bool FoundPhiOneDNNKernelWithCorrectDataType( return false; } -bool MKLDNNPlacementPass::IsSupport(const Node* op) const { +bool ONEDNNPlacementPass::IsSupport(const Node* op) const { if (FoundOneDNNKernelWithCorrectDataType(op) || FoundPhiOneDNNKernelWithCorrectDataType(op)) { // For interpolate ops, there's a little difference between Paddle and @@ -89,8 +89,8 @@ bool MKLDNNPlacementPass::IsSupport(const Node* op) const { } // namespace paddle::framework::ir -REGISTER_PASS(onednn_placement_pass, paddle::framework::ir::MKLDNNPlacementPass) - .RequirePassAttr("mkldnn_enabled_op_types"); +REGISTER_PASS(onednn_placement_pass, paddle::framework::ir::ONEDNNPlacementPass) + .RequirePassAttr("onednn_enabled_op_types"); REGISTER_PASS_CAPABILITY(onednn_placement_pass) .AddCombination( diff --git a/paddle/fluid/framework/ir/onednn/onednn_placement_pass.h b/paddle/fluid/framework/ir/onednn/onednn_placement_pass.h index 5fc1dbd24f18ef..b7e0e1d3383c69 100644 --- a/paddle/fluid/framework/ir/onednn/onednn_placement_pass.h +++ b/paddle/fluid/framework/ir/onednn/onednn_placement_pass.h @@ -26,17 +26,17 @@ namespace ir { /* * Specifies which operators should use MKLDNN. */ -class MKLDNNPlacementPass : public PlacementPassBase { +class ONEDNNPlacementPass : public PlacementPassBase { protected: bool IsSupport(const Node* op) const override; private: - const std::string GetPlacementName() const override { return "MKLDNN"; } + const std::string GetPlacementName() const override { return "ONEDNN"; } const std::string GetAttrName() const override { return "use_mkldnn"; } const std::unordered_set GetOpTypesList() const override { - return Get>("mkldnn_enabled_op_types"); + return Get>("onednn_enabled_op_types"); } }; diff --git a/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc b/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc index c8346dcbafd7a0..8df7b54bf415f4 100644 --- a/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc +++ b/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc @@ -133,7 +133,7 @@ class PlacementPassTest { auto pass = PassRegistry::Instance().Get("onednn_placement_pass"); - pass->Set("mkldnn_enabled_op_types", + pass->Set("onednn_enabled_op_types", new std::unordered_set(onednn_enabled_op_types)); graph.reset(pass->Apply(graph.release())); @@ -160,23 +160,23 @@ class PlacementPassTest { } }; -TEST(MKLDNNPlacementPass, enable_conv_relu) { +TEST(ONEDNNPlacementPass, enable_conv_relu) { // 2 conv (1 conv is always true) + 2 relu (1 relu is always true) + 0 pool PlacementPassTest().MainTest({"conv2d", "relu"}, 4); } -TEST(MKLDNNPlacementPass, enable_relu_pool) { +TEST(ONEDNNPlacementPass, enable_relu_pool) { // 1 conv (1 conv is always true) + 2 relu (1 relu is always true) + 1 pool PlacementPassTest().MainTest({"relu", "pool2d"}, 4); } -TEST(MKLDNNPlacementPass, enable_all) { +TEST(ONEDNNPlacementPass, enable_all) { // 2 conv (1 conv is always true) + 2 relu (1 relu is always true) + 1 pool + // 1 concat PlacementPassTest().MainTest({}, 6); } -TEST(MKLDNNPlacementPass, placement_name) { +TEST(ONEDNNPlacementPass, placement_name) { PlacementPassTest().PlacementNameTest(); } diff --git a/paddle/fluid/inference/analysis/argument.h b/paddle/fluid/inference/analysis/argument.h index 5e11ce0e3f47cb..6b2b38feebef02 100644 --- a/paddle/fluid/inference/analysis/argument.h +++ b/paddle/fluid/inference/analysis/argument.h @@ -193,12 +193,12 @@ struct Argument { // whether to mute all logs in inference. DECL_ARGUMENT_FIELD(disable_logs, DisableLogs, bool); - // Pass a set of op types to enable its mkldnn kernel - DECL_ARGUMENT_FIELD(mkldnn_enabled_op_types, - MKLDNNEnabledOpTypes, + // Pass a set of op types to enable its onednn kernel + DECL_ARGUMENT_FIELD(onednn_enabled_op_types, + ONEDNNEnabledOpTypes, std::unordered_set); - // The cache capacity of different input shapes for mkldnn. - DECL_ARGUMENT_FIELD(mkldnn_cache_capacity, MkldnnCacheCapacity, int); + // The cache capacity of different input shapes for onednn. + DECL_ARGUMENT_FIELD(mkldnn_cache_capacity, OnednnCacheCapacity, int); #ifdef PADDLE_WITH_DNNL // A set of op types to enable their quantized kernels @@ -219,7 +219,7 @@ struct Argument { Bfloat16EnabledOpTypes, std::unordered_set); - DECL_ARGUMENT_FIELD(use_onednn_int8, UseMkldnnInt8, bool); + DECL_ARGUMENT_FIELD(use_onednn_int8, UseOnednnInt8, bool); #endif // Passed from config. diff --git a/paddle/fluid/inference/analysis/ir_pass_manager.cc b/paddle/fluid/inference/analysis/ir_pass_manager.cc index 6048d8b4944477..c416926df5dfdd 100644 --- a/paddle/fluid/inference/analysis/ir_pass_manager.cc +++ b/paddle/fluid/inference/analysis/ir_pass_manager.cc @@ -131,9 +131,9 @@ void IRPassManager::CreatePasses(Argument *argument, pass->Set("optim_cache_dir", new std::string(std::move(optim_cache_dir))); pass_num++; } else if (pass_name == "onednn_placement_pass") { - pass->Set("mkldnn_enabled_op_types", + pass->Set("onednn_enabled_op_types", new std::unordered_set( - argument->mkldnn_enabled_op_types())); + argument->onednn_enabled_op_types())); } else if (pass_name == "cudnn_placement_pass") { pass->Set("cudnn_enabled_op_types", new std::unordered_set()); diff --git a/paddle/fluid/inference/api/analysis_predictor.cc b/paddle/fluid/inference/api/analysis_predictor.cc index e8e857dc3e1b86..755397bb3ccc14 100644 --- a/paddle/fluid/inference/api/analysis_predictor.cc +++ b/paddle/fluid/inference/api/analysis_predictor.cc @@ -1031,8 +1031,8 @@ void AnalysisPredictor::OptimizeInferencePirProgram() { } #endif #ifdef PADDLE_WITH_DNNL - } else if (config_.mkldnn_enabled()) { - // mkldnn + } else if (config_.onednn_enabled()) { + // onednn pir::IrContext *ctx = pir::IrContext::Instance(); ctx->GetOrRegisterDialect(); if (!config_.custom_pass_only_) { @@ -2100,9 +2100,9 @@ void AnalysisPredictor::PrepareArgument() { argument_->SetIpuCustomPatterns(config_.ipu_custom_patterns_); #endif - if (config_.mkldnn_enabled() && !config_.use_gpu()) { - LOG(INFO) << "MKLDNN is enabled"; - argument_->SetMKLDNNEnabledOpTypes(config_.onednn_enabled_op_types_); + if (config_.onednn_enabled() && !config_.use_gpu()) { + LOG(INFO) << "ONEDNN is enabled"; + argument_->SetONEDNNEnabledOpTypes(config_.onednn_enabled_op_types_); } if (config_.cinn_enabled()) { @@ -2115,7 +2115,7 @@ void AnalysisPredictor::PrepareArgument() { argument_->SetBfloat16EnabledOpTypes(config_.bfloat16_enabled_op_types_); } - if (config_.mkldnn_int8_enabled()) { + if (config_.onednn_int8_enabled()) { LOG(INFO) << "Int8 is enabled"; argument_->SetQuantizeEnabledOpTypes(config_.quantize_enabled_op_types_); argument_->SetQuantizeExcludedOpIds(config_.quantize_excluded_op_ids_); @@ -2296,7 +2296,7 @@ void AnalysisPredictor::OptimizeInferenceProgram() { #if defined(_WIN32) argument_->PartiallyRelease(); #else - if (config_.mkldnn_enabled() || + if (config_.onednn_enabled() || (config_.tensorrt_engine_enabled() && config_.tensorrt_precision_mode_ == AnalysisConfig::Precision::kInt8)) { // NOLINT diff --git a/paddle/fluid/inference/capi/pd_config.cc b/paddle/fluid/inference/capi/pd_config.cc index 96b0b49915d5d0..b19a33e5eadfd9 100644 --- a/paddle/fluid/inference/capi/pd_config.cc +++ b/paddle/fluid/inference/capi/pd_config.cc @@ -311,7 +311,7 @@ bool PD_OnednnEnabled(const PD_AnalysisConfig* config) { config, common::errors::InvalidArgument( "The pointer of analysis configuration shouldn't be nullptr")); - return config->config.mkldnn_enabled(); + return config->config.onednn_enabled(); } void PD_SetCpuMathLibraryNumThreads(PD_AnalysisConfig* config, diff --git a/paddle/fluid/jit/engine/interpreter_engine.cc b/paddle/fluid/jit/engine/interpreter_engine.cc index 0bba3ebd2e554b..d1f341b504c965 100644 --- a/paddle/fluid/jit/engine/interpreter_engine.cc +++ b/paddle/fluid/jit/engine/interpreter_engine.cc @@ -53,7 +53,7 @@ void InterpreterEngine::CreateInterpreterCore() { #ifdef PADDLE_WITH_DNNL auto onednn_pass = framework::ir::PassRegistry::Instance().Get("onednn_placement_pass"); - onednn_pass->Set("mkldnn_enabled_op_types", + onednn_pass->Set("onednn_enabled_op_types", new std::unordered_set({})); onednn_pass->Apply(&graph); #endif diff --git a/paddle/fluid/operators/generator/get_expected_kernel_func.cc b/paddle/fluid/operators/generator/get_expected_kernel_func.cc index 944d9f6bfca1e2..4089772637abf0 100644 --- a/paddle/fluid/operators/generator/get_expected_kernel_func.cc +++ b/paddle/fluid/operators/generator/get_expected_kernel_func.cc @@ -61,7 +61,7 @@ static bool ReduceOpHasOptimizedOneDNNKernel( } // only poolop -bool CanMKLDNNSupportPool(const framework::ExecutionContext& ctx) { +bool CanONEDNNSupportPool(const framework::ExecutionContext& ctx) { if (ctx.Attr("adaptive") == false) return true; // oneDNN is supporting only unchangeable in size pool window auto src_tz = common::vectorize(ctx.Input("X")->dims()); @@ -181,7 +181,7 @@ phi::KernelKey GetPoolExpectedKernelType( auto data_type = op_ptr->OperatorWithKernel::IndicateVarDataType(ctx, "X"); // NOTE(jiahongyu): Below codes originally enclosed by PADDLE_WITH_DNNL - op_ptr->SetDnnFallback(!CanMKLDNNSupportPool(ctx)); + op_ptr->SetDnnFallback(!CanONEDNNSupportPool(ctx)); // NOTE(jiahongyu) END: Above codes originally enclosed by PADDLE_WITH_DNNL return phi::KernelKey(data_type, ctx.GetPlace()); @@ -194,7 +194,7 @@ phi::KernelKey GetPoolDoubleGradExpectedKernelType( op_ptr->OperatorWithKernel::IndicateVarDataType(ctx, "grad_x@GRAD"); // NOTE(jiahongyu): Below codes originally enclosed by PADDLE_WITH_DNNL - op_ptr->SetDnnFallback(!CanMKLDNNSupportPool(ctx)); + op_ptr->SetDnnFallback(!CanONEDNNSupportPool(ctx)); // NOTE(jiahongyu) END: Above codes originally enclosed by PADDLE_WITH_DNNL return phi::KernelKey(data_type, ctx.GetPlace()); diff --git a/paddle/fluid/pybind/compiled_program.cc b/paddle/fluid/pybind/compiled_program.cc index 563ff805815fc7..18f36a2b2efe33 100644 --- a/paddle/fluid/pybind/compiled_program.cc +++ b/paddle/fluid/pybind/compiled_program.cc @@ -824,6 +824,15 @@ void BindCompiledProgram(pybind11::module &m) { // NOLINT const std::unordered_set &onednn_enabled_op_types) { self.onednn_enabled_op_types_ = onednn_enabled_op_types; }) + .def_property( + "onednn_enabled_op_types", + [](const BuildStrategy &self) { + return self.onednn_enabled_op_types_; + }, + [](BuildStrategy &self, + const std::unordered_set &onednn_enabled_op_types) { + self.onednn_enabled_op_types_ = onednn_enabled_op_types; + }) .def_property( "allow_cuda_graph_capture", [](const BuildStrategy &self) { diff --git a/python/paddle/static/quantization/quant2_int8_onednn_pass.py b/python/paddle/static/quantization/quant2_int8_onednn_pass.py index 72b505d44a6054..5efc8c1a588028 100644 --- a/python/paddle/static/quantization/quant2_int8_onednn_pass.py +++ b/python/paddle/static/quantization/quant2_int8_onednn_pass.py @@ -429,7 +429,7 @@ def _optimize_fp32_graph(self, graph): graph = self._update_activations(graph) graph = self._remove_ctrl_vars(graph) graph = self._apply_pass( - graph, 'onednn_placement_pass', ['mkldnn_enabled_op_types'], [set()] + graph, 'onednn_placement_pass', ['onednn_enabled_op_types'], [set()] ) # remove dropout ops graph = self._apply_pass(graph, 'simplify_with_basic_ops_pass') From 0dddf5a624c143f5d20d98fb0cce5a00d9b3e2d7 Mon Sep 17 00:00:00 2001 From: co63oc Date: Wed, 6 Aug 2025 09:24:37 +0800 Subject: [PATCH 2/2] Fix --- .../framework/ir/onednn/onednn_placement_pass_tester.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc b/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc index 8df7b54bf415f4..81f4ca871d550a 100644 --- a/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc +++ b/paddle/fluid/framework/ir/onednn/onednn_placement_pass_tester.cc @@ -143,8 +143,10 @@ class PlacementPassTest { for (auto* node : graph->Nodes()) { if (node->IsOp()) { auto* op = node->Op(); - if (op->HasAttr("use_mkldnn") && - PADDLE_GET_CONST(bool, op->GetAttr("use_mkldnn"))) { + if ((op->HasAttr("use_mkldnn") && + PADDLE_GET_CONST(bool, op->GetAttr("use_mkldnn"))) || + (op->HasAttr("use_onednn") && + PADDLE_GET_CONST(bool, op->GetAttr("use_onednn")))) { ++use_onednn_true_count; } } @@ -156,7 +158,7 @@ class PlacementPassTest { void PlacementNameTest() { auto pass = PassRegistry::Instance().Get("onednn_placement_pass"); EXPECT_EQ(static_cast(pass.get())->GetPlacementName(), - "MKLDNN"); + "ONEDNN"); } };