Skip to content

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

[GPU] Fix memory dependency hang and reshape bad format problem. (#30929)

hyunback kimff46380901bd
diff --git a/src/plugins/intel_gpu/src/graph/include/pass_manager.h b/src/plugins/intel_gpu/src/graph/include/pass_manager.h
index 0d8dd3b60cd..4569fb4ae60 100644
--- a/src/plugins/intel_gpu/src/graph/include/pass_manager.h
+++ b/src/plugins/intel_gpu/src/graph/include/pass_manager.h
@@ -323,6 +323,12 @@ public:
return;
}
+ // If this dependency is already there, exit early
+ auto mem_deps = node->get_memory_dependencies();
+ if (mem_deps.find(static_cast<uint32_t>(dep->get_unique_id())) != mem_deps.end()) {
+ return;
+ }
+
// LoRA can reuse the memory of the previous node, but not be optimized
// Therefore, the dependency of LoRA must also be the dependency of the current node
if (dep->is_type<lora>()) {
@@ -330,8 +336,8 @@ public:
dep->get_dependency(0).add_memory_dependency(*node);
}
- if ((!dep->can_be_optimized() || !dep->is_runtime_skippable()) && ((node->can_be_optimized() && !node->is_runtime_skippable())
- || !dep->can_be_optimized())) {
+ if ((!dep->can_be_optimized() || !dep->is_runtime_skippable()) &&
+ ((node->can_be_optimized() && !node->is_runtime_skippable()) || !dep->can_be_optimized())) {
node->add_memory_dependency(*dep);
} else {
if (node->is_runtime_skippable() || dep->is_runtime_skippable() || dep->can_be_optimized()) {
diff --git a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
index 31a55a2fbcf..4a4838435b8 100644
--- a/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
+++ b/src/plugins/intel_gpu/src/graph/layout_optimizer.cpp
@@ -1216,15 +1216,15 @@ format layout_optimizer::get_preferred_format(program_node& node) {
node.set_preferred_input_fmt(i, fmt);
} else if (in_lay_rank != out_lay_rank) {
auto fmt = get_preferred_format(node.get_dependency(i));
- // Check if selected format can be adjusted to the required input rank
- // If no, use default fotmat instead
+ // Check if selected format can be adjusted to the required input and output rank.
+ // If no, use default format instead.
+ // There are many primitives such as reshape, gather, eltwise, etc., which have different input and output ranks.
+ // Input fmt should be selected by considering the ranks of both input and output.
+ // For example, when input fmt is 4-rank block format and output rank is 6 or higher,
+ // input fmt should be selected as a default format. (blocked format, ranks higher than 6 are not supported.)
try {
- // 7-dimention and 8-dimention only support plain format
- if (in_lay_rank >= 7 || out_lay_rank >= 7) {
- fmt = format::get_default_format(in_lay_rank);
- } else {
- format::adjust_to_rank(fmt, in_lay_rank);
- }
+ format::adjust_to_rank(fmt, in_lay_rank);
+ format::adjust_to_rank(fmt, out_lay_rank);
} catch (ov::Exception&) {
fmt = format::get_default_format(in_lay_rank);
}
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
index 8555a251c7c..2beba16160d 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
@@ -10454,6 +10454,121 @@ TEST(convolution_gpu_onednn, grouped_runtime_weights) {
ASSERT_EQ(output_layout.get_shape(), ov::Shape({1, 256, 25, 25}));
}
+TEST(convolution_gpu_onednn, dyn_conv4d_reshape6d_pattern) {
+ tests::random_generator rg(GET_SUITE_NAME);
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ return;
+
+ if (!engine.get_device_info().supports_fp16)
+ {
+ std::cout << "[ SKIPPED ] The test is skipped (cl_khr_fp16 is not supported)." << std::endl;
+ ASSERT_EQ(1, 1);
+ return;
+ }
+
+ const int input_x = 8, input_y = 8, input_f = 32, output_f = 32, filter_x = 3, filter_y = 3, groups = 1, batch_num = 1;
+ const uint64_t stride = 1;
+ auto input_data_format = format::bfyx;
+
+ auto input_size = tensor(batch_num, input_f, input_x, input_y);
+ auto input_data = rg.generate_random_4d<ov::float16>(batch_num, input_f, input_y, input_x, -1, 1);
+ auto input_data_bfyx = flatten_4d(format::bfyx, input_data);
+ auto input_mem = engine.allocate_memory({ data_types::f16, format::bfyx, input_size });
+ auto in_layout = layout { ov::PartialShape::dynamic(4), data_types::f16, format::bfyx };;
+ set_values(input_mem, input_data_bfyx);
+
+ auto weights_size = tensor(output_f, input_f, filter_y, filter_x, 1);
+ auto weights_data = rg.generate_random_4d<ov::float16>(output_f, input_f, filter_y, filter_x, -1, 1);
+ auto weights_data_bfyx = flatten_4d(format::bfyx, weights_data);
+ auto weights_mem = engine.allocate_memory({ data_types::f16, format::bfyx, weights_size });
+ set_values(weights_mem, weights_data_bfyx);
+
+ // Will be used to store reference values calculated in branches depending on bias
+ auto expected_result = VVVVF<ov::float16>(batch_num, VVVF<ov::float16>(output_f));
+ topology topology;
+
+ // Calculate reference values
+ for (auto bi = 0; bi < batch_num; ++bi) {
+ for (auto ofi = 0; ofi < output_f; ++ofi) {
+ expected_result[bi][ofi] = reference_convolve(input_data[bi], // input
+ weights_data[ofi], // weights
+ stride, stride, // strides
+ 0, // bias
+ 1, 1, // dilation
+ 0, 0, // input padding
+ 0, 0); // output_padding
+ }
+ }
+
+ topology.add(input_layout("input", in_layout),
+ data("weights_fsv", weights_mem),
+ reorder("input_fsv", input_info("input"), { data_types::f16, input_data_format, input_size }));
+
+ auto conv_fsv = convolution("conv_fsv",
+ input_info("input_fsv"),
+ "weights_fsv",
+ no_bias,
+ groups,
+ { stride, stride },
+ {1, 1},
+ { 0, 0 },
+ { 0, 0 },
+ false);
+ conv_fsv.output_paddings = {padding({ 0, 0, 0, 0 }, 0.f)};
+ topology.add(conv_fsv);
+
+
+ auto const_shape = engine.allocate_memory({ov::PartialShape{6}, data_types::i32, format::bfwzyx});
+ set_values<int32_t>(const_shape, {1, 32, 1, 1, 6, 6});
+ topology.add(data("const", const_shape));
+ auto reshape_prim = reshape("reshape", input_info("conv_fsv"), input_info("const"), false, ov::PartialShape::dynamic(6),
+ cldnn::reshape::reshape_mode::base);
+ topology.add(reshape_prim);
+
+ topology.add(reorder("reorder_bfyx", input_info("reshape"), format::bfwzyx, data_types::f32));
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::allow_new_shape_infer(true));
+ config.set_property(ov::intel_gpu::custom_outputs(std::vector<std::string>{"conv_fsv","reorder_bfyx"}));
+ network network(engine, topology, config);
+
+ network.set_input_data("input", input_mem);
+ auto outputs = network.execute();
+ ASSERT_EQ(outputs.size(), size_t(2));
+ ASSERT_TRUE(outputs.find("conv_fsv") != outputs.end());
+
+ for (auto& p : network.get_primitives_info())
+ std::cerr << p.original_id << " " << p.kernel_id << std::endl;
+
+ auto out_ptr = get_output_values_to_float<float>(network, outputs.find("reorder_bfyx")->second);
+
+ auto output_memory = outputs.at("reorder_bfyx").get_memory();
+ auto out_lay = output_memory->get_layout();
+
+ ASSERT_EQ(out_lay.batch(), expected_result.size());
+ ASSERT_EQ(out_lay.feature(), expected_result[0].size());
+ ASSERT_EQ(out_lay.spatial(1), expected_result[0][0].size());
+ ASSERT_EQ(out_lay.spatial(0), expected_result[0][0][0].size());
+
+ for (int bi = 0; bi < out_lay.batch(); ++bi)
+ for (int ofi = 0; ofi < out_lay.feature(); ++ofi)
+ for (int yi = 0; yi < out_lay.spatial(1); ++yi)
+ for (int xi = 0; xi < out_lay.spatial(0); ++xi) {
+ tensor coords = tensor(batch(bi), feature(ofi), spatial(xi, yi, 0, 0));
+ auto offset = out_lay.get_linear_offset(coords);
+ auto val = out_ptr[offset];
+ auto val_ref = expected_result[bi][ofi][yi][xi];
+ auto equal = are_equal(val_ref, val, 1);
+ if (!equal) {
+ std::cout << "Value at batch: " << bi << ", output_f: " << ofi
+ << ", y: " << yi << ", x: " << xi << " = " << static_cast<float>(val) << std::endl;
+ std::cout << "Reference value at batch: " << bi << ", output_f: " << ofi << ", y: " << yi
+ << ", x: " << xi << " = " << static_cast<float>(val_ref) << std::endl;
+ }
+ ASSERT_TRUE(equal);
+ }
+}
#endif // ENABLE_ONEDNN_FOR_GPU
template <typename T>