OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Refactor kernels cache and stream (#26752)
Vladimir Paramuzovd0ddbced14f0
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/device.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/device.hppindex 63f0311f675..bf305e787e8 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/device.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/device.hpp@@ -24,6 +24,7 @@ public:virtual bool is_same(const device::ptr other) = 0;float get_gops(cldnn::data_types dt) const;+ bool use_unified_shared_memory() const;virtual ~device() = default;};diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hppindex 79e37d1890b..9638bf5fbf8 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp@@ -6,6 +6,7 @@#include "device.hpp"#include "event.hpp"+#include "kernel.hpp"#include "memory_caps.hpp"#include "memory_pool.hpp"#include "layout.hpp"@@ -149,6 +150,10 @@ public:virtual dnnl::engine& get_onednn_engine() const = 0;#endif+ /// This method is intended to create kernel handle for current engine from handle from arbitrary engine+ /// For instance, source kernel can be compiled using ocl engine, and then we can build L0 kernel object based on that+ virtual kernel::ptr prepare_kernel(const kernel::ptr kernel) const = 0;+/// Factory method which creates engine object with impl configured by @p engine_type/// @param engine_type requested engine type/// @param runtime_type requested execution runtime for the engine. @note some runtime/engine types configurations might be unsupporteddiff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/stream.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/stream.hppindex f2cd18f069e..d990f356333 100644--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/stream.hpp+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/stream.hpp@@ -18,10 +18,36 @@namespace cldnn {+// Possible sync methods for kernels in stream+enum class SyncMethods {+ /* Build dependency graph using events. Each kernel creates proper ze_event which is set as dependency of users+ At this moment it requires multiple retain/release calls for ze_event after each enqueueNDRange+ which is less performant comparing to the barriers version+ */+ events = 0,+ /* Enqueue barriers between dependent kernels. For example consider the following dimond dependency graph:+ kernel_0+ / \+ kernel_1 kernel_2+ \ /+ kernel_3+ In that case we do the following:+ 1. Enqueue kernel_0+ 2. Enqueue barrier (ensures kernel_0 is completed)+ 3. Enqueue kernel_1+ 4. Enqueue kernel_2 (doesn't depend on kernel_1)+ 5. Enqueue barrier (ensures kernel_1 and kernel_2 are completed)+ 6. Enqueue kernel_3+ */+ barriers = 1,+ /* No explicit syncronization is needed. Applicable for in-order queue only */+ none = 2+};+class stream {public:using ptr = std::shared_ptr<stream>;- explicit stream(QueueTypes queue_type) : queue_type(queue_type) {}+ explicit stream(QueueTypes queue_type, SyncMethods sync_method) : m_queue_type(queue_type), m_sync_method(sync_method) {}virtual ~stream() = default;virtual void flush() const = 0;@@ -42,16 +68,18 @@ public:virtual event::ptr create_base_event() = 0;virtual event::ptr aggregate_events(const std::vector<event::ptr>& events, bool group = false, bool is_output = false);- QueueTypes get_queue_type() const { return queue_type; }+ QueueTypes get_queue_type() const { return m_queue_type; }static QueueTypes detect_queue_type(engine_types engine_type, void* queue_handle);+ static SyncMethods get_expected_sync_method(const ExecutionConfig& config);#ifdef ENABLE_ONEDNN_FOR_GPUvirtual dnnl::stream& get_onednn_stream() = 0;#endifprotected:- QueueTypes queue_type;+ QueueTypes m_queue_type;+ SyncMethods m_sync_method;};} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/graph/impls/ocl/kernel_selector_helper.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/kernel_selector_helper.cppindex 762118ec5b1..fbf885ff3dd 100644--- a/src/plugins/intel_gpu/src/graph/impls/ocl/kernel_selector_helper.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/kernel_selector_helper.cpp@@ -43,7 +43,7 @@#include "kernel_selector/kernels/quantize/quantize_kernel_params.h"#include "kernel_selector/kernels/reorder/reorder_kernel_base.h"-#include "runtime/kernels_cache.hpp"+#include "impls/ocl/kernels_cache.hpp"#include <string>#include <type_traits>diff --git a/src/plugins/intel_gpu/src/runtime/kernels_cache.cpp b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cppsimilarity index 90%rename from src/plugins/intel_gpu/src/runtime/kernels_cache.cpprename to src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cppindex d6dcaf5dd72..5db452dcda2 100644--- a/src/plugins/intel_gpu/src/runtime/kernels_cache.cpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.cpp@@ -2,19 +2,23 @@// SPDX-License-Identifier: Apache-2.0//-#include "kernels_factory.hpp"#include "kernels_cache.hpp"-#include "ocl/ocl_kernel.hpp"-#include "ocl/ocl_engine.hpp"-#include "ocl/ocl_common.hpp"++#include "openvino/util/pp.hpp"#include "intel_gpu/graph/serialization/set_serializer.hpp"#include "intel_gpu/graph/serialization/vector_serializer.hpp"#include "intel_gpu/graph/serialization/map_serializer.hpp"#include "intel_gpu/graph/serialization/string_serializer.hpp"+#include "intel_gpu/graph/program.hpp"++#include "intel_gpu/runtime/utils.hpp"#include "intel_gpu/runtime/debug_configuration.hpp"#include "intel_gpu/runtime/itt.hpp"#include "intel_gpu/runtime/file_util.hpp"-#include "openvino/util/pp.hpp"++#include "ocl/ocl_kernel.hpp"+#include "ocl/ocl_common.hpp"+#include "ocl/ocl_device.hpp"#ifdef WIN32#include <sdkddkver.h>@@ -23,7 +27,6 @@#endif#endif-#include <algorithm>#include <cassert>#include <sstream>#include <fstream>@@ -47,6 +50,26 @@namespace {std::mutex cacheAccessMutex;+static const cldnn::device::ptr get_target_device(const cldnn::engine& engine) {+ using namespace cldnn;+ if (engine.runtime_type() == runtime_types::ocl) {+ return engine.get_device();+ } else {+ ocl::ocl_device_detector detector;+ auto device_map = detector.get_available_devices(nullptr, nullptr);+ auto original_device = engine.get_device();++ for (auto& d : device_map) {+ const auto& target_uuid = d.second->get_info().uuid;+ const auto& original_uuid = original_device->get_info().uuid;+ if (target_uuid.uuid == original_uuid.uuid)+ return d.second;+ }+ }++ OPENVINO_THROW("[GPU] Couldn't find target device for kernels cache");+}+#ifdef ENABLE_ONEDNN_FOR_GPUcl::Program fuse_microkernels(const cl::Context& context, const cl::Device& device, cl::Program& program, const std::string& code) {using namespace dnnl::impl::gpu::intel;@@ -212,8 +235,8 @@ void kernels_cache::get_program_source(const kernels_code& kernels_source_code,auto options = c.first;auto& batches = std::get<1>(c.second);for (auto& b : batches) {- std::string full_code = options + " " + _engine.get_device_info().driver_version;- full_code += _engine.get_device_info().dev_name;+ std::string full_code = options + " " + _device->get_info().driver_version;+ full_code += _device->get_info().dev_name;for (auto& ss : b.source)full_code += ss;@@ -248,7 +271,7 @@ kernels_cache::kernels_cache(engine& engine,uint32_t prog_id,std::shared_ptr<ov::threading::ITaskExecutor> task_executor,const std::map<std::string, std::string>& batch_headers)- : _engine(engine)+ : _device(get_target_device(engine)), _task_executor(task_executor), _config(config), _prog_id(prog_id)@@ -271,10 +294,10 @@ static std::vector<unsigned char> getProgramBinaries(cl::Program program) {}// TODO: This build_batch method should be backend specific-void kernels_cache::build_batch(const engine& build_engine, const batch_program& batch, compiled_kernels& compiled_kernels) {+void kernels_cache::build_batch(const batch_program& batch, compiled_kernels& compiled_kernels) {OV_ITT_SCOPED_TASK(ov::intel_gpu::itt::domains::intel_gpu_plugin, "KernelsCache::build_batch");- auto& cl_build_engine = dynamic_cast<const ocl::ocl_engine&>(build_engine);+ auto& cl_build_device = dynamic_cast<const ocl::ocl_device&>(*_device);bool dump_sources = batch.dump_custom_program;std::string dump_sources_dir = "";@@ -325,10 +348,10 @@ void kernels_cache::build_batch(const engine& build_engine, const batch_program&// Run compilationif (precompiled_kernels.empty()) {- cl::Program program(cl_build_engine.get_cl_context(), batch.source);+ cl::Program program(cl_build_device.get_context(), batch.source);{OV_ITT_SCOPED_TASK(ov::intel_gpu::itt::domains::intel_gpu_plugin, "KernelsCache::BuildProgram::RunCompilation");- if (program.build({cl_build_engine.get_cl_device()}, batch.options.c_str()) != CL_SUCCESS)+ if (program.build({cl_build_device.get_device()}, batch.options.c_str()) != CL_SUCCESS)throw std::runtime_error("Failed in building program.");}@@ -344,7 +367,7 @@ void kernels_cache::build_batch(const engine& build_engine, const batch_program&#ifdef ENABLE_ONEDNN_FOR_GPUOPENVINO_ASSERT(batch.kernels_counter == 1);// Do we need full source code here (with batch headers)?- program = fuse_microkernels(cl_build_engine.get_cl_context(), cl_build_engine.get_cl_device(), program, batch.source.back());+ program = fuse_microkernels(cl_build_device.get_context(), cl_build_device.get_device(), program, batch.source.back());#else // ENABLE_ONEDNN_FOR_GPUOPENVINO_THROW("[GPU] Can't compile kernel w/ microkernels as onednn is not available");#endif // ENABLE_ONEDNN_FOR_GPU@@ -362,8 +385,8 @@ void kernels_cache::build_batch(const engine& build_engine, const batch_program&ov::intel_gpu::save_binary(cached_bin_name, getProgramBinaries(program));}} else {- cl::Program program(cl_build_engine.get_cl_context(), {cl_build_engine.get_cl_device()}, precompiled_kernels);- if (program.build({cl_build_engine.get_cl_device()}, batch.options.c_str()) != CL_SUCCESS)+ cl::Program program(cl_build_device.get_context(), {cl_build_device.get_device()}, precompiled_kernels);+ if (program.build({cl_build_device.get_device()}, batch.options.c_str()) != CL_SUCCESS)throw std::runtime_error("Failed in building program with a precompiled kernel.");program.createKernels(&kernels);@@ -375,9 +398,8 @@ void kernels_cache::build_batch(const engine& build_engine, const batch_program&const auto& entry_point = k.getInfo<CL_KERNEL_FUNCTION_NAME>();const auto& iter = batch.entry_point_to_id.find(entry_point);if (iter != batch.entry_point_to_id.end()) {- cl_kernel kern = k.get();- cl_context context = cl_build_engine.get_cl_context().get();- kernel::ptr kernel = kernels_factory::create(_engine, context, kern, entry_point);+ kernel::ptr kernel = std::make_shared<ocl::ocl_kernel>(ocl::ocl_kernel_type(k, cl_build_device.get_usm_helper()), entry_point);+auto& params = iter->second.first;auto kernel_part_idx = iter->second.second;if (compiled_kernels.find(params) != compiled_kernels.end()) {@@ -437,7 +459,7 @@ kernel::ptr kernels_cache::get_kernel_from_cached_kernels(std::string id) constreturn res->second->clone(_reuse_kernels);}-std::vector<kernel::ptr> kernels_cache::get_kernels(kernel_impl_params params) const {+std::vector<kernel::ptr> kernels_cache::get_kernels(const kernel_impl_params& params) const {OPENVINO_ASSERT((_pending_compilation == false), "Kernel cache is not compiled, call build_all() first!");std::string current_node_id;@@ -448,11 +470,13 @@ std::vector<kernel::ptr> kernels_cache::get_kernels(kernel_impl_params params) cOPENVINO_ASSERT(_kernels.end() != res, "Kernel for {" + current_node_id + "} is not found in the kernel cache!");OPENVINO_ASSERT(res->second.size() != 0, "Number of kernels should not be zero for " + current_node_id);+ auto& engine = params.get_program().get_engine();+std::vector<kernel::ptr> kernels(res->second.size());for (auto& k : res->second) {auto& kernel_ptr = k.first;auto kernel_part_idx = k.second;- kernels[kernel_part_idx] = kernel_ptr->clone(_reuse_kernels);+ kernels[kernel_part_idx] = engine.prepare_kernel(kernel_ptr->clone(_reuse_kernels));}return kernels;}@@ -461,7 +485,7 @@ bool kernels_cache::validate_simple_kernel_execution(kernel::ptr krl) {auto casted = downcast<ocl::ocl_kernel>(krl.get());auto kernel = casted->get_handle();try {- auto casted_dev = dynamic_cast<ocl::ocl_device*>(_engine.get_device().get());+ auto casted_dev = dynamic_cast<ocl::ocl_device*>(_device.get());auto device = casted_dev->get_device();cl::Context ctx(device);@@ -496,7 +520,6 @@ void kernels_cache::build_all() {if (!_pending_compilation)return;- ocl::ocl_engine& _build_engine = downcast<ocl::ocl_engine>(_engine);std::vector<batch_program> batches;{std::lock_guard<std::mutex> lock(_mutex);@@ -518,9 +541,9 @@ void kernels_cache::build_all() {std::vector<ov::threading::Task> tasks;for (size_t idx = 0; idx < batches.size(); idx++) {auto& batch = batches[idx];- tasks.push_back([this, &_build_engine, &batch, &exception] {+ tasks.push_back([this, &batch, &exception] {try {- build_batch(_build_engine, batch, _kernels);+ build_batch(batch, _kernels);} catch (...) {exception = std::current_exception();}@@ -534,7 +557,7 @@ void kernels_cache::build_all() {}} else {for (size_t idx = 0; idx < batches.size(); idx++) {- build_batch(_build_engine, batches[idx], _kernels);+ build_batch(batches[idx], _kernels);}}@@ -620,8 +643,6 @@ void kernels_cache::add_to_cached_kernels(const std::vector<kernel::ptr>& kernel}void kernels_cache::save(BinaryOutputBuffer& ob) const {- OPENVINO_ASSERT(_engine.type() == engine_types::ocl || _engine.type() == engine_types::sycl, "[GPU] Not supported engine type");-ob << _cached_binaries.size();for (auto& cached_binary : _cached_binaries) {ob << cached_binary.second;@@ -630,8 +651,6 @@ void kernels_cache::save(BinaryOutputBuffer& ob) const {}void kernels_cache::load(BinaryInputBuffer& ib) {- OPENVINO_ASSERT(_engine.type() == engine_types::ocl || _engine.type() == engine_types::sycl, "[GPU] Not supported engine type");-std::unordered_map<uint32_t, std::vector<unsigned char>> precompiled_kernels;size_t num_cached_binaries;@@ -642,8 +661,7 @@ void kernels_cache::load(BinaryInputBuffer& ib) {ib >> precompiled_kernels[id];}- std::unique_ptr<ocl::ocl_engine> build_engine =- cldnn::make_unique<ocl::ocl_engine>(_engine.get_device(), runtime_types::ocl);+ const auto& build_device = downcast<ocl::ocl_device>(*_device);try {std::lock_guard<std::mutex> lock(_mutex);@@ -651,8 +669,8 @@ void kernels_cache::load(BinaryInputBuffer& ib) {for (auto& precompiled_kernel : precompiled_kernels) {cl::vector<cl::Kernel> kernels;- cl::Program program(build_engine->get_cl_context(), {build_engine->get_cl_device()}, {precompiled_kernel.second});- program.build({build_engine->get_cl_device()});+ cl::Program program(build_device.get_context(), {build_device.get_device()}, {precompiled_kernel.second});+ program.build({build_device.get_device()});program.createKernels(&kernels);for (auto& k : kernels) {@@ -660,10 +678,7 @@ void kernels_cache::load(BinaryInputBuffer& ib) {std::string cached_kernel_id = entry_point + "@" + std::to_string(precompiled_kernel.first);const auto& iter = _cached_kernels.find(cached_kernel_id);if (iter == _cached_kernels.end()) {- cl_kernel cl_kernel = k.get();- cl_context cl_context = build_engine->get_cl_context().get();- kernel::ptr kernel = kernels_factory::create(_engine, cl_context, cl_kernel, entry_point);- _cached_kernels[cached_kernel_id] = kernel;+ _cached_kernels[cached_kernel_id] = std::make_shared<ocl::ocl_kernel>(ocl::ocl_kernel_type(k, build_device.get_usm_helper()), entry_point);}}}@@ -690,8 +705,6 @@ kernels_cache::compiled_kernels kernels_cache::compile(const kernel_impl_params&t_kernels_code.insert({params, {kernel_sources, params, dump_custom_program}});}- ocl::ocl_engine& _build_engine = downcast<ocl::ocl_engine>(_engine);-// Create batchesstd::vector<batch_program> batches;get_program_source(t_kernels_code, &batches);@@ -699,7 +712,7 @@ kernels_cache::compiled_kernels kernels_cache::compile(const kernel_impl_params&compiled_kernels output_kernels;// Build batchesfor (size_t idx = 0; idx < batches.size(); ++idx) {- build_batch(_build_engine, batches[idx], output_kernels);+ build_batch(batches[idx], output_kernels);}OPENVINO_ASSERT(output_kernels.size() == 1, "Only the kernels of the single primitive should be compiled.");diff --git a/src/plugins/intel_gpu/src/runtime/kernels_cache.hpp b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.hppsimilarity index 93%rename from src/plugins/intel_gpu/src/runtime/kernels_cache.hpprename to src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.hppindex f505dcbb94d..b08b087c558 100644--- a/src/plugins/intel_gpu/src/runtime/kernels_cache.hpp+++ b/src/plugins/intel_gpu/src/graph/impls/ocl/kernels_cache.hpp@@ -5,7 +5,7 @@#pragma once#include "intel_gpu/graph/serialization/binary_buffer.hpp"-#include "intel_gpu/runtime/engine.hpp"+#include "intel_gpu/runtime/device.hpp"#include "intel_gpu/runtime/kernel.hpp"#include "intel_gpu/runtime/execution_config.hpp"#include "intel_gpu/graph/kernel_impl_params.hpp"@@ -16,11 +16,9 @@#include <memory>#include <atomic>#include <string>-#include <set>-#include "openvino/runtime/threading/cpu_streams_executor.hpp"-#include "kernels_factory.hpp"-#include "ocl/ocl_engine.hpp"+#include "openvino/runtime/threading/itask_executor.hpp"+namespace cldnn {@@ -89,7 +87,7 @@ public:private:static std::mutex _mutex;- engine& _engine;+ const device::ptr _device;std::shared_ptr<ov::threading::ITaskExecutor> _task_executor;ExecutionConfig _config;uint32_t _prog_id = 0;@@ -101,7 +99,7 @@ private:std::map<std::string, std::string> batch_headers;std::unordered_map<kernel_impl_params, size_t, impl_hasher> _kernel_batch_hash;void get_program_source(const kernels_code& kernels_source_code, std::vector<batch_program>*) const;- void build_batch(const engine& build_engine, const batch_program& batch, compiled_kernels& compiled_kernels);+ void build_batch(const batch_program& batch, compiled_kernels& compiled_kernels);std::string get_cache_path() const;bool is_cache_enabled() const;@@ -116,7 +114,7 @@ public:std::shared_ptr<ov::threading::ITaskExecutor> task_executor = nullptr,const std::map<std::string, std::string>& batch_headers = {});kernel::ptr get_kernel_from_cached_kernels(std::string id) const;- std::vector<kernel::ptr> get_kernels(kernel_impl_params params) const;+ std::vector<kernel::ptr> get_kernels(const kernel_impl_params& params) const;void set_kernels_reuse(bool reuse_kernels) { _reuse_kernels = reuse_kernels; }bool get_kernels_reuse() const { return _reuse_kernels; }diff --git a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h b/src/plugins/intel_gpu/src/graph/include/primitive_inst.hindex df418e0c920..33571db8496 100644--- a/src/plugins/intel_gpu/src/graph/include/primitive_inst.h+++ b/src/plugins/intel_gpu/src/graph/include/primitive_inst.h@@ -21,7 +21,7 @@#include "intel_gpu/graph/serialization/layout_serializer.hpp"#include "intel_gpu/graph/serialization/vector_serializer.hpp"#include "intel_gpu/runtime/itt.hpp"-#include "runtime/kernels_cache.hpp"+#include "impls/ocl/kernels_cache.hpp"// TODO: add generic interface for weights_reorder_params and get rid of this dependency#include "impls/ocl/kernel_selector_helper.h"diff --git a/src/plugins/intel_gpu/src/graph/network.cpp b/src/plugins/intel_gpu/src/graph/network.cppindex e457ff95433..59f3647f987 100644--- a/src/plugins/intel_gpu/src/graph/network.cpp+++ b/src/plugins/intel_gpu/src/graph/network.cpp@@ -38,12 +38,8 @@#include "kv_cache_inst.h"#include "program_helpers.h"#include "to_string_utils.h"-#include "kernels_cache.hpp"#include "program_dump_graph.h"-// TODO: Remove once we have an abstraction for kernels_cache-#include "kernel_base.h"-#include <algorithm>#include <string>#include <vector>diff --git a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp b/src/plugins/intel_gpu/src/graph/primitive_inst.cppindex 8f05de55643..b0f7c4db565 100644--- a/src/plugins/intel_gpu/src/graph/primitive_inst.cpp+++ b/src/plugins/intel_gpu/src/graph/primitive_inst.cpp@@ -49,7 +49,6 @@#include "json_object.h"#include <string>-#include <stack>#include <vector>#include <memory>#include <algorithm>diff --git a/src/plugins/intel_gpu/src/graph/program.cpp b/src/plugins/intel_gpu/src/graph/program.cppindex f673e4c81c8..03cc8df8b43 100644--- a/src/plugins/intel_gpu/src/graph/program.cpp+++ b/src/plugins/intel_gpu/src/graph/program.cpp@@ -71,7 +71,7 @@#include "to_string_utils.h"// TODO: Remove once we have interface for kernels cache-#include "runtime/kernels_cache.hpp"+#include "impls/ocl/kernels_cache.hpp"// TODO: implement self-registration for impls#include "impls/ocl/register.hpp"diff --git a/src/plugins/intel_gpu/src/runtime/device.cpp b/src/plugins/intel_gpu/src/runtime/device.cppindex 01f6f586234..fa027ebe9e2 100644--- a/src/plugins/intel_gpu/src/runtime/device.cpp+++ b/src/plugins/intel_gpu/src/runtime/device.cpp@@ -3,6 +3,7 @@//#include "intel_gpu/runtime/device.hpp"+#include "intel_gpu/runtime/debug_configuration.hpp"namespace cldnn {@@ -63,4 +64,15 @@ float device::get_gops(cldnn::data_types dt) const {return freqGHz * opsPerComputeBlock * computeBlockIPC * numEUs;}+bool device::use_unified_shared_memory() const {+ GPU_DEBUG_GET_INSTANCE(debug_config);+ GPU_DEBUG_IF(debug_config->disable_usm) {+ return false;+ }+ if (get_mem_caps().supports_usm()) {+ return true;+ }+ return false;+}+} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/runtime/kernels_factory.cpp b/src/plugins/intel_gpu/src/runtime/kernels_factory.cppdeleted file mode 100644index 448afa86360..00000000000--- a/src/plugins/intel_gpu/src/runtime/kernels_factory.cpp+++ /dev/null@@ -1,21 +0,0 @@-// Copyright (C) 2018-2024 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#include "kernels_factory.hpp"-#include "ocl/ocl_kernels_factory.hpp"--namespace cldnn {-namespace kernels_factory {--std::shared_ptr<kernel> create(engine& engine, cl_context context, cl_kernel kernel, std::string entry_point) {- switch (engine.type()) {- case engine_types::sycl:- case engine_types::ocl:- return ocl::create_ocl_kernel(engine, context, kernel, entry_point);- default: throw std::runtime_error("Unsupported engine type in kernels_factory::create");- }-}--} // namespace kernels_factory-} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/runtime/kernels_factory.hpp b/src/plugins/intel_gpu/src/runtime/kernels_factory.hppdeleted file mode 100644index 0ba1cf6a568..00000000000--- a/src/plugins/intel_gpu/src/runtime/kernels_factory.hpp+++ /dev/null@@ -1,22 +0,0 @@-// Copyright (C) 2018-2024 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#pragma once--#include "intel_gpu/runtime/kernel.hpp"-#include "intel_gpu/runtime/engine.hpp"-#include "ocl/ocl_common.hpp"--#include <memory>--namespace cldnn {--namespace kernels_factory {--// Creates instance of kernel for selected engine type.-// For ocl engine it creates a copy of kernel object-std::shared_ptr<kernel> create(engine& engine, cl_context context, cl_kernel kernel, kernel_id kernel_id);--} // namespace kernels_factory-} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cppindex 74d9b033c64..be8b48b157b 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp@@ -369,7 +369,9 @@ ocl_device::ocl_device(const cl::Device dev, const cl::Context& ctx, const cl::P, _device(dev), _platform(platform), _info(init_device_info(dev, ctx))-, _mem_caps(init_memory_caps(dev, _info)) { }+, _mem_caps(init_memory_caps(dev, _info))+, _usm_helper(new cl::UsmHelper(_context, _device, use_unified_shared_memory())) {+}bool ocl_device::is_same(const device::ptr other) {auto casted = downcast<ocl_device>(other.get());diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hppindex 24102b91a19..ac6081fc480 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hpp@@ -21,6 +21,7 @@ public:cl::Device& get_device() { return _device; }const cl::Context& get_context() const { return _context; }const cl::Platform& get_platform() const { return _platform; }+ const cl::UsmHelper& get_usm_helper() const { return *_usm_helper; }bool is_same(const device::ptr other) override;@@ -32,6 +33,7 @@ private:cl::Platform _platform;device_info _info;memory_capabilities _mem_caps;+ std::unique_ptr<cl::UsmHelper> _usm_helper;};} // namespace ocldiff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cppindex 4de85d27c06..df1cad281d6 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp@@ -3,6 +3,8 @@//#include "ocl_engine.hpp"+#include "intel_gpu/runtime/utils.hpp"+#include "ocl/ocl_kernel.hpp"#include "ocl_common.hpp"#include "ocl_memory.hpp"#include "ocl_stream.hpp"@@ -51,7 +53,6 @@ ocl_engine::ocl_engine(const device::ptr dev, runtime_types runtime_type)OPENVINO_ASSERT(casted, "[GPU] Invalid device type passed to ocl engine");casted->get_device().getInfo(CL_DEVICE_EXTENSIONS, &_extensions);- _usm_helper.reset(new cl::UsmHelper(get_cl_context(), get_cl_device(), use_unified_shared_memory()));_service_stream.reset(new ocl_stream(*this, ExecutionConfig()));}@@ -117,7 +118,9 @@ const cl::Device& ocl_engine::get_cl_device() const {}const cl::UsmHelper& ocl_engine::get_usm_helper() const {- return *_usm_helper;+ auto cl_device = std::dynamic_pointer_cast<ocl_device>(_device);+ OPENVINO_ASSERT(cl_device, "[GPU] Invalid device type for ocl_engine");+ return cl_device->get_usm_helper();}allocation_type ocl_engine::detect_usm_allocation_type(const void* memory) const {@@ -299,6 +302,11 @@ void* ocl_engine::get_user_context() const {return static_cast<void*>(cl_device.get_context().get());}+kernel::ptr ocl_engine::prepare_kernel(const kernel::ptr kernel) const {+ OPENVINO_ASSERT(downcast<const ocl::ocl_kernel>(kernel.get()) != nullptr);+ return kernel;+}+bool ocl_engine::extension_supported(std::string extension) const {return _extensions.find(extension) != std::string::npos;}diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hppindex ee76fcca82a..2f6541bf0e7 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp@@ -45,6 +45,8 @@ public:stream_ptr create_stream(const ExecutionConfig& config, void *handle) const override;stream& get_service_stream() const override;+ kernel::ptr prepare_kernel(const kernel::ptr kernel) const override;+#ifdef ENABLE_ONEDNN_FOR_GPUvoid create_onednn_engine(const ExecutionConfig& config) override;// Returns onednn engine object which shares device and context with current engine@@ -56,7 +58,6 @@ public:private:std::string _extensions;std::unique_ptr<stream> _service_stream;- std::unique_ptr<cl::UsmHelper> _usm_helper;#ifdef ENABLE_ONEDNN_FOR_GPUstd::mutex onednn_mutex;diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_kernels_factory.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_kernels_factory.cppdeleted file mode 100644index 926070075ca..00000000000--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_kernels_factory.cpp+++ /dev/null@@ -1,23 +0,0 @@-// Copyright (C) 2018-2024 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#include "ocl_kernel.hpp"-#include "ocl_engine.hpp"-#include "ocl_kernels_factory.hpp"--#include <memory>-#include <vector>--namespace cldnn {-namespace ocl {--std::shared_ptr<kernel> create_ocl_kernel(engine& engine, cl_context /* context */, cl_kernel kernel, std::string entry_point) {- // Retain kernel to keep it valid- cl::Kernel k(kernel, true);- ocl_engine& cl_engine = dynamic_cast<ocl_engine&>(engine);- return std::make_shared<ocl::ocl_kernel>(ocl::ocl_kernel_type(k, cl_engine.get_usm_helper()), entry_point);-}--} // namespace ocl-} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_kernels_factory.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_kernels_factory.hppdeleted file mode 100644index 474381b3a3a..00000000000--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_kernels_factory.hpp+++ /dev/null@@ -1,16 +0,0 @@-// Copyright (C) 2018-2024 Intel Corporation-// SPDX-License-Identifier: Apache-2.0-//--#include "kernels_factory.hpp"--#include <memory>-#include <string>--namespace cldnn {-namespace ocl {--std::shared_ptr<kernel> create_ocl_kernel(engine& engine, cl_context /* context */, cl_kernel kernel, std::string entry_point);--} // namespace ocl-} // namespace cldnndiff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cppindex be734208c7a..dba2bf4132a 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.cpp@@ -1,22 +1,17 @@-// Copyright (C) 2019-2022 Intel Corporation+// Copyright (C) 2019-2024 Intel Corporation// SPDX-License-Identifier: Apache-2.0//#include "ocl_stream.hpp"+#include "intel_gpu/runtime/stream.hpp"#include "ocl_event.hpp"#include "ocl_user_event.hpp"#include "ocl_command_queues_builder.hpp"-#include "intel_gpu/plugin/common_utils.hpp"#include "intel_gpu/runtime/debug_configuration.hpp"#include "ocl_kernel.hpp"#include "ocl_common.hpp"#include <cassert>-#include <iomanip>-#include <ios>--#include <fstream>-#include <thread>#include <string>#include <vector>#include <memory>@@ -190,28 +185,19 @@ void set_arguments_impl(ocl_kernel_type& kernel,}}-sync_methods get_expected_sync_method(const ExecutionConfig& config) {- auto profiling = config.get_property(ov::enable_profiling);- auto queue_type = config.get_property(ov::intel_gpu::queue_type);- return profiling ? sync_methods::events : queue_type == QueueTypes::out_of_order ? sync_methods::barriers- : sync_methods::none;-}-} // namespaceocl_stream::ocl_stream(const ocl_engine &engine, const ExecutionConfig& config)- : stream(config.get_property(ov::intel_gpu::queue_type))- , _engine(engine)- , sync_method(get_expected_sync_method(config)) {+ : stream(config.get_property(ov::intel_gpu::queue_type), stream::get_expected_sync_method(config))+ , _engine(engine) {auto context = engine.get_cl_context();auto device = engine.get_cl_device();ocl::command_queues_builder queue_builder;queue_builder.set_profiling(config.get_property(ov::enable_profiling));- queue_builder.set_out_of_order(queue_type == QueueTypes::out_of_order);+ queue_builder.set_out_of_order(m_queue_type == QueueTypes::out_of_order);- if (sync_method == sync_methods::none && queue_type == QueueTypes::out_of_order) {- throw std::runtime_error("[CLDNN] Unexpected sync method (none) is specified for out_of_order queue");- }+ OPENVINO_ASSERT(m_sync_method != SyncMethods::none || m_queue_type == QueueTypes::in_order,+ "[GPU] Unexpected sync method (none) is specified for out_of_order queue");bool priorty_extensions = engine.extension_supported("cl_khr_priority_hints") && engine.extension_supported("cl_khr_create_command_queue");queue_builder.set_priority_mode(config.get_property(ov::intel_gpu::hint::queue_priority), priorty_extensions);@@ -226,16 +212,15 @@ ocl_stream::ocl_stream(const ocl_engine &engine, const ExecutionConfig& config)}ocl_stream::ocl_stream(const ocl_engine &engine, const ExecutionConfig& config, void *handle)- : stream(ocl_stream::detect_queue_type(handle))- , _engine(engine)- , sync_method(get_expected_sync_method(config)) {+ : stream(ocl_stream::detect_queue_type(handle), stream::get_expected_sync_method(config))+ , _engine(engine) {auto casted_handle = static_cast<cl_command_queue>(handle);_command_queue = ocl_queue_type(casted_handle, true);}#ifdef ENABLE_ONEDNN_FOR_GPUdnnl::stream& ocl_stream::get_onednn_stream() {- OPENVINO_ASSERT(queue_type == QueueTypes::in_order, "[GPU] Can't create onednn stream handle as onednn doesn't support out-of-order queue");+ OPENVINO_ASSERT(m_queue_type == QueueTypes::in_order, "[GPU] Can't create onednn stream handle as onednn doesn't support out-of-order queue");OPENVINO_ASSERT(_engine.get_device_info().vendor_id == INTEL_VENDOR_ID, "[GPU] Can't create onednn stream handle as for non-Intel devices");if (!_onednn_stream) {_onednn_stream = std::make_shared<dnnl::stream>(dnnl::ocl_interop::make_stream(_engine.get_onednn_engine(), _command_queue.get()));@@ -284,7 +269,7 @@ event::ptr ocl_stream::enqueue_kernel(kernel& kernel,auto local = toNDRange(args_desc.workGroups.local);std::vector<cl::Event> dep_events;std::vector<cl::Event>* dep_events_ptr = nullptr;- if (sync_method == sync_methods::events) {+ if (m_sync_method == SyncMethods::events) {for (auto& dep : deps) {if (auto ocl_base_ev = std::dynamic_pointer_cast<ocl_base_event>(dep)) {if (ocl_base_ev->get().get() != nullptr)@@ -292,13 +277,13 @@ event::ptr ocl_stream::enqueue_kernel(kernel& kernel,}}dep_events_ptr = &dep_events;- } else if (sync_method == sync_methods::barriers) {+ } else if (m_sync_method == SyncMethods::barriers) {sync_events(deps, is_output);}cl::Event ret_ev;- bool set_output_event = sync_method == sync_methods::events || is_output;+ bool set_output_event = m_sync_method == SyncMethods::events || is_output;try {_command_queue.enqueueNDRangeKernel(kern, cl::NullRange, global, local, dep_events_ptr, set_output_event ? &ret_ev : nullptr);@@ -330,7 +315,7 @@ event::ptr ocl_stream::enqueue_marker(std::vector<event::ptr> const& deps, boolreturn std::make_shared<ocl_event>(ret_ev);}- if (sync_method == sync_methods::events) {+ if (m_sync_method == SyncMethods::events) {cl::Event ret_ev;std::vector<cl::Event> dep_events;for (auto& dep : deps) {@@ -349,7 +334,7 @@ event::ptr ocl_stream::enqueue_marker(std::vector<event::ptr> const& deps, bool}return std::make_shared<ocl_event>(ret_ev, ++_queue_counter);- } else if (sync_method == sync_methods::barriers) {+ } else if (m_sync_method == SyncMethods::barriers) {sync_events(deps, is_output);return std::make_shared<ocl_event>(_last_barrier_ev, _last_barrier);} else {diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.hppindex c02a1517486..b9c51ccb046 100644--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.hpp+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_stream.hpp@@ -10,55 +10,24 @@#include "ocl_engine.hpp"#include <memory>-#include <chrono>-#include <thread>-#include <iostream>-#include <sstream>-#include <utility>#include <vector>namespace cldnn {namespace ocl {-// Possible sync methods for kernels in stream-enum class sync_methods {- /* Build dependency graph using events. Each kernel creates proper cl_event which is set as dependency of users- At this moment it requires multiple retain/release calls for cl_event after each enqueueNDRange- which is less performant comparing to the barriers version- */- events = 0,- /* Enqueue barriers between dependent kernels. For example consider the following dimond dependency graph:- kernel_0- / \- kernel_1 kernel_2- \ /- kernel_3- In that case we do the following:- 1. Enqueue kernel_0- 2. Enqueue barrier (ensures kernel_0 is completed)- 3. Enqueue kernel_1- 4. Enqueue kernel_2 (doesn't depend on kernel_1)- 5. Enqueue barrier (ensures kernel_1 and kernel_2 are completed)- 6. Enqueue kernel_3- */- barriers = 1,- /* No explicit syncronization is needed. Applicable for in-order queue only */- none = 2-};class ocl_stream : public stream {public:const ocl_queue_type& get_cl_queue() const { return _command_queue; }- explicit ocl_stream(const ocl_engine& engine, const ExecutionConfig& config);+ ocl_stream(const ocl_engine& engine, const ExecutionConfig& config);ocl_stream(const ocl_engine &engine, const ExecutionConfig& config, void *handle);ocl_stream(ocl_stream&& other)- : stream(other.queue_type)+ : stream(other.m_queue_type, other.m_sync_method), _engine(other._engine), _command_queue(other._command_queue), _queue_counter(other._queue_counter.load()), _last_barrier(other._last_barrier.load())- , _last_barrier_ev(other._last_barrier_ev)- , sync_method(other.sync_method) {}+ , _last_barrier_ev(other._last_barrier_ev) {}~ocl_stream() = default;@@ -96,8 +65,6 @@ private:std::atomic<uint64_t> _last_barrier{0};cl::Event _last_barrier_ev;- sync_methods sync_method;-#ifdef ENABLE_ONEDNN_FOR_GPUstd::shared_ptr<dnnl::stream> _onednn_stream = nullptr;#endifdiff --git a/src/plugins/intel_gpu/src/runtime/stream.cpp b/src/plugins/intel_gpu/src/runtime/stream.cppindex 169f320e40d..5f898596f46 100644--- a/src/plugins/intel_gpu/src/runtime/stream.cpp+++ b/src/plugins/intel_gpu/src/runtime/stream.cpp@@ -19,6 +19,13 @@ QueueTypes stream::detect_queue_type(engine_types engine_type, void* queue_handl}}+SyncMethods stream::get_expected_sync_method(const ExecutionConfig& config) {+ auto profiling = config.get_property(ov::enable_profiling);+ auto queue_type = config.get_property(ov::intel_gpu::queue_type);+ return profiling ? SyncMethods::events : queue_type == QueueTypes::out_of_order ? SyncMethods::barriers+ : SyncMethods::none;+}+event::ptr stream::aggregate_events(const std::vector<event::ptr>& events, bool group, bool is_output) {if (events.size() == 1 && !is_output)return events[0];diff --git a/src/plugins/intel_gpu/tests/unit/passes/kernels_cache_test.cpp b/src/plugins/intel_gpu/tests/unit/passes/kernels_cache_test.cppindex 6ebe5d3dced..30225132c35 100644--- a/src/plugins/intel_gpu/tests/unit/passes/kernels_cache_test.cpp+++ b/src/plugins/intel_gpu/tests/unit/passes/kernels_cache_test.cpp@@ -120,6 +120,8 @@ TEST(kernels_cache, sub_kernel_ordering_test) {kernel_code_list.push_back(kernel_string);}kernel_impl_params dummy_params;+ auto dummy_prog = std::make_shared<program>(engine, config);+ dummy_params.prog = dummy_prog.get();_kernels_cache->add_kernels_source(dummy_params, kernel_code_list, false);_kernels_cache->build_all();auto _out_kernels = _kernels_cache->get_kernels(dummy_params);