Skip to content

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

[GPU] Fix recursive format lookup for rank-changing Resample users (#39099)

Joonho Yeomd42881a8eb9d
diff --git a/src/plugins/intel_gpu/src/graph/include/layout_optimizer.h b/src/plugins/intel_gpu/src/graph/include/layout_optimizer.h
index 694443566b5..67371aa4789 100644
--- a/src/plugins/intel_gpu/src/graph/include/layout_optimizer.h
+++ b/src/plugins/intel_gpu/src/graph/include/layout_optimizer.h
@@ -6,6 +6,7 @@
#include <map>
#include <memory>
+#include <unordered_set>
#include <utility>
#include <vector>
@@ -154,6 +155,9 @@ private:
static const std::vector<std::pair<format::type, bool>> optimized_formats; // pair of format type and allowed weak restriction
size_t _total_conv;
std::map<std::pair<format::type, bool>, size_t> _optimized_conv_count;
+ // Resample nodes whose users are being checked by all_users_simple_format_until_output().
+ // Used to break the get_preferred_format() <-> all_users_simple_format_until_output() cycle.
+ std::unordered_set<const program_node*> _resample_users_check_in_progress;
format get_expected_format(const convolution_node& node);
format get_expected_format(const deconvolution_node& node);
diff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
index f01cba1f143..702ad21895f 100644
--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
@@ -1471,11 +1471,24 @@ format layout_optimizer::get_preferred_format(program_node& node) {
// if the resample is in the last part of the network and there are no users using blocked format,
// it is better to reorder to bfyx before resample is done.
// Skip all user format check when node is dynamic. It could cause endless recursive call in get_preferred_foramt()
- if (!node.is_dynamic() && all_users_simple_format_until_output(node, node, 0, 10)) {
- const auto& dim = format::dimension(node.get_output_layout().format);
- expected = format::get_default_format(dim, false, false);
- } else {
- expected = format::any;
+ // A rank-changing user (e.g. reshape, gather) may query this resample's preferred format again while its users are
+ // being checked, which would restart the user check and recurse endlessly. Such a re-entrant query returns any.
+ expected = format::any;
+ if (!node.is_dynamic()) {
+ const bool is_reentrant = !_resample_users_check_in_progress.insert(&node).second;
+ if (!is_reentrant) {
+ struct in_progress_guard {
+ std::unordered_set<const program_node*>& nodes;
+ const program_node* node;
+ ~in_progress_guard() {
+ nodes.erase(node);
+ }
+ } guard{_resample_users_check_in_progress, &node};
+ if (all_users_simple_format_until_output(node, node, 0, 10)) {
+ const auto& dim = format::dimension(node.get_output_layout().format);
+ expected = format::get_default_format(dim, false, false);
+ }
+ }
}
} else if (node.is_type<permute>()) {
if (node.get_dependencies().size() == 1 && node.get_dependencies().front().first->is_type<convolution>()) {
@@ -1559,9 +1572,7 @@ bool layout_optimizer::all_users_simple_format_until_output(program_node& origin
return false;
}
- // Reshape intrinsically requires plain input and output formats. Continue through it without
- // recursively querying its dependency's preferred format.
- if (cur_node.is_in_data_flow() && !cur_node.is_type<reshape>() && (cur_node.type() != origin_node.type())) {
+ if (cur_node.is_in_data_flow() && (cur_node.type() != origin_node.type())) {
const auto& fmt = get_preferred_format(cur_node);
if (fmt != format::any && !format::is_simple_data_format(fmt)) {
return false;
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 23d97b4da1f..2c6706d09e5 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
@@ -684,14 +684,22 @@ TEST(reorder_inputs, dynamic_conv_chain_no_throw) {
ASSERT_NE(prog, nullptr);
}
-TEST(reorder_inputs, static_resample_with_rank_changing_reshape_no_recursion) {
+// There are other rank-changing ops, such as eltwise and broadcast,
+// but we test these two as representative cases.
+enum class rank_changing_op { reshape, gather };
+
+class static_resample_with_rank_changing_user : public ::testing::TestWithParam<rank_changing_op> {};
+
+TEST_P(static_resample_with_rank_changing_user, no_recursion) {
// Topology:
//
- // input -> Resample -> Reshape (Unsqueeze) --+
- // +-> Concat -> Reduce -> Convolution
- // skip -------------> Reshape (Unsqueeze) ---+
- // The downstream format lookup must treat the rank-changing Reshape as an intrinsic plain-format
- // boundary instead of querying its Resample dependency's preferred format and re-entering the same lookup.
+ // input -> Resample -> RankChange (4D -> 5D) --+
+ // +-> Concat -> Reduce -> Convolution
+ // skip -------------> Reshape (Unsqueeze) -----+
+ //
+ // Resample checks its users via all_users_simple_format_until_output(), which queries the user(RankChange)'s
+ // preferred format. RankChange (Reshape, Gather, ..) changes the rank, so it queries its Resample dependency's
+ // preferred format, which must not restart the user check and recurse endlessly.
auto& engine = get_test_engine();
auto weights = engine.allocate_memory({data_types::f16, format::bfyx, {512, 384, 1, 1}});
@@ -712,19 +720,25 @@ TEST(reorder_inputs, static_resample_with_rank_changing_reshape_no_recursion) {
resample::InterpolateOp::ShapeCalcMode::SCALES,
resample::InterpolateOp::CoordinateTransformMode::ASYMMETRIC,
resample::InterpolateOp::NearestMode::SIMPLE));
- topology.add(reshape("reshape",
- input_info("resample"),
- false,
- {1},
- {1, 1, 384, 40, 40},
- reshape::reshape_mode::unsqueeze));
+ switch (GetParam()) {
+ case rank_changing_op::reshape:
+ topology.add(reshape("rank_change", input_info("resample"), false, {1}, {1, 1, 384, 40, 40}, reshape::reshape_mode::unsqueeze));
+ break;
+ case rank_changing_op::gather: {
+ auto indices = engine.allocate_memory(layout{ov::PartialShape{1, 1}, data_types::i32, format::bfyx});
+ set_values<int32_t>(indices, {0});
+ topology.add(data("indices", indices));
+ topology.add(gather("rank_change", input_info("resample"), input_info("indices"), 0, 4, ov::Shape{1, 1, 384, 40, 40}));
+ break;
+ }
+ }
topology.add(reshape("skip_reshape",
input_info("skip"),
false,
{1},
{1, 1, 384, 40, 40},
reshape::reshape_mode::unsqueeze));
- topology.add(concatenation("concat", {input_info("reshape"), input_info("skip_reshape")}, 0));
+ topology.add(concatenation("concat", {input_info("rank_change"), input_info("skip_reshape")}, 0));
topology.add(reduce("reduce", input_info("concat"), reduce_mode::sum, {0}, false));
topology.add(convolution("output", input_info("reduce"), "weights", "", 1, {1, 1}, {1, 1}, {0, 0}, {0, 0}, false));
topology.add(reorder("sink", input_info("output"), format::bfyx, data_types::f16));
@@ -740,17 +754,17 @@ TEST(reorder_inputs, static_resample_with_rank_changing_reshape_no_recursion) {
program_wrapper::apply_opt_pass<mark_nodes>(*prog);
ASSERT_TRUE(prog->is_new_shape_infer());
ASSERT_FALSE(prog->get_node("resample").is_dynamic());
- ASSERT_TRUE(prog->get_node("reshape").is_in_data_flow());
- ASSERT_NE(prog->get_node("reshape").get_input_layout(0).get_rank(),
- prog->get_node("reshape").get_output_layout().get_rank());
+ ASSERT_TRUE(prog->get_node("rank_change").is_in_data_flow());
+ ASSERT_NE(prog->get_node("rank_change").get_input_layout(0).get_rank(), prog->get_node("rank_change").get_output_layout().get_rank());
OV_ASSERT_NO_THROW(prog->get_layout_optimizer().get_preferred_format(prog->get_node("resample")));
+ OV_ASSERT_NO_THROW(prog->get_layout_optimizer().get_preferred_format(prog->get_node("rank_change")));
reorder_factory rf;
OV_ASSERT_NO_THROW(program_wrapper::apply_opt_pass<reorder_inputs>(*prog, rf));
ASSERT_EQ(prog->get_node("resample").get_output_layout().format, format::bfyx);
- ASSERT_EQ(prog->get_node("reshape").get_input_layout(0).format, format::bfyx);
- ASSERT_EQ(prog->get_node("reshape").get_output_layout().format, format::bfzyx);
+ ASSERT_EQ(prog->get_node("rank_change").get_input_layout(0).format, format::bfyx);
+ ASSERT_EQ(prog->get_node("rank_change").get_output_layout().format, format::bfzyx);
ASSERT_EQ(prog->get_node("reduce").get_output_layout().format, format::bfyx);
auto blocked_prog = program::build_program(engine, topology, config, false, true);
@@ -759,12 +773,22 @@ TEST(reorder_inputs, static_resample_with_rank_changing_reshape_no_recursion) {
blocked_prog->get_layout_optimizer().set_implementation_forcing(
ov::intel_gpu::ImplForcingMap{{"output", {format::b_fs_yx_fsv16, ""}}});
- // Reshape is a non-recursive plain-format boundary, not a traversal stop. The blocked consumer
+ // The rank-changing user is a traversal pass-through, not a traversal stop. The blocked consumer
// after it must still prevent the Resample from being forced to a plain format.
ASSERT_EQ(blocked_prog->get_layout_optimizer().get_preferred_format(blocked_prog->get_node("resample")),
format::any);
+
+ OV_ASSERT_NO_THROW(prog = program::build_program(engine, topology, config));
+ ASSERT_NE(prog, nullptr);
}
+INSTANTIATE_TEST_SUITE_P(reorder_inputs,
+ static_resample_with_rank_changing_user,
+ ::testing::Values(rank_changing_op::reshape, rank_changing_op::gather),
+ [](const ::testing::TestParamInfo<rank_changing_op>& info) {
+ return info.param == rank_changing_op::reshape ? "reshape" : "gather";
+ });
+
#ifdef ENABLE_ONEDNN_FOR_GPU
TEST(reorder_inputs, has_reshape_user) {
auto& engine = get_test_engine();