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
Preserve current_iteration element type in UnrollTensorIterator (#35428)
SamareshSingh349dcb5cb820
diff --git a/src/common/transformations/src/transformations/control_flow/unroll_tensor_iterator.cpp b/src/common/transformations/src/transformations/control_flow/unroll_tensor_iterator.cppindex afe51852ac9..d9fc4f84f72 100644--- a/src/common/transformations/src/transformations/control_flow/unroll_tensor_iterator.cpp+++ b/src/common/transformations/src/transformations/control_flow/unroll_tensor_iterator.cpp@@ -193,7 +193,8 @@ bool ov::pass::UnrollTensorIterator::run_on_model(const std::shared_ptr<ov::Modefor (int64_t idx = 0; idx < num_iter; ++idx) {const auto iter_idx = loop->get_special_body_ports().current_iteration_input_idx;const auto& param_to_delete = body_functions[idx]->get_parameters()[iter_idx];- auto cur_iter_const = std::make_shared<v0::Constant>(ov::element::i64, Shape{}, idx);+ auto cur_iter_const =+ std::make_shared<v0::Constant>(param_to_delete->get_element_type(), Shape{}, idx);replace_node(param_to_delete, cur_iter_const);body_functions[idx]->remove_parameter(param_to_delete);}diff --git a/src/common/transformations/tests/control_flow/unroll_tensor_iterator_test.cpp b/src/common/transformations/tests/control_flow/unroll_tensor_iterator_test.cppindex 1a89fac0996..e5ebcfad93c 100644--- a/src/common/transformations/tests/control_flow/unroll_tensor_iterator_test.cpp+++ b/src/common/transformations/tests/control_flow/unroll_tensor_iterator_test.cpp@@ -13,9 +13,13 @@#include "common_test_utils/ov_test_utils.hpp"#include "common_test_utils/test_common.hpp"#include "openvino/core/model.hpp"+#include "openvino/op/add.hpp"#include "openvino/op/concat.hpp"+#include "openvino/op/constant.hpp"+#include "openvino/op/convert.hpp"#include "openvino/op/gru_cell.hpp"#include "openvino/op/gru_sequence.hpp"+#include "openvino/op/loop.hpp"#include "openvino/op/lstm_cell.hpp"#include "openvino/op/rnn_cell.hpp"#include "openvino/op/split.hpp"@@ -627,3 +631,60 @@ TEST(TransformationTests, CheckTensorNamesAfterUnrolling) {ASSERT_EQ(names_after.size(), 0);EXPECT_EQ(names_before, names_after);}++// Regression test: when the Loop's current_iteration parameter has type i32, the+// replacement Constant generated by UnrollTensorIterator must also be i32 (not i64).+// Hardcoding i64 causes type mismatches downstream (e.g. Add gets i64+i32 inputs),+// which is the root cause of the GPU bug reported in GitHub issue #35411.+TEST(TransformationTests, UnrollLoopCurrentIterationPreservesElementType) {+ const size_t num_iter = 4;+ const ov::Shape scalar{};++ auto b_iter = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, scalar);+ auto b_data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1});+ b_iter->set_friendly_name("b_iter");+ b_data->set_friendly_name("b_data");++ auto cast = std::make_shared<ov::op::v0::Convert>(b_iter, ov::element::f32);+ auto unsqueeze_axis = ov::op::v0::Constant::create(ov::element::i32, ov::Shape{1}, {0});+ auto iter_f32 = std::make_shared<ov::op::v0::Unsqueeze>(cast, unsqueeze_axis);+ auto b_add = std::make_shared<ov::op::v1::Add>(b_data, iter_f32);+ auto b_cond = ov::op::v0::Constant::create(ov::element::boolean, scalar, {true});++ auto body = std::make_shared<ov::Model>(ov::OutputVector{b_cond, b_add}, ov::ParameterVector{b_iter, b_data});++ auto trip_count = ov::op::v0::Constant::create(ov::element::i64, scalar, {num_iter});+ auto exec_cond = ov::op::v0::Constant::create(ov::element::boolean, scalar, {true});++ auto loop = std::make_shared<ov::op::v5::Loop>(trip_count, exec_cond);+ loop->set_function(body);+ loop->set_special_body_ports({0, 0});++ auto outer_data = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, ov::Shape{1});+ loop->set_merged_input(b_data, outer_data, b_add);+ loop->get_iter_value(b_add, -1);++ auto result = std::make_shared<ov::op::v0::Result>(loop->output(0));+ auto model = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{outer_data});++ ov::pass::Manager manager;+ manager.register_pass<ov::pass::InitNodeInfo>();+ manager.register_pass<ov::pass::UnrollTensorIterator>();+ manager.run_passes(model);++ for (const auto& node : model->get_ops()) {+ if (const auto constant = std::dynamic_pointer_cast<ov::op::v0::Constant>(node)) {+ if (constant->get_element_type() == ov::element::i64 && constant->get_shape() == ov::Shape{}) {+ const auto& consumers = constant->output(0).get_target_inputs();+ for (const auto& consumer : consumers) {+ EXPECT_FALSE(std::dynamic_pointer_cast<ov::op::v0::Convert>(+ consumer.get_node()->shared_from_this()) != nullptr)+ << "i64 constant feeds a Convert node after unrolling; "+ "current_iteration replacement should be i32, not i64";+ }+ }+ }+ }++ EXPECT_NO_THROW(model->validate_nodes_and_infer_types());+}diff --git a/src/plugins/intel_gpu/tests/functional/subgraph_tests/loop.cpp b/src/plugins/intel_gpu/tests/functional/subgraph_tests/loop.cppindex 597f9eab00d..eaca55cc5d6 100644--- a/src/plugins/intel_gpu/tests/functional/subgraph_tests/loop.cpp+++ b/src/plugins/intel_gpu/tests/functional/subgraph_tests/loop.cpp@@ -602,4 +602,60 @@ INSTANTIATE_TEST_SUITE_P(smoke_LoopWithScatterUpdate, LoopWithScatterUpdateTest,testing::Values(ov::element::f32, ov::element::f16),LoopWithScatterUpdateTest::getTestCaseName);+// Regression test for GitHub issue #35411.+// Loop body where the i32 current_iteration feeds via Cast+Unsqueeze into an Add+// together with an f32/f16 buffer. Before the fix, UnrollTensorIterator replaced+// the i32 Parameter with Constant(i64), causing a type mismatch at Add on GPU.+class LoopWithAddTest : public testing::WithParamInterface<ov::element::Type>, virtual public ov::test::SubgraphBaseTest {+public:+ static std::string getTestCaseName(const testing::TestParamInfo<ov::element::Type>& obj) {+ std::ostringstream result;+ result << "data_type=" << obj.param;+ return result.str();+ }++protected:+ void SetUp() override {+ targetDevice = ov::test::utils::DEVICE_GPU;+ const auto data_type = GetParam();++ const size_t num_iter = 8;+ const size_t feature_sz = 4;+ const ov::Shape data_shape{feature_sz};++ auto outer_data = std::make_shared<ov::op::v0::Parameter>(data_type, data_shape);+ outer_data->set_friendly_name("outer_data");++ auto trip_count = std::make_shared<ov::op::v0::Constant>(ov::element::i64, ov::Shape{}, num_iter);+ auto exec_cond = std::make_shared<ov::op::v0::Constant>(ov::element::boolean, ov::Shape{}, true);+ auto b_timestep = std::make_shared<ov::op::v0::Parameter>(ov::element::i32, ov::Shape{});+ auto b_data = std::make_shared<ov::op::v0::Parameter>(data_type, data_shape);+ b_timestep->set_friendly_name("timestep");+ b_data->set_friendly_name("b_data");++ auto cast = std::make_shared<ov::op::v0::Convert>(b_timestep, data_type);+ auto unsqueeze_axis = std::make_shared<ov::op::v0::Constant>(ov::element::i32, ov::Shape{1}, std::vector<int32_t>{0});+ auto timestep_1d = std::make_shared<ov::op::v0::Unsqueeze>(cast, unsqueeze_axis);+ auto b_add = std::make_shared<ov::op::v1::Add>(b_data, timestep_1d);+ auto b_cond = std::make_shared<ov::op::v0::Constant>(ov::element::boolean, ov::Shape{}, true);+ auto body = std::make_shared<ov::Model>(ov::OutputVector{b_cond, b_add}, ov::ParameterVector{b_timestep, b_data});+ auto loop = std::make_shared<ov::op::v5::Loop>(trip_count, exec_cond);+ loop->set_function(body);+ loop->set_special_body_ports({0, 0});+ loop->set_merged_input(b_data, outer_data, b_add);+ loop->get_iter_value(b_add, -1);++ auto result = std::make_shared<ov::op::v0::Result>(loop->output(0));+ function = std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{outer_data});++ targetStaticShapes = {{data_shape}};+ }+};++TEST_P(LoopWithAddTest, Inference) {+ run();+}++INSTANTIATE_TEST_SUITE_P(smoke_LoopWithAdd, LoopWithAddTest, testing::Values(ov::element::f32, ov::element::f16), LoopWithAddTest::getTestCaseName);+} // namespace