Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

Fix ReshapePRelu crash on scalar input (#33937)

Evgeny Kotovfd8315b20335
diff --git a/src/common/transformations/src/transformations/common_optimizations/reshape_prelu.cpp b/src/common/transformations/src/transformations/common_optimizations/reshape_prelu.cpp
index e537d58c0e7..68805cc6667 100644
--- a/src/common/transformations/src/transformations/common_optimizations/reshape_prelu.cpp
+++ b/src/common/transformations/src/transformations/common_optimizations/reshape_prelu.cpp
@@ -10,6 +10,7 @@
#include "openvino/op/constant.hpp"
#include "openvino/op/prelu.hpp"
#include "openvino/op/reshape.hpp"
+#include "openvino/pass/pattern/op/pattern.hpp"
#include "openvino/pass/pattern/op/wrap_type.hpp"
#include "transformations/utils/utils.hpp"
@@ -19,8 +20,9 @@ namespace ov::pass {
ReshapePRelu::ReshapePRelu() {
MATCHER_SCOPE(ReshapePRelu);
- auto input_m = pattern::any_input(pattern::has_static_rank());
- auto slope_m = pattern::any_input(pattern::has_static_rank());
+ // PRelu input must have rank >= 2 to access channel_dim_idx=1
+ auto input_m = pattern::any_input(pattern::has_static_rank() && pattern::rank_more_than(1));
+ auto slope_m = pattern::any_input(pattern::has_static_rank() && pattern::rank_equals(1));
auto prelu_m = pattern::wrap_type<v0::PRelu>({input_m, slope_m});
matcher_pass_callback callback = [=](pattern::Matcher& m) {
@@ -32,10 +34,6 @@ ReshapePRelu::ReshapePRelu() {
const auto prelu_pshape = prelu->get_input_partial_shape(0);
const auto prelu_rank = prelu_pshape.rank();
const auto slope_pshape = prelu->get_input_partial_shape(1);
- const auto slope_rank = slope_pshape.rank();
- if (prelu_rank.get_length() == 1 || slope_rank.get_length() != 1) {
- return false;
- }
const auto channel_dim_idx = 1;
if (slope_pshape.is_static()) {
diff --git a/src/common/transformations/tests/common_optimizations/reshape_prelu_test.cpp b/src/common/transformations/tests/common_optimizations/reshape_prelu_test.cpp
index b1beec8efec..4109146729c 100644
--- a/src/common/transformations/tests/common_optimizations/reshape_prelu_test.cpp
+++ b/src/common/transformations/tests/common_optimizations/reshape_prelu_test.cpp
@@ -253,3 +253,24 @@ TEST(TransformationTests, ReshapePReluTest9) {
auto res = compare_functions(f, f_ref);
ASSERT_TRUE(res.first) << res.second;
}
+
+// Test for scalar input (rank=0) - should not crash and should not apply transformation
+TEST(TransformationTests, ReshapePReluTestScalarInput) {
+ std::shared_ptr<ov::Model> f(nullptr), f_ref(nullptr);
+ {
+ auto input = std::make_shared<opset1::Parameter>(element::f32, Shape{});
+ auto slope = opset1::Constant::create(element::f32, Shape{1}, {-2.f});
+ auto prelu = std::make_shared<opset1::PRelu>(input, slope);
+
+ f = std::make_shared<ov::Model>(OutputVector{prelu}, ParameterVector{input});
+ f_ref = f;
+
+ pass::Manager m;
+ m.register_pass<ov::pass::InitNodeInfo>();
+ m.register_pass<ReshapePRelu>();
+ m.run_passes(f);
+ }
+
+ auto res = compare_functions(f, f_ref);
+ ASSERT_TRUE(res.first) << res.second;
+}