Skip to content

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

[GPU] Extend in-place crop optimization to support batch-axis split with reshape (#34346)

Andrew Kwangwoong Park222c9d434f8a
diff --git a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_buffer_fusing.cpp b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_buffer_fusing.cpp
index c2a0af74db3..e49a2b90922 100644
--- a/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_buffer_fusing.cpp
+++ b/src/plugins/intel_gpu/src/graph/graph_optimizer/prepare_buffer_fusing.cpp
@@ -698,20 +698,25 @@ void crop_in_place_optimization::update_in_place_crop_padding_simple_data_format
auto reshape_mode = reshape_desc->mode;
auto reshape_axis = crop_axis;
if (reshape_mode == reshape::reshape_mode::base) {
- auto reshape_ps = user_info.second.get_partial_shape();
- auto crop_dim_val = crop_layout.get_partial_shape()[crop_axis].get_length();
-
- auto mul = 1;
- reshape_axis = reshape_ps.size() - 1;
- for (size_t i = reshape_ps.size(); i > 1; i--) {
- if (reshape_ps[i - 1].is_dynamic() || mul == crop_dim_val)
- break;
-
- mul *= reshape_ps[i - 1].get_length();
- reshape_axis = i - 1;
+ if (crop_axis == 0 && !crop_layout.get_partial_shape()[0].is_dynamic() &&
+ crop_layout.get_partial_shape()[0].get_length() == 1) {
+ // The crop produces exactly batch=1 per slice.
+ // The reshape absorbs that dim, so the padding axis in the output remains 0.
+ reshape_axis = 0;
+ } else {
+ auto mul = 1;
+ auto reshape_ps = user_info.second.get_partial_shape();
+ reshape_axis = reshape_ps.size() - 1;
+ auto crop_dim_val = crop_layout.get_partial_shape()[crop_axis].get_length();
+ for (size_t i = reshape_ps.size(); i > 1; i--) {
+ if (reshape_ps[i - 1].is_dynamic() || mul == crop_dim_val)
+ break;
+
+ mul *= reshape_ps[i - 1].get_length();
+ reshape_axis = i - 1;
+ }
}
} else if (reshape_mode == reshape::reshape_mode::unsqueeze || reshape_mode == reshape::reshape_mode::squeeze) {
- auto reshape_ps = user_info.second.get_partial_shape();
auto output_pattern = reshape_desc->output_pattern;
for (size_t i = 0; i < output_pattern.size(); i++) {
@@ -754,31 +759,42 @@ void crop_in_place_optimization::update_in_place_crop_padding_simple_data_format
auto reshape_ps = user_info.second.get_partial_shape();
auto crop_dim_val = crop_layout.get_partial_shape()[crop_axis].get_length();
- auto divider = 1;
- auto reshape_axis = reshape_ps.size();
- for (size_t i = reshape_ps.size(); i > 1; i--) {
- const auto& dim_value = reshape_ps[i - 1].get_length();
- if (divider * dim_value == crop_dim_val)
- break;
-
- divider *= dim_value;
- reshape_axis = i - 1;
- }
- reshape_axis -= 1;
-
const auto output_rank = std::max(reshape_ps.size(), static_cast<size_t>(4));
std::vector<ov::Dimension::value_type> reshape_lower_sizes(output_rank, 0);
std::vector<ov::Dimension::value_type> reshape_upper_sizes(output_rank, 0);
padding::DynamicDimsMask reshape_dyn_pad_mask;
- reshape_lower_sizes[reshape_axis] = lower_sizes[crop_axis];
- reshape_upper_sizes[reshape_axis] = upper_sizes[crop_axis];
- reshape_dyn_pad_mask[reshape_axis] = 1;
+ if (crop_axis == 0 && crop_dim_val == 1) {
+ // The crop splits on the batch axis with exactly batch=1 per slice.
+ // The reshape squeezes that batch=1 dim: [1, f, y, x] -> [f, y, x].
+ // Padding offsets are in units of one 4D batch slice (= f*y*x elements),
+ // but the 3D output counts elements at axis 0 directly, so multiply by f.
+ const auto batch_stride_factor = reshape_ps[0].get_length();
+ reshape_lower_sizes[0] = lower_sizes[0] * batch_stride_factor;
+ reshape_upper_sizes[0] = upper_sizes[0] * batch_stride_factor;
+ reshape_dyn_pad_mask[0] = 1;
+ } else {
+ auto divider = 1;
+ auto reshape_axis = reshape_ps.size();
+ for (size_t i = reshape_ps.size(); i > 1; i--) {
+ const auto& dim_value = reshape_ps[i - 1].get_length();
+ if (divider * dim_value == crop_dim_val)
+ break;
+
+ divider *= dim_value;
+ reshape_axis = i - 1;
+ }
+ reshape_axis -= 1;
- if (reshape_lower_sizes[reshape_axis])
- reshape_lower_sizes[reshape_axis] /= divider;
- if (reshape_upper_sizes[reshape_axis])
- reshape_upper_sizes[reshape_axis] /= divider;
+ reshape_lower_sizes[reshape_axis] = lower_sizes[crop_axis];
+ reshape_upper_sizes[reshape_axis] = upper_sizes[crop_axis];
+ reshape_dyn_pad_mask[reshape_axis] = 1;
+
+ if (reshape_lower_sizes[reshape_axis])
+ reshape_lower_sizes[reshape_axis] /= divider;
+ if (reshape_upper_sizes[reshape_axis])
+ reshape_upper_sizes[reshape_axis] /= divider;
+ }
user_info.second.data_padding = padding(reshape_lower_sizes, reshape_upper_sizes, reshape_dyn_pad_mask);
} else {
diff --git a/src/plugins/intel_gpu/src/graph/include/reshape_inst.h b/src/plugins/intel_gpu/src/graph/include/reshape_inst.h
index 4b7f25ac97c..dbeb07ccedf 100644
--- a/src/plugins/intel_gpu/src/graph/include/reshape_inst.h
+++ b/src/plugins/intel_gpu/src/graph/include/reshape_inst.h
@@ -7,8 +7,8 @@
#include "intel_gpu/runtime/tensor_accessor.hpp"
#include "openvino/core/partial_shape.hpp"
#include "crop_inst.h"
-#include "rope_inst.h"
#include "mvn_inst.h"
+#include "vl_sdpa_inst.h"
#include "primitive_inst.h"
#include <string>
@@ -58,12 +58,23 @@ public:
if (!has_outer_padding_offset() && get_users().size() == 1 && get_users().front()->get_preferred_impl_type() == impl_types::onednn)
return false;
- // TODO: If user is RoPE or MVN and dynamic padding exists, ouput padding propagation is not supported in the base mode
- if (get_users().size() == 1 && get_users().front()->is_type<mvn>())
+ // TODO: If user is mvn or vl_sdpa and dynamic padding exists, output padding propagation is not supported in the base mode
+ // vl_sdpa uses raw SVM pointers (CM kernel) without shape_info support, so it cannot apply dynamic padding offsets
+ if (get_users().size() == 1 && (get_users().front()->is_type<mvn>() || get_users().front()->is_type<vl_sdpa>()))
return false;
auto axis = input().as<crop>().get_primitive()->axis;
const auto& input_pshape = input().get_output_layout(false).get_partial_shape();
+
+ // directly to the reshape's leading axis when the cropped batch size is exactly 1.
+ // Keep the same safeguard as for inner-axis crops: if the reshape output pattern is
+ // not known at model loading time, do not enable runtime padding propagation.
+ if (axis == 0 && !input_pshape[0].is_dynamic()) {
+ if (prim->output_pattern.empty())
+ return false;
+ return input_pshape[0].get_length() == 1;
+ }
+
auto input_rank = input_pshape.size();
auto input_last_dim = static_cast<int64_t>(input_rank - 1);
if (axis != input_last_dim || input_pshape[input_last_dim].is_dynamic())
diff --git a/src/plugins/intel_gpu/tests/unit/passes/prepare_buffer_fusing_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/prepare_buffer_fusing_test.cpp
index 0834c80d605..bfdc024bb77 100644
--- a/src/plugins/intel_gpu/tests/unit/passes/prepare_buffer_fusing_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/passes/prepare_buffer_fusing_test.cpp
@@ -1769,3 +1769,76 @@ TEST(prepare_buffer_fusing, disable_reshape_with_feature_upper_padding) {
ASSERT_FALSE(reshape_node.can_be_optimized())
<< "Reshape with feature upper padding should NOT be optimized";
}
+
+TEST(prepare_buffer_fusing, in_place_crop_dynamic_batch_axis_split_with_reshape) {
+ auto& engine = get_test_engine();
+ tests::random_generator rg(GET_SUITE_NAME);
+
+ auto in_layout = layout{ov::PartialShape{3, -1, 2, 4}, data_types::f32, format::bfyx};
+ auto input_mem = engine.allocate_memory({{3, 2, 2, 4}, data_types::f32, format::bfyx});
+ auto axis_mem = engine.allocate_memory({{}, data_types::i64, format::bfyx});
+ auto splits_length_mem = engine.allocate_memory({{3}, data_types::i64, format::bfyx});
+
+ const int64_t axis = 0;
+ const size_t dim_f = 2, dim_y = 2, dim_x = 4;
+ const size_t slice_elems = dim_f * dim_y * dim_x;
+
+ auto input_data = rg.generate_random_1d<float>(input_mem->count(), -1.f, 1.f);
+ set_values(input_mem, input_data);
+ set_values<int64_t>(axis_mem, {axis});
+ set_values<int64_t>(splits_length_mem, {1, 1, 1});
+
+ // reshape [1, dim_f, dim_y, dim_x] → [-1, dim_y, dim_x] (base mode, absorbs static b=1)
+ const std::vector<int64_t> squeeze_pattern = {-1, static_cast<int64_t>(dim_y), static_cast<int64_t>(dim_x)};
+ const ov::PartialShape squeeze_out_shape = {-1, static_cast<int64_t>(dim_y), static_cast<int64_t>(dim_x)};
+
+ cldnn::crop_ngraph_op_mode op_mode = cldnn::crop_ngraph_op_mode::variadic_split;
+ topology topology(
+ input_layout("input", in_layout),
+ data("axis", axis_mem),
+ data("splits_length", splits_length_mem),
+ // Q branch: crop output_idx=0 → [1, dim_f, dim_y, dim_x] → reshape → [-1, dim_y, dim_x]
+ crop("crop_q", {input_info("input"), input_info("axis"), input_info("splits_length")}, cldnn::tensor(1), cldnn::tensor(0), op_mode, 0, axis),
+ reshape("reshape_q", input_info("crop_q"), false, squeeze_pattern, squeeze_out_shape, cldnn::reshape::reshape_mode::base),
+ reorder("output_q", input_info("reshape_q"), format::bfyx, data_types::f32, std::vector<float>(), reorder_mean_mode::subtract, padding(), true),
+ // K branch: crop output_idx=1
+ crop("crop_k", {input_info("input"), input_info("axis"), input_info("splits_length")}, cldnn::tensor(1), cldnn::tensor(0), op_mode, 1, axis),
+ reshape("reshape_k", input_info("crop_k"), false, squeeze_pattern, squeeze_out_shape, cldnn::reshape::reshape_mode::base),
+ reorder("output_k", input_info("reshape_k"), format::bfyx, data_types::f32, std::vector<float>(), reorder_mean_mode::subtract, padding(), true),
+ // V branch: crop output_idx=2
+ crop("crop_v", {input_info("input"), input_info("axis"), input_info("splits_length")}, cldnn::tensor(1), cldnn::tensor(0), op_mode, 2, axis),
+ reshape("reshape_v", input_info("crop_v"), false, squeeze_pattern, squeeze_out_shape, cldnn::reshape::reshape_mode::base),
+ reorder("output_v", input_info("reshape_v"), format::bfyx, data_types::f32, std::vector<float>(), reorder_mean_mode::subtract, padding(), true)
+ );
+
+ auto 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));
+ network network(engine, topology, config);
+ network.set_input_data("input", input_mem);
+
+ auto outputs = network.execute();
+
+ ASSERT_TRUE(network.get_primitive("crop_q")->can_be_optimized());
+ ASSERT_TRUE(network.get_primitive("crop_k")->can_be_optimized());
+ ASSERT_TRUE(network.get_primitive("crop_v")->can_be_optimized());
+
+ auto q_mem = outputs.at("output_q").get_memory();
+ cldnn::mem_lock<float> q_out(q_mem, get_test_stream());
+ auto k_mem = outputs.at("output_k").get_memory();
+ cldnn::mem_lock<float> k_out(k_mem, get_test_stream());
+ auto v_mem = outputs.at("output_v").get_memory();
+ cldnn::mem_lock<float> v_out(v_mem, get_test_stream());
+
+ ASSERT_EQ(q_out.size(), slice_elems);
+ ASSERT_EQ(k_out.size(), slice_elems);
+ ASSERT_EQ(v_out.size(), slice_elems);
+
+ // Each in-place crop+reshape must read the correct slice without data movement
+ for (size_t i = 0; i < slice_elems; i++)
+ ASSERT_FLOAT_EQ(q_out[i], input_data[0 * slice_elems + i]) << "Q mismatch at " << i;
+ for (size_t i = 0; i < slice_elems; i++)
+ ASSERT_FLOAT_EQ(k_out[i], input_data[1 * slice_elems + i]) << "K mismatch at " << i;
+ for (size_t i = 0; i < slice_elems; i++)
+ ASSERT_FLOAT_EQ(v_out[i], input_data[2 * slice_elems + i]) << "V mismatch at " << i;
+}