Skip to content

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

[GPU] Fix recursive format lookup for Reshape users (#38074)

Andrew Kwangwoong Parked24b4cb73da
diff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
index 5eee260e0c6..4e4be6da6db 100644
--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
@@ -18,7 +18,6 @@
#include "select_inst.h"
#include "condition_inst.h"
#include "strided_slice_inst.h"
-#include <sstream>
#include "gated_mlp_inst.h"
#include "gemm_inst.h"
@@ -1559,7 +1558,9 @@ bool layout_optimizer::all_users_simple_format_until_output(program_node& origin
return false;
}
- if (cur_node.is_in_data_flow() && (cur_node.type() != origin_node.type())) {
+ // 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())) {
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 08649e5596f..8cb1baa7897 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
@@ -22,6 +22,8 @@
#include "concatenation_inst.h"
#include "fully_connected_inst.h"
#include "mvn_inst.h"
+#include "reduce_inst.h"
+#include "resample_inst.h"
#include "pass_manager.h"
#include "to_string_utils.h"
@@ -671,6 +673,87 @@ TEST(reorder_inputs, dynamic_conv_chain_no_throw) {
ASSERT_NE(prog, nullptr);
}
+TEST(reorder_inputs, static_resample_with_rank_changing_reshape_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.
+ auto& engine = get_test_engine();
+ auto weights = engine.allocate_memory({data_types::f16, format::bfyx, {512, 384, 1, 1}});
+
+ topology topology;
+ topology.add(data("weights", weights));
+ topology.add(input_layout("input", layout{{1, 384, 20, 20}, data_types::f16, format::bfyx}));
+ topology.add(input_layout("skip", layout{{1, 384, 40, 40}, data_types::f16, format::bfyx}));
+ topology.add(resample("resample",
+ input_info("input"),
+ std::vector<int64_t>{},
+ std::vector<float>{2.0f, 2.0f},
+ std::vector<int64_t>{2, 3},
+ std::vector<size_t>{0, 0, 0, 0},
+ std::vector<size_t>{0, 0, 0, 0},
+ 0,
+ -0.75f,
+ resample::InterpolateOp::InterpolateMode::NEAREST,
+ 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));
+ 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(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));
+
+ 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));
+
+ program::ptr prog = nullptr;
+ OV_ASSERT_NO_THROW(prog = program::build_program(engine, topology, config, false, true));
+ ASSERT_NE(prog, nullptr);
+
+ 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());
+ OV_ASSERT_NO_THROW(prog->get_layout_optimizer().get_preferred_format(prog->get_node("resample")));
+
+ 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("reduce").get_output_layout().format, format::bfyx);
+
+ auto blocked_prog = program::build_program(engine, topology, config, false, true);
+ ASSERT_NE(blocked_prog, nullptr);
+ program_wrapper::apply_opt_pass<mark_nodes>(*blocked_prog);
+ 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
+ // 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);
+}
+
#ifdef ENABLE_ONEDNN_FOR_GPU
TEST(reorder_inputs, has_reshape_user) {
auto& engine = get_test_engine();