Skip to content

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

[GPU] Skip async static impl compilation when no shape-agnostic impl exists (#39446)

Yazhan Ma940895eec8cd
diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
index 27790fdcb61..00039a97865 100644
--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp
@@ -1778,6 +1778,7 @@ void primitive_inst::update_impl(bool use_async_compilation) {
if (!get_node().is_type<data>() && (!get_node().is_type<mutable_data>() || !get_node().get_dependencies().empty())) {
#ifdef ENABLE_ONEDNN_FOR_GPU
if (_impls_factory->has(impl_types::onednn)) {
+ // create() modifies attrs_onednn: only one thread may create impls from it, see the async push in get_primitive_impl_for_params.
auto attrs_onednn = std::make_shared<dnnl::primitive_attr>();
std::vector<cldnn::fused_primitive_desc_onednn> fused_desc_onednn;
get_node().create_onednn_primitive_attributes(_impl_params->fused_desc, attrs_onednn, fused_desc_onednn, _impl_params.get());
@@ -3714,8 +3715,37 @@ std::shared_ptr<primitive_impl> ImplementationsFactory::get_primitive_impl_for_p
}
}
- // 2. If async compilation is enabled and dynamic impl exists or shape changed - try to compile asynchronously
- if (compile_async) {
+ std::shared_ptr<primitive_impl> dynamic_impl = nullptr;
+ // 2. Try to find existing dynamic impl which supports given shapes
+ for (auto& impl : m_dynamic_impls_cache) {
+ if (impl->m_manager->support_shapes(params)) {
+ dynamic_impl = impl;
+ break;
+ }
+ }
+
+ // 3. Try to create new shape agnostic impl & cache it
+ // If cached dynamic impls all reject these shapes (e.g. CM for a fused epilogue needing broadcast), the fallback is selected
+ // with the runtime params but created from the node's own params, so it stays shape agnostic for other shapes.
+ // Without any cached dynamic impl, the node may not support dynamic params at all (e.g. a forced conv kernel): keep using the runtime params.
+ if (!dynamic_impl) {
+ const auto node_params = m_dynamic_impls_cache.empty() ? nullptr : node->get_kernel_impl_params();
+ const kernel_impl_params& create_params = node_params ? *node_params : params;
+ dynamic_impl = find_impl(node, params, shape_types::dynamic_shape, node_params.get());
+ if (dynamic_impl && !inst.can_be_optimized()) {
+ dynamic_impl->set_node_params(*node);
+ auto kernels = kernels_cache.compile(create_params, dynamic_impl->get_kernels_source());
+ dynamic_impl->set_kernels(std::move(kernels));
+ if (!node_params || dynamic_impl->is_dynamic()) {
+ m_dynamic_impls_cache.push_back(dynamic_impl);
+ }
+ }
+ }
+
+ // 4. If async compilation is enabled and a dynamic impl serves this shape - compile the static impl asynchronously.
+ // Otherwise step 6 compiles it synchronously, and an async duplicate would run create() concurrently on the same onednn attributes.
+ // A shape-agnostic oneDNN impl would also create() on them in step 3, so revisit this if one is added for an async-eligible type.
+ if (compile_async && dynamic_impl) {
auto& compilation_context = prog.get_compilation_context();
compilation_context.push_task(updated_params, [&inst, &compilation_context, updated_params, find_impl]() {
if (compilation_context.is_stopped()) {
@@ -3744,33 +3774,6 @@ std::shared_ptr<primitive_impl> ImplementationsFactory::get_primitive_impl_for_p
});
}
- std::shared_ptr<primitive_impl> dynamic_impl = nullptr;
- // 3. Try to find existing dynamic impl which supports given shapes
- for (auto& impl : m_dynamic_impls_cache) {
- if (impl->m_manager->support_shapes(params)) {
- dynamic_impl = impl;
- break;
- }
- }
-
- // 4. Try to create new shape agnostic impl & cache it
- // If cached dynamic impls all reject these shapes (e.g. CM for a fused epilogue needing broadcast), the fallback is selected
- // with the runtime params but created from the node's own params, so it stays shape agnostic for other shapes.
- // Without any cached dynamic impl, the node may not support dynamic params at all (e.g. a forced conv kernel): keep using the runtime params.
- if (!dynamic_impl) {
- const auto node_params = m_dynamic_impls_cache.empty() ? nullptr : node->get_kernel_impl_params();
- const kernel_impl_params& create_params = node_params ? *node_params : params;
- dynamic_impl = find_impl(node, params, shape_types::dynamic_shape, node_params.get());
- if (dynamic_impl && !inst.can_be_optimized()) {
- dynamic_impl->set_node_params(*node);
- auto kernels = kernels_cache.compile(create_params, dynamic_impl->get_kernels_source());
- dynamic_impl->set_kernels(std::move(kernels));
- if (!node_params || dynamic_impl->is_dynamic()) {
- m_dynamic_impls_cache.push_back(dynamic_impl);
- }
- }
- }
-
// 5. If we have any dynamic impl, do adjustment for new shape before returning in back
if (dynamic_impl) {
dynamic_impl->update(inst, params);
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp
index c5a11976a64..3d2895ff8b3 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/fully_connected_gpu_test.cpp
@@ -7534,6 +7534,78 @@ TEST(fully_connected_gpu_ternocl_int2_dynamic, non_ternary_zero_point_falls_back
ASSERT_NE(net->get_primitive("fc_prim")->get_impl()->get_kernel_name(), "ternocl_int2");
}
+namespace {
+// Counts the async compilation tasks queued by the implementations factory; the tasks still run on a real context.
+class counting_compilation_context : public ICompilationContext {
+public:
+ explicit counting_compilation_context(std::shared_ptr<ICompilationContext> context) : _context(std::move(context)) {}
+ void push_task(kernel_impl_params key, Task&& task) override {
+ pushed_tasks++;
+ _context->push_task(std::move(key), std::move(task));
+ }
+ void remove_keys(std::vector<kernel_impl_params>&& keys) override { _context->remove_keys(std::move(keys)); }
+ bool is_stopped() override { return _context->is_stopped(); }
+ void cancel() override { _context->cancel(); }
+ void wait_all() override { _context->wait_all(); }
+
+ size_t pushed_tasks = 0;
+
+private:
+ std::shared_ptr<ICompilationContext> _context;
+};
+
+// Runs a dynamic u8 compressed FC (scale + zero point) on several new shapes and returns the number of queued async compilation tasks.
+size_t count_async_compilation_tasks(impl_types fc_impl_type) {
+ auto& engine = get_test_engine();
+ const int64_t ifm_num = 64, ofm_num = 32;
+ auto weights_mem = engine.allocate_memory({{ofm_num, ifm_num}, data_types::u8, format::bfyx});
+ auto scale_mem = engine.allocate_memory({{ofm_num, 1}, data_types::f16, format::bfyx});
+ auto zp_mem = engine.allocate_memory({{ofm_num, 1}, data_types::u8, format::bfyx});
+ set_values(weights_mem, std::vector<uint8_t>(ofm_num * ifm_num, 3));
+ set_values(scale_mem, std::vector<ov::float16>(ofm_num, ov::float16(0.5f)));
+ set_values(zp_mem, std::vector<uint8_t>(ofm_num, 1));
+
+ topology topology(input_layout("input", layout{{-1, ifm_num}, data_types::f16, format::bfyx}),
+ data("weights", weights_mem),
+ data("scale", scale_mem),
+ data("zp", zp_mem),
+ fully_connected("fc_prim", input_info("input"), "weights", "", "scale", "zp", data_types::f16, 2, 2));
+
+ 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));
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{{"fc_prim", {format::bfyx, "", fc_impl_type}}}));
+
+ auto context = std::make_shared<counting_compilation_context>(program::make_compilation_context(config));
+ auto prog = program::build_program(engine, topology, config, program::make_task_executor(config), context);
+ auto net = network::allocate_network(engine, prog);
+ for (int64_t batch : {1, 3, 17, 40}) {
+ auto input_mem = engine.allocate_memory({{batch, ifm_num}, data_types::f16, format::bfyx});
+ set_values(input_mem, std::vector<ov::float16>(batch * ifm_num, ov::float16(1.0f)));
+ net->set_input_data("input", input_mem);
+ net->execute();
+ EXPECT_EQ(net->get_primitive("fc_prim")->get_impl()->is_onednn(), fc_impl_type == impl_types::onednn);
+ }
+ context->wait_all();
+ return context->pushed_tasks;
+}
+} // namespace
+
+#ifdef ENABLE_ONEDNN_FOR_GPU
+// The oneDNN FC has no shape-agnostic impl, so each new shape is compiled synchronously. An async compilation task for the same
+// shape would only duplicate that work and run create() concurrently on the same onednn attributes, which create() modifies.
+TEST(fully_connected_onednn_gpu, dynamic_compressed_no_async_compilation_without_shape_agnostic_impl) {
+ if (!get_test_engine().get_device_info().supports_immad)
+ GTEST_SKIP();
+ ASSERT_EQ(count_async_compilation_tasks(impl_types::onednn), size_t(0));
+}
+#endif
+
+// Async compilation is kept when a shape-agnostic impl serves the new shapes meanwhile.
+TEST(fully_connected_gpu, dynamic_compressed_async_compilation_with_shape_agnostic_impl) {
+ ASSERT_GT(count_async_compilation_tasks(impl_types::ocl), size_t(0));
+}
+
class fully_connected_gpu_u3_validation : public fully_connected_gpu_sub_byte_validation {};
// Validates UINT3 decompression correctness in the reference kernel. u3 is a tightly packed LSB-first bit stream,