Skip to content

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

[GPU] Postponed context initialization (#32347)

Roman Lyamin48eadd7e555c
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp
index 11424c180d4..7eee5dc8800 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.cpp
@@ -344,16 +344,18 @@ device_info init_device_info(const cl::Device& device, const cl::Context& contex
#ifdef ENABLE_ONEDNN_FOR_GPU
using namespace dnnl::impl::gpu::intel::jit;
- ngen::Product product = ngen::OpenCLCodeGenerator<ngen::HW::Unknown>::detectHWInfo(context.get(), device.get());
- info.arch = convert_ngen_arch(ngen::getCore(product.family));
-
- // We change the value of this flag to avoid OneDNN usage for the platforms unknown to OneDNN
- // This is required to guarantee some level of forward compatibility for the new HW generations
- // as OneDNN code generators are not generic and typically requires some updates for the new architectures
- // Ideally, we shouldn't do that as OCL impls sometimes also check this flag, but in order to avoid that
- // we need to ensure that graph transformations are not relying on this flag as indicator that onednn will be used
- if (product.family == ngen::ProductFamily::Unknown) {
- info.supports_immad = false;
+ if (context.get() != nullptr) {
+ ngen::Product product = ngen::OpenCLCodeGenerator<ngen::HW::Unknown>::detectHWInfo(context.get(), device.get());
+ info.arch = convert_ngen_arch(ngen::getCore(product.family));
+
+ // We change the value of this flag to avoid OneDNN usage for the platforms unknown to OneDNN
+ // This is required to guarantee some level of forward compatibility for the new HW generations
+ // as OneDNN code generators are not generic and typically requires some updates for the new architectures
+ // Ideally, we shouldn't do that as OCL impls sometimes also check this flag, but in order to avoid that
+ // we need to ensure that graph transformations are not relying on this flag as indicator that onednn will be used
+ if (product.family == ngen::ProductFamily::Unknown) {
+ info.supports_immad = false;
+ }
}
#else // ENABLE_ONEDNN_FOR_GPU
info.arch = gpu_arch::unknown;
@@ -389,29 +391,29 @@ memory_capabilities init_memory_caps(const cl::Device& device, const device_info
} // namespace
-void ocl_device::initialize_device(const cl::Device dev, const cl::Context& ctx) {
- _context = ctx;
- _device = dev;
+void ocl_device::initialize_context(const cl::Context& ctx) {
+ _context = ctx.get() != nullptr ? ctx : cl::Context(_device);
_usm_helper = std::make_unique<cl::UsmHelper>(_context, _device, use_unified_shared_memory());
_is_initialized = true;
}
-ocl_device::ocl_device(const cl::Device dev, const cl::Context& ctx, const cl::Platform& platform, bool initialize)
-: _platform(platform)
+ocl_device::ocl_device(const cl::Device dev, const cl::Context& ctx, const cl::Platform& platform, bool initialize_ctx)
+: _device(dev)
+, _platform(platform)
, _info(init_device_info(dev, ctx))
, _mem_caps(init_memory_caps(dev, _info)) {
- if (initialize) {
- initialize_device(dev, ctx);
+ if (initialize_ctx) {
+ initialize_context(ctx);
}
}
-ocl_device::ocl_device(const ocl_device::ptr other, bool initialize)
-: _platform(other->_platform)
+ocl_device::ocl_device(const ocl_device::ptr other, bool initialize_ctx)
+: _device(other->_device)
+, _platform(other->_platform)
, _info(other->_info)
, _mem_caps(other->_mem_caps) {
- if (initialize) {
- OPENVINO_ASSERT(other->is_initialized(), "[GPU] Can't initialize device because the source device is uninitialized");
- initialize_device(other->_device, other->_context);
+ if (initialize_ctx) {
+ initialize_context(other->_context);
}
}
@@ -468,14 +470,18 @@ void ocl_device::initialize() {
return;
ocl::ocl_device_detector detector;
- auto device_map = detector.get_available_devices(nullptr, nullptr, 0, -1, true);
+ auto device_map = detector.get_available_devices(nullptr, nullptr, 0, -1, false);
bool found = false;
for (auto& device : device_map) {
if (this->is_same(device.second)) {
OPENVINO_ASSERT(!found, "[GPU] Multiple matching devices found for ", this->get_info().dev_name, ". Only one matching device is expected");
if (auto casted = downcast<ocl_device>(device.second.get())) {
- initialize_device(casted->get_device(), casted->get_context());
+ auto& casted_device = casted->get_device();
+ if (casted->get_context().get() == nullptr) {
+ casted->set_context(cl::Context(casted_device));
+ }
+ initialize_context(casted->get_context());
found = true;
}
}
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hpp
index 0b29f080bb4..0b69c0c42f8 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device.hpp
@@ -39,7 +39,7 @@ public:
* before any use of the device.
*
* Copies platform, device info, and memory capabilities.
- * If @p initialize is true, initializes the new ocl_device instance accordingly.
+ * If @p initialize is true, initializes context of new ocl_device instance accordingly.
*/
ocl_device(const ocl_device::ptr other, bool initialize = true);
@@ -56,6 +56,7 @@ public:
void set_mem_caps(const memory_capabilities& memory_capabilities) override;
void set_sub_device_idx(uint32_t idx);
+ void set_context(const cl::Context& context) { _context = context; }
void initialize() override;
bool is_initialized() const override { return _is_initialized; };
@@ -63,7 +64,7 @@ public:
~ocl_device() = default;
protected:
- void initialize_device(const cl::Device dev, const cl::Context& ctx);
+ void initialize_context(const cl::Context& ctx);
bool _is_initialized = false;
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device_detector.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device_detector.cpp
index 83826e0a5d5..11dd1ea71d0 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_device_detector.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_device_detector.cpp
@@ -166,7 +166,9 @@ std::map<std::string, device::ptr> ocl_device_detector::get_available_devices(vo
// Additionally, for Intel GPUs, there may be cases where device is not clearly identifiable due to
// driver issues - to maintain compatibility, initialize them immediately
// For other vendors, allow deferred initialization to optimize power consumption
- if (user_context != nullptr || user_device != nullptr || dptr->get_info().vendor_id == cldnn::INTEL_VENDOR_ID) {
+
+ bool is_intel_gpu = dptr->get_info().vendor_id == cldnn::INTEL_VENDOR_ID;
+ if (user_context != nullptr || user_device != nullptr || is_intel_gpu) {
initialize_device = true;
}
@@ -176,7 +178,7 @@ std::map<std::string, device::ptr> ocl_device_detector::get_available_devices(vo
auto map_id = std::to_string(idx++);
ret[map_id] = std::make_shared<ocl_device>(root_device, initialize_device);
- OPENVINO_ASSERT(root_device->is_initialized(), "[GPU] Device is not initialized");
+ OPENVINO_ASSERT(root_device->is_initialized() || !is_intel_gpu, "[GPU] Device is not initialized");
auto sub_devices = getSubDevices(root_device->get_device());
if (!sub_devices.empty()) {
@@ -219,7 +221,12 @@ std::vector<device::ptr> ocl_device_detector::create_device_list() const {
for (auto& device : devices) {
if (!does_device_match_config(device))
continue;
- supported_devices.emplace_back(std::make_shared<ocl_device>(device, cl::Context(device), platform));
+
+ if (device.getInfo<CL_DEVICE_VENDOR_ID>() == cldnn::INTEL_VENDOR_ID) {
+ supported_devices.emplace_back(std::make_shared<ocl_device>(device, cl::Context(device), platform));
+ } else {
+ supported_devices.emplace_back(std::make_shared<ocl_device>(device, cl::Context(), platform, false));
+ }
}
} catch (std::exception& ex) {
GPU_DEBUG_LOG << "Devices query/creation failed for " << platform.getInfo<CL_PLATFORM_NAME>() << ": " << ex.what() << std::endl;
diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/device_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/device_test.cpp
index a12d542b076..33c6ada95ea 100644
--- a/src/plugins/intel_gpu/tests/unit/module_tests/device_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/module_tests/device_test.cpp
@@ -150,6 +150,9 @@ TEST(devices_test, is_same_device) {
ASSERT_TRUE(new_device->is_same(orig_device));
+ // To disable the short path in sameness check
+ new_device->get_device() = cl::Device();
+
auto new_device_info = new_device->get_info();
auto orig_uuid = new_device_info.uuid.uuid;
new_device_info.uuid.uuid[0] += 1;
@@ -197,7 +200,7 @@ TEST(devices_test, on_demand_initialization) {
auto ocl_device = std::dynamic_pointer_cast<ocl::ocl_device>(device.second);
auto should_be_initialized = ocl_device->get_info().vendor_id == cldnn::INTEL_VENDOR_ID;
ASSERT_EQ(ocl_device->is_initialized(), should_be_initialized);
- ASSERT_EQ(ocl_device->get_device().get() != nullptr, should_be_initialized);
+ ASSERT_EQ(ocl_device->get_device().get() != nullptr, true);
ASSERT_EQ(ocl_device->get_context().get() != nullptr, should_be_initialized);
ASSERT_FALSE(ocl_device->get_info().execution_units_count == 0);
ASSERT_FALSE(ocl_device->get_info().vendor_id == 0);