Skip to content

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

Fix accuracy issue from fusion (#31404)

Min, Byungil6d1c76fc4851
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/handle_reshape.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/handle_reshape.cpp
index 3f07b505388..e2b53fe9eaf 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/handle_reshape.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/handle_reshape.cpp
@@ -201,22 +201,22 @@ void handle_reshape::run(program& p) {
}
}
- auto reshape_layout = node->get_output_layout();
- auto target_format = format::get_default_format(reshape_layout.get_rank());
auto input_layout = input_node.get_output_layout();
- auto target_layout = layout({reshape_layout.get_partial_shape(), reshape_layout.data_type, target_format});
+ auto reshape_layout = node->get_output_layout();
+ auto expected_reshape_format = format::get_default_format(reshape_layout.get_rank());
+ auto expected_reshape_layout = layout({reshape_layout.get_partial_shape(), reshape_layout.data_type, expected_reshape_format});
if (!(node->is_output()) &&
- (((reshape_layout.format != target_format) && !reshape_layout.compatible(target_layout)) ||
+ (((reshape_layout.format != expected_reshape_format) && !reshape_layout.compatible(expected_reshape_layout)) ||
(reshape_layout.format != input_layout.format))) {
// when some primitive does an implicit reorder to some other format then we lose the info about pitches
// in reshape stage we assume user provides the input vector in bfyx
-
// Check whether input reorder is required for format change
if (!format::is_simple_data_format(input_layout.format)) {
+ auto preferred_input_format = format::get_default_format(input_layout.get_rank());
auto reshape_input = std::make_shared<reorder>("reorder:_reshape_input_" + node->id(),
cldnn::input_info(input_node.id(), input_port),
- target_format,
+ preferred_input_format,
reshape_layout.data_type);
GPU_DEBUG_LOG << "reshape_handler: " << reshape_input->id
<< " input_info : " << reshape_input->dependencies().front().to_string() << std::endl;
@@ -227,7 +227,7 @@ void handle_reshape::run(program& p) {
}
// Check whether output reorder is required for format change
- if (reshape_layout.format != target_format) {
+ if (reshape_layout.format != expected_reshape_format) {
auto reshape_users = node->get_users();
for (const auto& user : reshape_users) {
auto reshape_output = std::make_shared<reorder>("reorder:_reshape_output_" + node->id(),
diff --git a/src/plugins/intel_gpu/src/graph/include/reduce_inst.h b/src/plugins/intel_gpu/src/graph/include/reduce_inst.h
index 04c9ab185fb..0456376385b 100644
--- a/src/plugins/intel_gpu/src/graph/include/reduce_inst.h
+++ b/src/plugins/intel_gpu/src/graph/include/reduce_inst.h
@@ -38,7 +38,8 @@ public:
return false;
auto input_layout = _deps[0].first->_impl_params->get_output_layout(_deps[0].second);
- if (!format::format::is_simple_data_format(input_layout.format) && input_layout.feature() % 16 != 0) {
+ if (!format::format::is_simple_data_format(input_layout.format) &&
+ (input_layout.is_static() && input_layout.feature() % 16 != 0)) {
return true;
}
return false;
diff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
index 5553ddd97dc..c4cbb08d311 100644
--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
@@ -1260,6 +1260,7 @@ format layout_optimizer::get_preferred_format(program_node& node) {
} catch (ov::Exception&) {
fmt = format::get_default_format(in_lay_rank);
}
+
node.set_preferred_input_fmt(i, fmt);
}
}
@@ -1288,6 +1289,13 @@ format layout_optimizer::get_preferred_format(program_node& node) {
expected = node.get_output_layout().format;
}
} else if (node.is_type<reshape>()) {
+ // Reshape from blocked to simple format is not acceptable
+ auto dep_size = node.get_dependencies().size();
+ for (size_t i = 0; i < dep_size; i++) {
+ auto in_lay_rank = node.get_input_layout(i).get_rank();
+ node.set_preferred_input_fmt(i, format::get_default_format(in_lay_rank));
+ }
+
expected = format::get_default_format(node.get_output_layout().get_rank());
} else if (node.is_type<deconvolution>()) {
expected = get_expected_format(node.as<deconvolution>());
diff --git a/src/plugins/intel_gpu/tests/unit/passes/handle_reshape.cpp b/src/plugins/intel_gpu/tests/unit/passes/handle_reshape.cpp
index 9e165327792..7b031ae042d 100644
--- a/src/plugins/intel_gpu/tests/unit/passes/handle_reshape.cpp
+++ b/src/plugins/intel_gpu/tests/unit/passes/handle_reshape.cpp
@@ -211,70 +211,6 @@ TEST(handle_reshape, correct_parameters_propagation_2_inputs) {
ASSERT_EQ(out_shape1, expected_out_shape);
}
-TEST(handle_reshape, reshape_input_reorder) {
- tests::random_generator rg(GET_SUITE_NAME);
- auto& engine = get_test_engine();
- auto shape_memory = engine.allocate_memory({ ov::PartialShape{5}, data_types::i32, format::bfyx });
- auto in0_layout = layout{ ov::PartialShape{1, -1, 16, 64, 64}, data_types::f16, format::bfzyx };
- auto in0_memory = engine.allocate_memory(layout{ ov::PartialShape{1, 2, 16, 64, 64}, data_types::f16, format::bfzyx });
- auto in1_layout = layout{ ov::PartialShape{-1, 16, 64, 64}, data_types::f16, format::bfyx };
- auto in1_memory = engine.allocate_memory({ ov::PartialShape{2, 16, 64, 64}, data_types::f16, format::bfyx });
-
- auto in0 = rg.generate_random_1d<ov::float16>(in0_memory->count(), -10, 10);
- auto in1 = rg.generate_random_1d<ov::float16>(in1_memory->count(), -10, 10);
- set_values<ov::float16>(in0_memory, in0);
- set_values<int32_t>(shape_memory, {1, 2, 16, 64, 64});
- set_values<ov::float16>(in1_memory, in1);
-
- topology topology;
- topology.add(input_layout("input0", in0_layout));
- topology.add(input_layout("target_shape", shape_memory->get_layout()));
- topology.add(broadcast("broadcast", input_info("input0"), input_info("target_shape"), {}, ov::op::BroadcastType::BIDIRECTIONAL));
- topology.add(reshape("reshape", input_info("broadcast"), true, {-1, 16, 64, 64}, {-1, 16, 64, 64}));
- topology.add(input_layout("input1", in1_layout));
- topology.add(eltwise("eltw", input_info("reshape"), input_info("input1"), eltwise_mode::sum));
- topology.add(reorder("reorder", input_info("eltw"), format::bfyx, data_types::f32));
-
- ExecutionConfig config = get_test_default_config(engine);
- config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
- config.set_property(ov::intel_gpu::optimize_data(true));
- auto prog = program::build_program(engine, topology, config);
-
- ASSERT_NE(prog, nullptr);
- ASSERT_TRUE(has_node_with_type<reshape>(*prog));
-
- ASSERT_TRUE(prog->get_node("reshape").can_be_optimized());
- auto reshape_layout_in = prog->get_node("reshape").get_input_layouts()[0];
- auto reshape_layout_out = prog->get_node("reshape").get_output_layout();
-
- // At this moment transfomations insert reorder before reshape which
- // converts tensor to default format with rank = reshape_out_rank
- // Likely in the future we'll update that reorder so it will use reshape_input_rank
- // After that expected in format will be bfzyx
- // [Updated] get_preferred_format() updated to use 'in_lay_rank' instead of 'out_lay_rank' for preferred input format
- ASSERT_EQ(reshape_layout_in.format, format::bfzyx);
- ASSERT_EQ(reshape_layout_out.format, format::bfyx);
-
- ov::PartialShape expected_out_shape{-1, 16, 64, 64};
- ASSERT_EQ(reshape_layout_out.get_partial_shape(), expected_out_shape);
-
- network net(prog);
-
- net.set_input_data("input0", in0_memory);
- net.set_input_data("input1", in1_memory);
- net.set_input_data("target_shape", shape_memory);
- auto output = net.execute();
-
- auto out_mem = output.at("reorder").get_memory();
- mem_lock<float> lock(out_mem, get_test_stream());
-
- for (size_t i = 0; i < out_mem->count(); i++) {
- float expected = static_cast<float>(in0[i]) + static_cast<float>(in1[i]);
- float actual = lock[i];
- ASSERT_EQ(expected, actual) << " i = " << i;
- }
-}
-
TEST(handle_reshape, reshape_opt_out_layout_update) {
tests::random_generator rg(GET_SUITE_NAME);
auto& engine = get_test_engine();
@@ -313,3 +249,77 @@ TEST(handle_reshape, reshape_opt_out_layout_update) {
ASSERT_EQ(reshape_layout_in.format, format::bfyx);
ASSERT_EQ(reshape_layout_out.format, format::bfyx);
}
+
+class handle_reshape_mismatch_format_test: public ::testing::Test {
+public:
+ void test_reshape_mismatch_input_format(format reshape_input_format) {
+ tests::random_generator rg(GET_SUITE_NAME);
+ auto& engine = get_test_engine();
+ auto shape_memory = engine.allocate_memory({ ov::PartialShape{5}, data_types::i32, format::bfyx });
+ auto in0_layout = layout{ ov::PartialShape{1, -1, 16, 64, 64}, data_types::f32, format::bfzyx };
+ auto in0_memory = engine.allocate_memory(layout{ ov::PartialShape{1, 2, 16, 64, 64}, data_types::f32, format::bfzyx });
+ auto in1_layout = layout{ ov::PartialShape{-1, 16, 64, 64}, data_types::f32, format::bfyx };
+ auto in1_memory = engine.allocate_memory({ ov::PartialShape{2, 16, 64, 64}, data_types::f32, format::bfyx });
+
+ auto in0 = rg.generate_random_1d<float>(in0_memory->count(), -10, 10);
+ auto in1 = rg.generate_random_1d<float>(in1_memory->count(), -10, 10);
+ set_values<float>(in0_memory, in0);
+ set_values<int32_t>(shape_memory, {1, 2, 16, 64, 64});
+ set_values<float>(in1_memory, in1);
+
+ topology topology;
+ topology.add(input_layout("input0", in0_layout));
+ topology.add(reorder("reorder_input0", input_info("input0"), reshape_input_format, data_types::f16));
+ topology.add(input_layout("target_shape", shape_memory->get_layout()));
+ topology.add(broadcast("broadcast", input_info("reorder_input0"), input_info("target_shape"), {}, ov::op::BroadcastType::BIDIRECTIONAL));
+ topology.add(reshape("reshape", input_info("broadcast"), true, {-1, 16, 64, 64}, {-1, 16, 64, 64}));
+ topology.add(input_layout("input1", in1_layout));
+ topology.add(reorder("reorder_input1", input_info("input1"), format::bfyx, data_types::f16));
+ topology.add(eltwise("eltw", input_info("reshape"), input_info("reorder_input1"), eltwise_mode::sum));
+ topology.add(reorder("reorder", input_info("eltw"), format::bfyx, data_types::f32));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ auto prog = program::build_program(engine, topology, config);
+
+ ASSERT_NE(prog, nullptr);
+ ASSERT_TRUE(has_node_with_type<reshape>(*prog));
+
+ ASSERT_TRUE(prog->get_node("reshape").can_be_optimized());
+ auto reshape_layout_in = prog->get_node("reshape").get_input_layouts()[0];
+ auto reshape_layout_out = prog->get_node("reshape").get_output_layout();
+
+ // Expected in format will be bfzyx for 5dims
+ // [Updated] get_preferred_format() updated to use 'in_lay_rank' instead of 'out_lay_rank' for preferred input format
+ ASSERT_EQ(reshape_layout_in.format, format::bfzyx);
+ ASSERT_EQ(reshape_layout_out.format, format::bfyx);
+
+ ov::PartialShape expected_out_shape{-1, 16, 64, 64};
+ ASSERT_EQ(reshape_layout_out.get_partial_shape(), expected_out_shape);
+
+ network net(prog);
+
+ net.set_input_data("input0", in0_memory);
+ net.set_input_data("input1", in1_memory);
+ net.set_input_data("target_shape", shape_memory);
+ auto output = net.execute();
+
+ auto out_mem = output.at("reorder").get_memory();
+ mem_lock<float> lock(out_mem, get_test_stream());
+
+ for (size_t i = 0; i < out_mem->count(); i++) {
+ float expected = in0[i] + in1[i];
+ float actual = lock[i];
+ ASSERT_NEAR(expected, actual, 1e-4);
+ }
+ }
+};
+
+TEST_F(handle_reshape_mismatch_format_test, reshape_5dim_simple_input_reorder) {
+ this->test_reshape_mismatch_input_format(format::bfzyx);
+}
+
+TEST_F(handle_reshape_mismatch_format_test, reshape_5dim_blocked_input_reorder) {
+ this->test_reshape_mismatch_input_format(format::b_fs_zyx_fsv16);
+}
diff --git a/src/plugins/intel_gpu/tests/unit/passes/reorder_inputs_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/reorder_inputs_test.cpp
index 4f9707ae59d..7ce5e1691f1 100644
--- a/src/plugins/intel_gpu/tests/unit/passes/reorder_inputs_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/passes/reorder_inputs_test.cpp
@@ -191,7 +191,7 @@ TEST(reorder_inputs, mixed_ranks_reshape) {
auto& reshape_node = prog_impl->get_node("reshape");
- ov::PartialShape expected_reorder_shape{ 1, 32, 16384, 2 };
+ ov::PartialShape expected_reorder_shape{ 1, 32, 128, 128, 2 };
ASSERT_EQ(reshape_node.get_input_layouts()[0].get_partial_shape(), expected_reorder_shape);
}