Skip to content

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

[GPU] Keep refreshing FC shape from a skipped fused-op input (#39271)

hyunback kim4404a421d595
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index 692fefe8ec0..e80dd0769b0 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -1115,10 +1115,30 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {
// Update output layout with respect to FC's fake alignment
auto updated_layouts = actual_layouts;
+ auto is_fused_prim_of_user = [](const primitive_inst* user, const primitive_id& id) -> bool {
+ for (const auto& p : user->get_node().get_fused_primitives()) {
+ if (p.has_outer_dep()) {
+ const auto start_idx = p.outer_dep_start_idx;
+ // exclude fused_node from total_num_deps
+ const auto end_idx = p.outer_dep_start_idx + p.total_num_deps - 1;
+ for (size_t idx = start_idx; idx < end_idx; idx++) {
+ if (user->get_node().get_dependency(idx).id() == id) {
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+ };
// A skipped node takes its input's memory right below and returns, so the sizing would be thrown away; its FC users
- // then run update_shape() themselves.
+ // then run update_shape() themselves. Except an FC user whose shape was already updated by its input[0] producer:
+ // it skips its own update_shape(), so a skipped node that is its fused-op input must still refresh it below.
const bool reuses_input_memory =
- !get_node().is_type<concatenation>() && (get_node().is_runtime_skippable() || get_node().is_type<crop>()) && can_be_optimized();
+ !get_node().is_type<concatenation>() && (get_node().is_runtime_skippable() || get_node().is_type<crop>()) && can_be_optimized() &&
+ std::none_of(get_user_insts().begin(), get_user_insts().end(), [&](const primitive_inst* user) {
+ return user->get_node().is_type<fully_connected>() && user->is_dynamic() && user->_update_shape_done_by_other &&
+ is_fused_prim_of_user(user, id());
+ });
std::vector<cldnn::primitive_inst*> user_insts;
if (!reuses_input_memory) {
const auto& user_insts_origin = get_user_insts();
@@ -1141,25 +1161,10 @@ void primitive_inst::realloc_outputs(bool prev_execution_skipped) {
OPENVINO_ASSERT(user_insts.size() == user_insts_origin.size(), "Should have same size between ", user_insts.size(), " and ", user_insts_origin.size());
}
for (auto* user : user_insts) {
- auto is_fused_prim_of_user = [&](primitive_id id) -> bool {
- for (const auto& p : user->get_node().get_fused_primitives()) {
- if (p.has_outer_dep()) {
- const auto start_idx = p.outer_dep_start_idx;
- // exclude fused_node from total_num_deps
- const auto end_idx = p.outer_dep_start_idx + p.total_num_deps - 1;
- for (size_t idx = start_idx; idx < end_idx; idx++) {
- if (user->get_node().get_dependency(idx).id() == id) {
- return true;
- }
- }
- }
- }
- return false;
- };
// Since fake alignment is applicable for input tensor as well, make sure we allocate enough memory
// to prevent reading beyond the allocated memory bounds
if (user->get_node().is_type<fully_connected>() && user->is_dynamic()) {
- if (user->_deps[0].first == this || (is_fused_prim_of_user(id()) && user->_update_shape_done_by_other)) {
+ if (user->_deps[0].first == this || (is_fused_prim_of_user(user, id()) && user->_update_shape_done_by_other)) {
size_t dep_idx = 0;
for (const auto& dep : user->_deps) {
if (dep.first->id() == id()) {
diff --git a/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp b/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp
index 297c1cd41e9..75f03e05879 100644
--- a/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/dynamic_execution/update_shape_test.cpp
@@ -13,6 +13,11 @@
#include <intel_gpu/primitives/non_zero.hpp>
#include <intel_gpu/primitives/paged_attention.hpp>
#include <intel_gpu/primitives/gather.hpp>
+#include <intel_gpu/primitives/activation.hpp>
+#include <intel_gpu/primitives/eltwise.hpp>
+#include <intel_gpu/primitives/fully_connected.hpp>
+#include <intel_gpu/primitives/reorder.hpp>
+#include <intel_gpu/primitives/reshape.hpp>
#include "program_wrapper.h"
@@ -852,4 +857,70 @@ TEST(update_shape_test, paged_attention_mixed_stage_h256_micro_qtile64_buffers)
ASSERT_EQ(intermediate_mems[4]->size(), 200u * 2 * sizeof(int32_t)) << dump_sizes();
}
+TEST(update_shape_test, fc_fused_eltwise_input_from_optimized_reshape) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_immad)
+ return;
+
+ const int64_t K = 16;
+ const int64_t N = 8;
+ tests::random_generator rg(GET_SUITE_NAME);
+ auto gate_w_data = rg.generate_random_1d<ov::float16>(N * K, -1, 1);
+ auto side_w_data = rg.generate_random_1d<ov::float16>(N * K, -1, 1);
+ auto gate_w = engine.allocate_memory(layout{ov::PartialShape{N, K}, data_types::f16, format::bfyx});
+ auto side_w = engine.allocate_memory(layout{ov::PartialShape{N, K}, data_types::f16, format::bfyx});
+ set_values(gate_w, gate_w_data);
+ set_values(side_w, side_w_data);
+
+ topology topology(input_layout("input", layout{ov::PartialShape{-1, 1, K}, data_types::f16, format::bfyx}),
+ data("gate_w", gate_w),
+ data("side_w", side_w),
+ activation("act", input_info("input"), activation_func::relu),
+ fully_connected("gate", input_info("act"), "gate_w", "", 3),
+ reshape("act_2d", input_info("act"), true, std::vector<int64_t>{-1, K}, ov::PartialShape{-1, K}),
+ fully_connected("side", input_info("act_2d"), "side_w", "", 2),
+ reshape("side_3d", input_info("side"), true, std::vector<int64_t>{-1, 1, N}, ov::PartialShape{-1, 1, N}),
+ eltwise("prod", {input_info("gate"), input_info("side_3d")}, eltwise_mode::prod),
+ reorder("output", input_info("prod"), format::bfyx, 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));
+ network network(engine, topology, config);
+
+ const auto& fc_node = network.get_primitive("output")->get_node();
+ ASSERT_TRUE(fc_node.is_type<fully_connected>());
+ ASSERT_TRUE(std::any_of(fc_node.get_fused_primitives().begin(), fc_node.get_fused_primitives().end(), [](const fused_primitive_desc& fd) {
+ return fd.is_type<eltwise>();
+ })) << "eltwise prod is expected to be fused into gate";
+ ASSERT_TRUE(network.get_primitive("side_3d")->get_node().can_be_optimized());
+
+ for (const int64_t batch : {7, 1, 9}) {
+ auto input_data = rg.generate_random_1d<ov::float16>(batch * K, -1, 1);
+ auto input_mem = engine.allocate_memory(layout{ov::PartialShape{batch, 1, K}, data_types::f16, format::bfyx});
+ set_values(input_mem, input_data);
+ network.set_input_data("input", input_mem);
+
+ std::map<primitive_id, network_output> outputs;
+ OV_ASSERT_NO_THROW(outputs = network.execute());
+ ASSERT_TRUE(network.get_primitive("output")->get_impl()->is_onednn());
+
+ ASSERT_EQ(outputs.at("output").get_layout().get_partial_shape(), (ov::PartialShape{batch, 1, N}));
+ auto output_mem = outputs.at("output").get_memory();
+ cldnn::mem_lock<float> output_ptr(output_mem, get_test_stream());
+ for (int64_t b = 0; b < batch; ++b) {
+ for (int64_t n = 0; n < N; ++n) {
+ float gate = 0.f;
+ float side = 0.f;
+ for (int64_t k = 0; k < K; ++k) {
+ const float act = std::max(0.f, static_cast<float>(input_data[b * K + k]));
+ gate += act * static_cast<float>(gate_w_data[n * K + k]);
+ side += act * static_cast<float>(side_w_data[n * K + k]);
+ }
+ ASSERT_NEAR(output_ptr[b * N + n], gate * side, 5e-2f) << "batch " << batch << ", b " << b << ", n " << n;
+ }
+ }
+ }
+}
+
} // update_shape_test