OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[Core] Loop: relax static merged-input dim grown by the body (#36512)
Maxim Vafina3897d3dcbbf
diff --git a/src/core/src/op/loop.cpp b/src/core/src/op/loop.cppindex 65c2e896dcb..934a2a68436 100644--- a/src/core/src/op/loop.cpp+++ b/src/core/src/op/loop.cpp@@ -236,9 +236,9 @@ void Loop::validate_and_infer_types() {PartialShape new_ps;bool shape_changed = false;if (body_rank_len == input_rank_len) {- new_ps = body_value_shape;+ new_ps = input_param_ps;for (auto j = 0; j < body_rank_len; j++) {- if (!body_value_shape[j].compatible(input_param_ps[j])) {+ if (!input_param_ps[j].relaxes(body_value_shape[j])) {new_ps[j] = Dimension::dynamic();shape_changed = true;}diff --git a/src/core/tests/type_prop/loop.cpp b/src/core/tests/type_prop/loop.cppindex 45c39f15e0e..1faa460e87b 100644--- a/src/core/tests/type_prop/loop.cpp+++ b/src/core/tests/type_prop/loop.cpp@@ -424,17 +424,18 @@ TEST(type_prop, loop_operation_for_and_condition_mode_dynamic_iter_partially_dynauto result0 = make_shared<ov::op::v0::Result>(out0);auto result1 = make_shared<ov::op::v0::Result>(out1);auto result2 = make_shared<ov::op::v0::Result>(out2);- Shape out0_shape{1};+ // M_body relaxes to dynamic (fed back from dynamic Zo), so out0 and out1 become dynamic.+ PartialShape out0_shape{Dimension::dynamic()};PartialShape out1_shape{Dimension::dynamic()};PartialShape out2_shape{Dimension::dynamic()};auto results = ResultVector{result0, result1, result2};auto f = make_shared<Model>(results, ParameterVector{X, Y, M});- EXPECT_EQ(result0->get_output_shape(0), out0_shape);+ EXPECT_EQ(result0->get_output_partial_shape(0), out0_shape);EXPECT_EQ(result1->get_output_partial_shape(0), out1_shape);EXPECT_EQ(result2->get_output_partial_shape(0), out2_shape);- EXPECT_EQ(loop->get_output_shape(0), out0_shape);+ EXPECT_EQ(loop->get_output_partial_shape(0), out0_shape);EXPECT_EQ(loop->get_output_partial_shape(1), out1_shape);EXPECT_EQ(loop->get_output_partial_shape(2), out2_shape);}@@ -1507,3 +1508,75 @@ TEST(type_prop, loop_merged_state_only_input_zero_dim_widened_dynamic_step) {EXPECT_EQ(body_carried->get_partial_shape(), (PartialShape{Dimension::dynamic(), 4}));EXPECT_EQ(loop->get_output_partial_shape(0), PartialShape{Dimension::dynamic()});}++// Static seed [1, 4] grown along axis 0 by the body: param dim[0] relaxes to dynamic, output [1.., 4].+TEST(type_prop, loop_merged_static_seed_dim_grown_by_body) {+ const auto body_param = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{1, 4});+ const auto extra = ov::op::v0::Constant::create(element::f32, Shape{1, 4}, {0});+ const auto concat = std::make_shared<ov::op::v0::Concat>(NodeVector{body_param, extra}, 0);+ const auto true_const = ov::op::v0::Constant::create(element::boolean, {1}, {1});+ auto body = std::make_shared<Model>(OutputVector{concat, true_const}, ParameterVector{body_param});++ const auto seed = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{1, 4});+ const auto trip_count = ov::op::v0::Constant::create(element::i64, {1}, {3});+ const auto exec_cond = ov::op::v0::Constant::create(element::boolean, {1}, {1});+ const auto loop = std::make_shared<ov::op::v5::Loop>(trip_count, exec_cond);+ loop->set_function(body);+ loop->set_merged_input(body_param, seed, concat);+ loop->set_special_body_ports(ov::op::v5::Loop::SpecialBodyPorts{-1, 1});++ auto result = std::make_shared<ov::op::v0::Result>(loop->get_iter_value(concat, -1));+ auto outer = std::make_shared<Model>(ResultVector{result}, ParameterVector{seed});++ EXPECT_EQ(result->get_output_partial_shape(0), (PartialShape{Dimension(1, -1), 4}));+}++// Back-edge reconciliation must reach a fixed point when the parameter is already+// dynamic, otherwise an unknown trip count spins the loop INT_MAX times.+TEST(type_prop, loop_back_edge_reconciliation_converges_when_param_already_dynamic) {+ const auto body_param = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape::dynamic(1));+ const auto extra = ov::op::v0::Constant::create(element::f32, Shape{1}, {0});+ const auto concat = std::make_shared<ov::op::v0::Concat>(NodeVector{body_param, extra}, 0);+ const auto true_const = ov::op::v0::Constant::create(element::boolean, {1}, {1});+ auto body = std::make_shared<Model>(OutputVector{concat, true_const}, ParameterVector{body_param});++ const auto seed = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape::dynamic(1));+ // Unknown trip count → INT_MAX iterations; must still converge.+ const auto trip_count = std::make_shared<ov::op::v0::Parameter>(element::i64, PartialShape{1});+ const auto exec_cond = ov::op::v0::Constant::create(element::boolean, {1}, {1});+ const auto loop = std::make_shared<ov::op::v5::Loop>(trip_count, exec_cond);+ loop->set_function(body);+ loop->set_merged_input(body_param, seed, concat);+ loop->set_special_body_ports(ov::op::v5::Loop::SpecialBodyPorts{-1, 1});++ auto result = std::make_shared<ov::op::v0::Result>(loop->get_iter_value(concat, -1));+ auto outer = std::make_shared<Model>(ResultVector{result}, ParameterVector{seed, trip_count});++ EXPECT_TRUE(result->get_output_partial_shape(0).is_dynamic());+}++// Static seed dim fed back from a dynamic body value must relax() to dynamic;+// compatible() would wrongly leave it static (autoregressive decoder pattern).+TEST(type_prop, loop_merged_static_seed_relaxed_when_body_value_dynamic) {+ const auto body_carried = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{1, 4});+ const auto body_step = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{1, Dimension::dynamic()});+ const auto concat = std::make_shared<ov::op::v0::Concat>(NodeVector{body_carried, body_step}, 1);+ const auto true_const = ov::op::v0::Constant::create(element::boolean, {1}, {1});+ auto body = std::make_shared<Model>(OutputVector{concat, true_const}, ParameterVector{body_carried, body_step});++ const auto seed = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{1, 4});+ const auto step = std::make_shared<ov::op::v0::Parameter>(element::f32, PartialShape{1, Dimension::dynamic()});+ const auto trip_count = ov::op::v0::Constant::create(element::i64, {1}, {3});+ const auto exec_cond = ov::op::v0::Constant::create(element::boolean, {1}, {1});+ const auto loop = std::make_shared<ov::op::v5::Loop>(trip_count, exec_cond);+ loop->set_function(body);+ loop->set_special_body_ports(ov::op::v5::Loop::SpecialBodyPorts{-1, 1});+ loop->set_merged_input(body_carried, seed, concat);+ loop->set_invariant_input(body_step, step);++ auto result = std::make_shared<ov::op::v0::Result>(loop->get_iter_value(concat, -1));+ auto outer = std::make_shared<Model>(ResultVector{result}, ParameterVector{seed, step});++ EXPECT_EQ(body_carried->get_partial_shape(), (PartialShape{1, Dimension::dynamic()}));+ EXPECT_TRUE(loop->get_output_partial_shape(0)[1].is_dynamic());+}diff --git a/src/frontends/onnx/tests/models/controlflow/loop_concat_growing_merged_dim.prototxt b/src/frontends/onnx/tests/models/controlflow/loop_concat_growing_merged_dim.prototxtnew file mode 100644index 00000000000..b57575a57aa--- /dev/null+++ b/src/frontends/onnx/tests/models/controlflow/loop_concat_growing_merged_dim.prototxt@@ -0,0 +1,124 @@+ir_version: 7+producer_name: "OpenVINO ONNX Frontend"+graph {+ node {+ input: "trip_count"+ input: "cond_init"+ input: "a_init"+ output: "a_final"+ op_type: "Loop"+ attribute {+ name: "body"+ g {+ node {+ input: "cond_in"+ output: "cond_out"+ op_type: "Identity"+ }+ node {+ input: "a_in"+ input: "step"+ output: "a_out"+ op_type: "Concat"+ attribute {+ name: "axis"+ i: 0+ type: INT+ }+ }+ name: "loop_body"+ initializer {+ dims: 1+ data_type: 1+ float_data: 2+ name: "step"+ }+ input {+ name: "i"+ type {+ tensor_type {+ elem_type: 7+ shape {+ }+ }+ }+ }+ input {+ name: "cond_in"+ type {+ tensor_type {+ elem_type: 9+ shape {+ }+ }+ }+ }+ input {+ name: "a_in"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ }+ }+ }+ }+ }+ output {+ name: "cond_out"+ type {+ tensor_type {+ elem_type: 9+ }+ }+ }+ output {+ name: "a_out"+ type {+ tensor_type {+ elem_type: 1+ }+ }+ }+ }+ type: GRAPH+ }+ }+ initializer {+ dims: 1+ data_type: 7+ int64_data: 3+ name: "trip_count"+ }+ initializer {+ dims: 1+ data_type: 9+ int32_data: 1+ name: "cond_init"+ }+ input {+ name: "a_init"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ output {+ name: "a_final"+ type {+ tensor_type {+ elem_type: 1+ }+ }+ }+}+opset_import {+ version: 13+}diff --git a/src/frontends/onnx/tests/onnx_import_controlflow.in.cpp b/src/frontends/onnx/tests/onnx_import_controlflow.in.cppindex 86adba998a9..04f9bf2c9e6 100644--- a/src/frontends/onnx/tests/onnx_import_controlflow.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_controlflow.in.cpp@@ -927,6 +927,21 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_sequence_insert_chain_concat) {test_case.run();}+/// @brief Body Concat(a_in, [2.0]) grows the merged-input dim each iteration; the body+/// parameter must widen to dynamic and the loop run to completion without hanging.+OPENVINO_TEST(${BACKEND_NAME}, onnx_loop_back_edge_growing_merged_dim) {+ const auto model = convert_model("controlflow/loop_concat_growing_merged_dim.onnx");++ auto test_case = ov::test::TestCase(model, s_device);+ // seed = [1.0], trip_count = 3, step (body initializer) = [2.0]+ // iter 0: concat([1.0], [2.0]) = [1.0, 2.0]+ // iter 1: concat([1.0, 2.0], [2.0]) = [1.0, 2.0, 2.0]+ // iter 2: concat([1.0, 2.0, 2.0], [2.0]) = [1.0, 2.0, 2.0, 2.0]+ test_case.add_input<float>({1.f}); // a_init+ test_case.add_expected_output<float>(Shape{4}, {1.f, 2.f, 2.f, 2.f});+ test_case.run();+}+/// @brief Test SequenceEmpty -> SequenceInsert chain -> ConcatFromSequence with new_axis=1/// Exercises the same chain pattern starting from an empty sequence and stacking along a new axis.OPENVINO_TEST(${BACKEND_NAME}, onnx_sequence_insert_chain_new_axis) {