Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scalar case if "then" and "else" subgraph output are scalar. #28444

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/core/src/op/if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,19 @@ static ov::PartialShape resolve_shape(const ov::PartialShape& then_pshape, const
return ov::PartialShape::dynamic();
}
if (then_rank.get_length() != else_rank.get_length()) {
auto is_one_element = [](const ov::PartialShape& pshape) {
return pshape.size() == 0 || pshape[0].get_max_length() == 1;
};
mitruska marked this conversation as resolved.
Show resolved Hide resolved
// Union of scalar and 1D case
if (then_rank.get_length() <= 1 && else_rank.get_length() <= 1) {
return ov::PartialShape::dynamic(1);
return (is_one_element(then_pshape) && is_one_element(else_pshape)) ?
ov::PartialShape{1} : ov::PartialShape::dynamic(1);
} else {
return ov::PartialShape::dynamic();
}
}
ov::PartialShape new_dims;

ov::PartialShape new_dims;
// If ranges are equal each dimension of then_body output is union with each dimension of
// else_body
for (auto then_it = then_pshape.cbegin(), else_it = else_pshape.cbegin(); then_it != then_pshape.cend();
Expand Down
30 changes: 30 additions & 0 deletions src/core/tests/type_prop/if.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,36 @@ TEST(type_prop, if_scalar_and_1d_static_union) {
EXPECT_EQ(sh, out_shape);
}

TEST(type_prop, if_output_one_element) {
// That which we iterate over
auto X = make_shared<ov::op::v0::Parameter>(element::f32, Shape{});
auto Y = make_shared<ov::op::v0::Parameter>(element::f32, Shape{1});
mitruska marked this conversation as resolved.
Show resolved Hide resolved
auto cond = make_shared<ov::op::v0::Parameter>(element::boolean, Shape{});

// Body parameters
auto Xt = make_shared<ov::op::v0::Parameter>(element::f32, PartialShape::dynamic());
auto Ye = make_shared<ov::op::v0::Parameter>(element::f32, PartialShape::dynamic());
// Body
auto then_op = std::make_shared<op::v1::Add>(Xt, Xt);
auto then_body_res = make_shared<ov::op::v0::Result>(then_op);
auto then_body = make_shared<ov::Model>(OutputVector{then_body_res}, ParameterVector{Xt});

auto else_op = std::make_shared<op::v1::Maximum>(Ye, Ye);
auto else_body_res = make_shared<ov::op::v0::Result>(else_op);
auto else_body = make_shared<ov::Model>(OutputVector{else_body_res}, ParameterVector{Ye});

auto if_op = make_shared<op::v8::If>(cond);
if_op->set_then_body(then_body);
if_op->set_else_body(else_body);
if_op->set_input(X, Xt, nullptr);
if_op->set_input(Y, nullptr, Ye);
auto res = if_op->set_output(then_body_res, else_body_res);
auto result0 = make_shared<ov::op::v0::Result>(res);
PartialShape out_shape{PartialShape{1}};
mitruska marked this conversation as resolved.
Show resolved Hide resolved
auto sh = result0->get_output_partial_shape(0);
EXPECT_EQ(sh, out_shape);
}

TEST(type_prop, if_element_type_dynamic) {
// That which we iterate over
auto X = make_shared<ov::op::v0::Parameter>(element::f16, Shape{32, 40, 10});
Expand Down
Loading