Skip to content

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

[GPU] fix padding mismatch between layout and attached memory (#33358)

Yazhan Ma0c1062852a60
diff --git a/src/plugins/intel_gpu/src/graph/program.cpp b/src/plugins/intel_gpu/src/graph/program.cpp
index 0ce2abec165..8feb7aaf10a 100644
--- a/src/plugins/intel_gpu/src/graph/program.cpp
+++ b/src/plugins/intel_gpu/src/graph/program.cpp
@@ -697,6 +697,19 @@ void program::transfer_memory_to_device() {
if (!get_engine().supports_allocation(allocation_type::usm_device))
return;
+ auto allocate_and_transfer = [this](typed_program_node<data>& data_node,
+ const layout& target_layout,
+ const memory& mem,
+ allocation_type target_alloc_type) {
+ // Allocate and transfer memory
+ auto device_mem = mem.get_engine()->allocate_memory(target_layout, target_alloc_type, false);
+ device_mem->copy_from(get_stream(), mem);
+ data_node.attach_memory(device_mem);
+ const_cast<memory::ptr&>(data_node.get_primitive()->mem).reset();
+ // TODO: Do we need finish call here? Maybe call it in network::execute() ?
+ get_stream().finish();
+ };
+
for (auto& node : processing_order) {
if (node->is_shape_infer_dep()) {
continue;
@@ -711,27 +724,32 @@ void program::transfer_memory_to_device() {
if (mem_layout.count() == 0)
continue;
+ allocation_type target_alloc_type = alloc_type;
+ // usm_device memory does not provide performance benefits on the LNL platform
+ if ((alloc_type == allocation_type::usm_host || alloc_type == allocation_type::usm_shared) &&
+ !(get_engine().get_device_info().arch >= gpu_arch::xe2 &&
+ get_engine().get_device_info().dev_type == device_type::integrated_gpu)) {
+ // Convert to usm_device for performance optimization
+ target_alloc_type = allocation_type::usm_device;
+ }
+
if (!mem_layout.compatible(data_node_layout)) {
+ if (data_node_layout.data_type == mem_layout.data_type &&
+ data_node_layout.format == mem_layout.format &&
+ data_node_layout.get_shape() == mem_layout.get_shape()) {
+ GPU_DEBUG_LOG << "[" << data_node.id() << ": padding fix]" << std::endl;
+ allocate_and_transfer(data_node, data_node_layout, mem, target_alloc_type);
+ GPU_DEBUG_LOG << "[" << data_node.id() << ": padding fix completed]" << std::endl;
+ continue;
+ }
std::string err_str("Node and memory layouts are incompatible, error occurred for " + node->id() + " node");
throw std::invalid_argument(err_str);
}
- if (alloc_type == allocation_type::usm_host || alloc_type == allocation_type::usm_shared) {
- // usm_device memory does not provide performance benefits on the integrated Xe2+ platforms
- if (get_engine().get_device_info().arch >= gpu_arch::xe2 &&
- get_engine().get_device_info().dev_type == device_type::integrated_gpu) {
- return;
- }
-
+ if (target_alloc_type != alloc_type) {
GPU_DEBUG_LOG << "[" << data_node.id() << ": constant]" << std::endl;
- // Allocate and transfer memory
- auto device_mem = mem.get_engine()->allocate_memory(data_node_layout, allocation_type::usm_device, false);
- device_mem->copy_from(get_stream(), mem);
- data_node.attach_memory(device_mem);
+ allocate_and_transfer(data_node, data_node_layout, mem, target_alloc_type);
GPU_DEBUG_LOG << "[" << data_node.id() << ": constant]" << std::endl;
- const_cast<memory::ptr&>(data_node.get_primitive()->mem).reset();
- // TODO: Do we need finish call here? Maybe call it in network::execute() ?
- get_stream().finish();
}
}
}
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/data_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/data_gpu_test.cpp
index 75f7d1295b4..186d2208a1b 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/data_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/data_gpu_test.cpp
@@ -6,6 +6,7 @@
#include "intel_gpu/runtime/memory_caps.hpp"
#include "test_utils.h"
#include "random_generator.hpp"
+#include "program_wrapper.h"
#include <intel_gpu/primitives/input_layout.hpp>
#include <intel_gpu/primitives/data.hpp>
@@ -109,3 +110,62 @@ TEST(data_gpu, usm_device_buffer) {
ASSERT_EQ(expected_output[i], output_ptr[i]);
}
}
+
+TEST(data_gpu, transfer_memory_padding_mismatch_forced) {
+ auto& engine = get_test_engine();
+
+ if (!engine.get_device_info().has_separate_cache) {
+ GTEST_SKIP() << "Device doesn't have separate cache, test not applicable";
+ }
+
+ if (!engine.supports_allocation(allocation_type::usm_device)) {
+ GTEST_SKIP() << "USM device allocation not supported, skipping test";
+ }
+
+ layout mem_layout_no_pad{data_types::f32, format::bfyx, {1, 1, 1, 91}};
+ auto const_mem = engine.allocate_memory(mem_layout_no_pad, allocation_type::usm_host, false);
+
+ std::vector<float> const_data(mem_layout_no_pad.count(), 1.0f);
+ const_mem->copy_from(get_test_stream(), const_data.data());
+ get_test_stream().finish();
+
+ cldnn::topology topology;
+ topology.add(input_layout("input", layout{data_types::f32, format::bfyx, {1, 1, 1, 91}}));
+ topology.add(data("const_data", const_mem));
+ topology.add(eltwise("output", {input_info("input"), input_info("const_data")}, eltwise_mode::sum));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+
+ auto prog = std::make_shared<program>(engine, topology, config, nullptr, nullptr, false, false, false);
+
+ auto& data_node = prog->get_node("const_data");
+ padding node_padding;
+ node_padding._lower_size = {0, 0, 0, 0};
+ node_padding._upper_size = {0, 16, 0, 1};
+ layout node_layout_with_pad{data_types::f32, format::bfyx, {1, 1, 1, 91}, node_padding};
+ data_node.set_output_layout(node_layout_with_pad, false);
+
+ bool exception_thrown = false;
+ std::string exception_msg;
+
+ try {
+ program_wrapper::init_graph(*prog);
+ auto& prog_config = const_cast<ExecutionConfig&>(prog->get_config());
+ prog_config.finalize(prog->get_engine());
+ prog->get_engine().set_enable_large_allocations(prog_config.get_enable_large_allocations());
+ program_wrapper::pre_optimize_graph(*prog, false);
+ program_wrapper::run_graph_compilation(*prog);
+ program_wrapper::post_optimize_graph(*prog, false);
+ program_wrapper::prepare_memory_dependencies(*prog);
+ program_wrapper::apply_opt_pass<build_implementations>(*prog);
+
+ if (prog->get_engine().get_device_info().has_separate_cache) {
+ program_wrapper::transfer_memory_to_device(*prog);
+ }
+ } catch (const std::invalid_argument& e) {
+ exception_thrown = true;
+ exception_msg = e.what();
+ }
+ ASSERT_FALSE(exception_thrown) << "Padding fix failed! Exception: " << exception_msg;
+}
diff --git a/src/plugins/intel_gpu/tests/unit/test_utils/program_wrapper.h b/src/plugins/intel_gpu/tests/unit/test_utils/program_wrapper.h
index e78d92244aa..9bcbf02f710 100644
--- a/src/plugins/intel_gpu/tests/unit/test_utils/program_wrapper.h
+++ b/src/plugins/intel_gpu/tests/unit/test_utils/program_wrapper.h
@@ -28,6 +28,18 @@ namespace cldnn
{
p.run_graph_compilation();
}
+ static void init_graph(program& p)
+ {
+ p.init_graph();
+ }
+ static void pre_optimize_graph(program& p, bool is_internal)
+ {
+ p.pre_optimize_graph(is_internal);
+ }
+ static void post_optimize_graph(program& p, bool is_internal)
+ {
+ p.post_optimize_graph(is_internal);
+ }
static void build(program& p)
{
program_wrapper::run_graph_compilation(p);
@@ -37,6 +49,10 @@ namespace cldnn
{
p.prepare_memory_dependencies();
}
+ static void transfer_memory_to_device(program& p)
+ {
+ p.transfer_memory_to_device();
+ }
static void update_configs_properties(program& p, const ov::AnyMap& properties) {
auto config_copy = p._config.clone();
config_copy.set_property(properties);