@@ -1349,7 +1349,8 @@ common::Status InferenceSession::TransformGraph(onnxruntime::Graph& graph, bool
13491349 ORT_RETURN_IF_ERROR_SESSIONID_ (apply_transformer_once (ensure_unique_dq_for_node_unit, *session_logger_, graph));
13501350 }
13511351
1352- // apply execution provider independent level 1 graph optimizations.
1352+ // apply execution provider independent level 0 and 1 graph optimizations.
1353+ ORT_RETURN_IF_ERROR_SESSIONID_ (graph_transformer_mgr_.ApplyTransformers (graph, TransformerLevel::Default, *session_logger_));
13531354 ORT_RETURN_IF_ERROR_SESSIONID_ (graph_transformer_mgr_.ApplyTransformers (graph, TransformerLevel::Level1, *session_logger_));
13541355
13551356 // if saving model to ORT format we only assign nodes a custom EP can handle and don't compile them.
@@ -3645,34 +3646,50 @@ common::Status InferenceSession::AddPredefinedTransformers(
36453646 RecordRuntimeOptimizationProducedNodeOpSchemaFn record_runtime_optimization_produced_op_schema_fn,
36463647 const logging::Logger& logger) const {
36473648 const auto & cpu_ep = *execution_providers_.Get (onnxruntime::kCpuExecutionProvider );
3648- for (int i = static_cast <int >(TransformerLevel::Level1 ); i <= static_cast <int >(TransformerLevel::MaxLevel); i++) {
3649+ for (int i = static_cast <int >(TransformerLevel::Default ); i <= static_cast <int >(TransformerLevel::MaxLevel); i++) {
36493650 TransformerLevel level = static_cast <TransformerLevel>(i);
3650- if (graph_optimization_level >= level) {
3651- // Generate and register transformers for level
3652- auto transformers_to_register = [&]() {
3653- const bool use_full_build_optimizations =
3654- level == TransformerLevel::Level1 ||
3655- minimal_build_optimization_handling == MinimalBuildOptimizationHandling::ApplyFullBuildOptimizations;
3656-
3657- if (use_full_build_optimizations) {
3651+ std::function<onnxruntime::InlinedVector<std::unique_ptr<GraphTransformer>>()> transformers_to_register;
3652+
3653+ // Enable free dimension override even when the graph optimization level is 0.
3654+ // If the optimization level is above 0, the override will be applied during level 1 optimization.
3655+ if (level == TransformerLevel::Default) {
3656+ if (graph_optimization_level == TransformerLevel::Default) {
3657+ transformers_to_register = [&]() {
36583658 return optimizer_utils::GenerateTransformers (level, session_options_, cpu_ep, logger,
36593659 optimizers_to_disable_,
36603660 GetIntraOpThreadPoolToUse ());
3661- } else {
3662- const auto sat_context =
3663- minimal_build_optimization_handling ==
3664- MinimalBuildOptimizationHandling::SaveMinimalBuildRuntimeOptimizations
3665- ? SatApplyContextVariant{SatRuntimeOptimizationSaveContext{
3666- record_runtime_optimization_produced_op_schema_fn}}
3667- : SatApplyContextVariant{SatDirectApplicationContext{}};
3668- return optimizer_utils::GenerateTransformersForMinimalBuild (level, session_options_, sat_context, cpu_ep,
3669- logger,
3670- optimizers_to_disable_,
3671- GetIntraOpThreadPoolToUse ());
3672- }
3673- }();
3661+ };
3662+ }
3663+ } else {
3664+ if (graph_optimization_level >= level) {
3665+ // Generate and register transformers for level
3666+ transformers_to_register = [&]() {
3667+ const bool use_full_build_optimizations =
3668+ level == TransformerLevel::Level1 ||
3669+ minimal_build_optimization_handling == MinimalBuildOptimizationHandling::ApplyFullBuildOptimizations;
3670+
3671+ if (use_full_build_optimizations) {
3672+ return optimizer_utils::GenerateTransformers (level, session_options_, cpu_ep, logger,
3673+ optimizers_to_disable_,
3674+ GetIntraOpThreadPoolToUse ());
3675+ } else {
3676+ const auto sat_context =
3677+ minimal_build_optimization_handling ==
3678+ MinimalBuildOptimizationHandling::SaveMinimalBuildRuntimeOptimizations
3679+ ? SatApplyContextVariant{SatRuntimeOptimizationSaveContext{
3680+ record_runtime_optimization_produced_op_schema_fn}}
3681+ : SatApplyContextVariant{SatDirectApplicationContext{}};
3682+ return optimizer_utils::GenerateTransformersForMinimalBuild (level, session_options_, sat_context, cpu_ep,
3683+ logger,
3684+ optimizers_to_disable_,
3685+ GetIntraOpThreadPoolToUse ());
3686+ }
3687+ };
3688+ }
3689+ }
36743690
3675- for (auto & entry : transformers_to_register) {
3691+ if (transformers_to_register) { // Ensure the lambda is initialized before invoking it
3692+ for (auto & entry : transformers_to_register ()) {
36763693 ORT_RETURN_IF_ERROR (transformer_manager.Register (std::move (entry), level));
36773694 }
36783695 }
0 commit comments