Skip to content

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

[GPU] Fix double shift folding in eltw_and_bin post-op with cache (#33315)

David Nambef2aa9b7908
diff --git a/src/plugins/intel_gpu/src/graph/program_node.cpp b/src/plugins/intel_gpu/src/graph/program_node.cpp
index 527d816cac0..35ca510a77a 100644
--- a/src/plugins/intel_gpu/src/graph/program_node.cpp
+++ b/src/plugins/intel_gpu/src/graph/program_node.cpp
@@ -1319,6 +1319,19 @@ dnnl::post_ops program_node::try_optimize_post_ops(std::vector<fused_primitive_d
bool cur_ops_pair_is_optimized = false;
if (can_try_optimize) {
+ auto update_buf_inplace = [&](memory::ptr mem_ptr, size_t elem_count, auto fn) {
+ auto& stream = mem_ptr->get_engine()->get_service_stream();
+ if (mem_ptr->get_allocation_type() != allocation_type::usm_device) {
+ mem_lock<float, mem_lock_type::read_write> lock(mem_ptr, stream);
+ fn(&lock[0], elem_count);
+ } else {
+ std::vector<float> host(elem_count);
+ mem_ptr->copy_to(stream, host.data(), true);
+ fn(host.data(), elem_count);
+ mem_ptr->copy_from(stream, host.data(), true);
+ }
+ };
+
if (eltw_and_eltw) {
dnnl::algorithm cur_alg, prev_alg;
float cur_alpha, prev_alpha, cur_beta, prev_beta;
@@ -1366,8 +1379,7 @@ dnnl::post_ops program_node::try_optimize_post_ops(std::vector<fused_primitive_d
// Eltwise operations can use runtime non-constant data buffers, so check that memory buffers consist of constant data only
auto bin_ops_can_be_optimized =
cur_node.is_type<data>() && cur_node.is_constant() && cur_node.get_users().size() == 1 &&
- desc.get_data_type() == dnnl_f32 &&
- cur_node.as<data>().get_attached_memory_ptr()->get_allocation_type() != allocation_type::usm_device;
+ desc.get_data_type() == dnnl_f32;
auto bin_add_and_eltw = alpha == 1.0f && type_is_binary_add(cur_type) && bin_ops_can_be_optimized;
auto bin_mul_and_eltw = beta == 0.f && type_is_binary_mul(cur_type) && bin_ops_can_be_optimized;
@@ -1376,20 +1388,21 @@ dnnl::post_ops program_node::try_optimize_post_ops(std::vector<fused_primitive_d
memory::ptr cur_bin_mem_ptr = cur_node.as<data>().get_attached_memory_ptr();
if (cur_bin_mem_ptr == nullptr)
throw std::runtime_error("OneDNN post-ops optimization error: nonexistent node for bin + eltw");
- auto& stream = cur_bin_mem_ptr->get_engine()->get_service_stream();
- mem_lock<float, mem_lock_type::read_write> bin_and_eltw_lock(cur_bin_mem_ptr, stream);
-
- size_t cur_bin_mem_size = cur_node.get_output_layout().count();
// Update all binary coefficients
- if (bin_add_and_eltw) {
- for (size_t data_idx = 0; data_idx < cur_bin_mem_size; data_idx++) {
- bin_and_eltw_lock[data_idx] += beta;
- }
- } else {
- for (size_t data_idx = 0; data_idx < cur_bin_mem_size; data_idx++) {
- bin_and_eltw_lock[data_idx] *= alpha;
- }
+ if (!get_program().is_loaded_from_cache()) {
+ size_t cur_bin_mem_size = cur_node.get_output_layout().count();
+ update_buf_inplace(cur_bin_mem_ptr, cur_bin_mem_size, [&](float* buf, size_t n) {
+ if (bin_add_and_eltw) {
+ for (size_t data_idx = 0; data_idx < n; data_idx++) {
+ buf[data_idx] += beta;
+ }
+ } else {
+ for (size_t data_idx = 0; data_idx < n; data_idx++) {
+ buf[data_idx] *= alpha;
+ }
+ }
+ });
}
// Marked previous eltwise operation as 'optimized' (it will be ignored on the next iteration of cycle)
@@ -1410,8 +1423,7 @@ dnnl::post_ops program_node::try_optimize_post_ops(std::vector<fused_primitive_d
// Eltwise operations can use runtime non-constant data buffers, so check that memory buffers consist of constant data only
auto bin_ops_can_be_optimized =
prev_node.is_type<data>() && prev_node.is_constant() && prev_node.get_users().size() == 1 &&
- desc.get_data_type() == dnnl_f32 &&
- prev_node.as<data>().get_attached_memory_ptr()->get_allocation_type() != allocation_type::usm_device;
+ desc.get_data_type() == dnnl_f32;
auto eltw_and_bin_add = alpha == 1.0f && type_is_binary_add(prev_type) && bin_ops_can_be_optimized;
auto eltw_and_bin_mul = beta == 0.f && type_is_binary_mul(prev_type) && bin_ops_can_be_optimized;
@@ -1420,20 +1432,21 @@ dnnl::post_ops program_node::try_optimize_post_ops(std::vector<fused_primitive_d
memory::ptr prev_bin_mem_ptr = prev_node.as<data>().get_attached_memory_ptr();
if (prev_bin_mem_ptr == nullptr)
throw std::runtime_error("OneDNN post-ops optimization error: nonexistent node for eltw + bin");
- auto& stream = prev_bin_mem_ptr->get_engine()->get_service_stream();
- mem_lock<float, mem_lock_type::read_write> eltw_and_bin_lock(prev_bin_mem_ptr, stream);
-
- size_t prev_bin_mem_size = prev_node.get_output_layout().count();
// Update all binary coefficients
- if (eltw_and_bin_add) {
- for (size_t data_idx = 0; data_idx < prev_bin_mem_size; data_idx++) {
- eltw_and_bin_lock[data_idx] += beta;
- }
- } else {
- for (size_t data_idx = 0; data_idx < prev_bin_mem_size; data_idx++) {
- eltw_and_bin_lock[data_idx] *= alpha;
- }
+ if (!get_program().is_loaded_from_cache()) {
+ size_t prev_bin_mem_size = prev_node.get_output_layout().count();
+ update_buf_inplace(prev_bin_mem_ptr, prev_bin_mem_size, [&](float* buf, size_t n) {
+ if (eltw_and_bin_add) {
+ for (size_t data_idx = 0; data_idx < n; data_idx++) {
+ buf[data_idx] += beta;
+ }
+ } else {
+ for (size_t data_idx = 0; data_idx < n; data_idx++) {
+ buf[data_idx] *= alpha;
+ }
+ }
+ });
}
// Marked current eltwise operation as 'optimized' (it will be ignored on the next iteration of cycle)
@@ -1450,19 +1463,19 @@ dnnl::post_ops program_node::try_optimize_post_ops(std::vector<fused_primitive_d
p_ops.get_params_eltwise(cur_post_op_idx, alg, alpha, beta);
// Eltwise can be inserted into the output_scale if cur_beta is equal to 0.f
- if (beta == 0.f && prev_node.get_output_layout().data_type == data_types::f32 &&
- prev_node.as<data>().get_attached_memory_ptr()->get_allocation_type() != allocation_type::usm_device) {
+ if (beta == 0.f && prev_node.get_output_layout().data_type == data_types::f32) {
memory::ptr prev_scale_mem_ptr = prev_node.as<data>().get_attached_memory_ptr();
if (prev_scale_mem_ptr == nullptr)
throw std::runtime_error("OneDNN post-ops optimization error: nonexistent node for eltw + scale");
- auto& stream = prev_scale_mem_ptr->get_engine()->get_service_stream();
- mem_lock<float, mem_lock_type::read_write> eltw_and_scale_lock(prev_scale_mem_ptr, stream);
-
- size_t prev_scale_mem_size = prev_node.get_output_layout().count();
// Update all scale coefficients
- for (size_t data_idx = 0; data_idx < prev_scale_mem_size; data_idx++) {
- eltw_and_scale_lock[data_idx] *= alpha;
+ if (!get_program().is_loaded_from_cache()) {
+ size_t prev_scale_mem_size = prev_node.get_output_layout().count();
+ update_buf_inplace(prev_scale_mem_ptr, prev_scale_mem_size, [&](float* buf, size_t n) {
+ for (size_t data_idx = 0; data_idx < n; data_idx++) {
+ buf[data_idx] *= alpha;
+ }
+ });
}
// Marked current eltwise operation as 'optimized' (it will be ignored on the next iteration of cycle)
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 0e824f90fc6..8016abad578 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
@@ -10966,6 +10966,105 @@ TEST(convolution_gpu_onednn, support_activation_zero_points_for_i32) {
}
}
+TEST(convolution_gpu_onednn, eltw_and_bin_double_shift_in_blob_cache) {
+ auto& engine = get_test_engine();
+ auto stream_ptr = get_test_stream_ptr();
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+
+ const float shift_init = 128.0f;
+ const float in_lo_v = 0.0f;
+ const float in_hi_v = 255.0f;
+ const float out_lo_v = -128.0f;
+ const float out_hi_v = 127.0f;
+ const int levels = 256;
+
+ auto run = [&](bool is_caching_test) -> float {
+ layout in_layout { {1, 64, 56, 56}, data_types::u8, format::bfyx };
+ layout w_layout { {256, 64, 1, 1}, data_types::i8, format::bfyx };
+ layout ch_layout { {1, 256, 1, 1}, data_types::f32, format::bfyx };
+ layout sc_layout { {1, 1, 1, 1}, data_types::f32, format::bfyx };
+
+ auto input_mem = engine.allocate_memory(in_layout);
+ auto weights_mem = engine.allocate_memory(w_layout);
+ auto shift_mem = engine.allocate_memory(ch_layout);
+ auto bin_in_mem = engine.allocate_memory(ch_layout);
+
+ tests::random_generator rg(GET_SUITE_NAME);
+
+ auto input_value = rg.generate_random_4d<uint8_t>(1, 64, 56, 56, 0, 0);
+ auto weights_value = rg.generate_random_4d<int8_t>(256, 64, 1, 1, 1, 1);
+ auto shift_value = rg.generate_random_4d<float>(1, 256, 1, 1, shift_init, shift_init);
+ auto bin_in_value = rg.generate_random_4d<float>(1, 256, 1, 1, 0, 0);
+
+ set_values(input_mem, flatten_4d(format::bfyx, input_value));
+ set_values(weights_mem, flatten_4d(format::bfyx, weights_value));
+ set_values(shift_mem, flatten_4d(format::bfyx, shift_value));
+ set_values(bin_in_mem, flatten_4d(format::bfyx, bin_in_value));
+
+ auto in_lo_mem = engine.allocate_memory(sc_layout);
+ auto in_hi_mem = engine.allocate_memory(sc_layout);
+ auto out_lo_mem = engine.allocate_memory(ch_layout);
+ auto out_hi_mem = engine.allocate_memory(ch_layout);
+
+ set_values(in_lo_mem, std::vector<float>{in_lo_v});
+ set_values(in_hi_mem, std::vector<float>{in_hi_v});
+ set_values(out_lo_mem, std::vector<float>(256, out_lo_v));
+ set_values(out_hi_mem, std::vector<float>(256, out_hi_v));
+
+ topology topo;
+ topo.add(input_layout("input", in_layout));
+ topo.add(input_layout("binary_add_input", ch_layout));
+ topo.add(reorder("reorder_in", input_info("input"), format::b_fs_yx_fsv32, data_types::u8));
+ topo.add(data("weights", weights_mem));
+ topo.add(convolution("conv",
+ input_info("reorder_in"),
+ "weights",
+ "" /*bias*/,
+ 1 /*groups*/,
+ ov::Strides{1, 1},
+ ov::Strides{1, 1},
+ ov::CoordinateDiff{0, 0},
+ ov::CoordinateDiff{0, 0},
+ false /*transposed*/));
+ topo.add(eltwise("add_bin",
+ { input_info("conv"), input_info("binary_add_input") },
+ eltwise_mode::sum));
+ topo.add(data("fq_in_shift", shift_mem));
+ topo.add(eltwise("add_shift",
+ { input_info("add_bin"), input_info("fq_in_shift") },
+ eltwise_mode::sum));
+ topo.add(data("in_lo", in_lo_mem));
+ topo.add(data("in_hi", in_hi_mem));
+ topo.add(data("out_lo", out_lo_mem));
+ topo.add(data("out_hi", out_hi_mem));
+ topo.add(quantize("q",
+ input_info("add_shift"),
+ input_info("in_lo"),
+ input_info("in_hi"),
+ input_info("out_lo"),
+ input_info("out_hi"),
+ levels,
+ data_types::i8));
+ topo.add(reorder("output", input_info("q"), format::bfyx, data_types::f32));
+
+ auto net = get_network(engine, topo, config, stream_ptr, is_caching_test);
+ net->set_input_data("input", input_mem);
+ net->set_input_data("binary_add_input", bin_in_mem);
+
+ auto outputs = net->execute();
+ auto out_vals = get_output_values_to_float(*net, outputs.at("output"), 1);
+
+ return out_vals[0];
+ };
+
+ auto result_no_cache = run(false);
+ auto result_cache = run(true);
+
+ EXPECT_NEAR(result_no_cache, result_cache, 1e-3f);
+}
+
TEST(convolution_gpu_onednn, has_proper_synchronization) {
auto& engine = get_test_engine();
if (!engine.get_device_info().supports_immad)