Skip to content

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

[GPU] Make singleton OCL Context (#32738)

Mingyu Kim8322000b1a4c
diff --git a/docs/sphinx_setup/api/nodejs_api/openvino-node/interfaces/Core.rst b/docs/sphinx_setup/api/nodejs_api/openvino-node/interfaces/Core.rst
index 87d4068b850..237ba78b537 100644
--- a/docs/sphinx_setup/api/nodejs_api/openvino-node/interfaces/Core.rst
+++ b/docs/sphinx_setup/api/nodejs_api/openvino-node/interfaces/Core.rst
@@ -2,10 +2,9 @@ Interface Core
==============
Core represents an OpenVINO runtime Core entity.
-User applications can create several Core class instances,
-but in this case, the underlying plugins
-are created multiple times and not shared between several Core instances.
-It is recommended to have a single Core instance per application.
+User applications can create several Core class instances.
+In that case the device plugins will still share
+underlying resources (such as OCL context) in per-device singleton.
.. code-block:: ts
diff --git a/src/bindings/js/node/lib/addon.ts b/src/bindings/js/node/lib/addon.ts
index dd9ffd9a976..8981e0fd4a4 100644
--- a/src/bindings/js/node/lib/addon.ts
+++ b/src/bindings/js/node/lib/addon.ts
@@ -28,10 +28,9 @@ export type OVAny = string | number | boolean;
/**
* Core represents an OpenVINO runtime Core entity.
*
- * User applications can create several Core class instances,
- * but in this case, the underlying plugins
- * are created multiple times and not shared between several Core instances.
- * It is recommended to have a single Core instance per application.
+ * User applications can create several Core class instances.
+ * In that case the device plugins will still share underlying resources
+ * (such as OCL context) in per-device singleton.
*/
export interface Core {
/**
diff --git a/src/bindings/python/src/openvino/_ov_api.pyi b/src/bindings/python/src/openvino/_ov_api.pyi
index 55a49bc66a4..8389fba57f4 100644
--- a/src/bindings/python/src/openvino/_ov_api.pyi
+++ b/src/bindings/python/src/openvino/_ov_api.pyi
@@ -273,10 +273,9 @@ class Core(openvino._pyopenvino.Core):
"""
Core class represents OpenVINO runtime Core entity.
- User applications can create several Core class instances, but in this
- case, the underlying plugins are created multiple times and not shared
- between several Core instances. The recommended way is to have a single
- Core instance per application.
+ User applications can create several Core class instances. In that case
+ the device plugins will still share underlying resources (such as OCL context)
+ in per-device singleton.
"""
def compile_model(self, model: typing.Union[openvino._ov_api.Model, str, pathlib.Path], device_name: typing.Optional[str] = None, config: typing.Optional[dict[str, typing.Any]] = None, *, weights: typing.Optional[bytes] = None) -> CompiledModel:
diff --git a/src/bindings/python/src/pyopenvino/core/core.cpp b/src/bindings/python/src/pyopenvino/core/core.cpp
index 241b0927656..6da879546cc 100644
--- a/src/bindings/python/src/pyopenvino/core/core.cpp
+++ b/src/bindings/python/src/pyopenvino/core/core.cpp
@@ -23,10 +23,9 @@ namespace py = pybind11;
void regclass_Core(py::module m) {
py::class_<ov::Core, std::shared_ptr<ov::Core>> cls(m, "Core");
- cls.doc() =
- "openvino.Core class represents OpenVINO runtime Core entity. User applications can create several "
- "Core class instances, but in this case, the underlying plugins are created multiple times and not shared "
- "between several Core instances. The recommended way is to have a single Core instance per application.";
+ cls.doc() = "openvino.Core class represents OpenVINO runtime Core entity. User applications can create several "
+ "Core class instances. In that case the device plugins will still share underlying "
+ "resources (such as OCL context) in per-device singleton.";
cls.def(py::init([](const py::object& xml_config_file) {
return std::make_shared<ov::Core>(Common::utils::to_fs_path(xml_config_file));
diff --git a/src/inference/include/openvino/runtime/core.hpp b/src/inference/include/openvino/runtime/core.hpp
index 55061d874f2..007141da46b 100644
--- a/src/inference/include/openvino/runtime/core.hpp
+++ b/src/inference/include/openvino/runtime/core.hpp
@@ -31,9 +31,8 @@ namespace ov {
/**
* @brief This class represents an OpenVINO runtime Core entity.
* @ingroup ov_runtime_cpp_api
- * User applications can create several Core class instances, but in this case the underlying plugins
- * are created multiple times and not shared between several Core instances. The recommended way is to have
- * a single Core instance per application.
+ * User applications can create several Core class instances. In that case the device plugins
+ * will still share underlying resources (such as OCL context) in per-device singleton.
*/
class OPENVINO_RUNTIME_API Core {
class Impl;
diff --git a/src/plugins/intel_gpu/src/plugin/plugin.cpp b/src/plugins/intel_gpu/src/plugin/plugin.cpp
index a730fec4d07..8051def463e 100644
--- a/src/plugins/intel_gpu/src/plugin/plugin.cpp
+++ b/src/plugins/intel_gpu/src/plugin/plugin.cpp
@@ -200,14 +200,31 @@ std::shared_ptr<ov::Model> Plugin::clone_and_transform_model(const std::shared_p
return cloned_model;
}
+// weak map to hold singleton default contexts
+// Weak singleton map is used to share contexts between multiple plugin(or Core) instances.
+// It is needed to ensure that context is released before plugin is unloaded.
+// As the actual ownership is in plugin class, the ownership is released when plugin is destructed.
+std::map<std::string, std::weak_ptr<RemoteContextImpl>> weak_singleton_default_contexts;
+std::mutex singleton_default_contexts_mutex;
+
std::map<std::string, RemoteContextImpl::Ptr> Plugin::get_default_contexts() const {
std::call_once(m_default_contexts_once, [this]() {
- // Create default context
+ std::lock_guard<std::mutex> lock(singleton_default_contexts_mutex);
for (auto& device : m_device_map) {
const auto device_name = get_device_name() + "." + device.first;
+
+ // If already initialized, use existing one
+ if (weak_singleton_default_contexts.find(device.first) != weak_singleton_default_contexts.end()) {
+ if (auto ctx = weak_singleton_default_contexts[device.first].lock()) {
+ m_default_contexts[device.first] = ctx;
+ continue;
+ }
+ }
+ // If context is not created yet or expired, create new one
const auto initialize = false;
auto ctx = std::make_shared<RemoteContextImpl>(device_name, std::vector<cldnn::device::ptr>{device.second}, initialize);
- m_default_contexts.insert({device.first, ctx});
+ weak_singleton_default_contexts[device.first] = ctx;
+ m_default_contexts[device.first] = ctx;
}
});
return m_default_contexts;
diff --git a/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp b/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp
index 4d702526bcb..4d88ccc9079 100644
--- a/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp
+++ b/src/plugins/intel_gpu/tests/functional/behavior/infer_request.cpp
@@ -18,6 +18,8 @@
#include "common_test_utils/subgraph_builders/read_concat_split_assign.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/unsqueeze.hpp"
+#include "openvino/runtime/intel_gpu/ocl/ocl.hpp"
+#include "openvino/runtime/intel_gpu/ocl/ocl_wrapper.hpp"
namespace {
typedef std::tuple<
@@ -398,4 +400,15 @@ TEST(TensorTest, smoke_canShareTensorIfModelsFromDifferentCores) {
OV_ASSERT_NO_THROW(request1.infer());
OV_ASSERT_NO_THROW(request2.infer());
}
+
+TEST(CoreTest, smoke_singletonOclContext) {
+ auto core1 = ov::Core();
+ auto ctx1 = core1.get_default_context("GPU");
+ auto& oclContext1 = static_cast<ov::intel_gpu::ocl::ClContext&>(ctx1);
+ auto core2 = ov::Core();
+ auto ctx2 = core2.get_default_context("GPU");
+ auto& oclContext2 = static_cast<ov::intel_gpu::ocl::ClContext&>(ctx2);
+ ASSERT_EQ(oclContext1.get(), oclContext2.get());
+}
+
} // namespace