Skip to content

Call zeInitDrivers in L0 provider #1101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/provider/provider_level_zero.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <umf/memory_provider_ops.h>
#include <umf/providers/provider_level_zero.h>

#include "base_alloc_global.h"
#include "provider_level_zero_internal.h"
#include "utils_load_library.h"
#include "utils_log.h"
Expand Down Expand Up @@ -111,7 +112,6 @@ umf_memory_provider_ops_t *umfLevelZeroMemoryProviderOps(void) {

#else // !defined(UMF_NO_LEVEL_ZERO_PROVIDER)

#include "base_alloc_global.h"
#include "libumf.h"
#include "utils_assert.h"
#include "utils_common.h"
Expand Down Expand Up @@ -211,6 +211,49 @@ static umf_result_t ze2umf_result(ze_result_t result) {
}
}

static umf_result_t ze_init_drivers(void *lib_handle, const char *lib_name) {
ze_result_t (*zeInitDriversFunc)(uint32_t *, ze_driver_handle_t *,
ze_init_driver_type_desc_t *);
*(void **)&zeInitDriversFunc =
utils_get_symbol_addr(lib_handle, "zeInitDrivers", lib_name);
if (!zeInitDriversFunc) {
return UMF_RESULT_ERROR_DEPENDENCY_UNAVAILABLE;
}

ze_init_driver_type_desc_t desc = {
.stype = ZE_STRUCTURE_TYPE_INIT_DRIVER_TYPE_DESC,
.pNext = NULL,
.flags = ZE_INIT_DRIVER_TYPE_FLAG_GPU};
Copy link
Contributor

@vinser52 vinser52 Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only GPUs?
That is why I was against calling zeInit or zeInitDrivers from UMF's code. It is the client's responsibility to initialize L0 properly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also against initializing Level Zero in UMF. Could we avoid that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't call zeInit/zeInitDrivers, the entire provider won't work. As for what specifically we are initializing - GPU is currently the only thing that makes sense from the UMF perspective. If there will be some other device types in the future that we want to support we can extend params to allow user to specify that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know how user can create level zero provider without zeInit on their side. They need to provoide context/device handle, which need level zero inited?

Copy link
Contributor

@vinser52 vinser52 Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for what @lplewa wrote.

@igchor are there any issues when adapter statically linked with libze_loader? If so, can we have a simple test/reproducer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, without zeInitDrivers/zeInit calling any L0 function results in a crash. I can prepare some simple reproducer.

uint32_t driverCount = 0;
ze_result_t result = zeInitDriversFunc(&driverCount, NULL, &desc);
if (result != ZE_RESULT_SUCCESS) {
return ze2umf_result(result);
}

ze_driver_handle_t *zeAllDrivers =
umf_ba_global_alloc(sizeof(ze_driver_handle_t) * driverCount);
result = zeInitDriversFunc(&driverCount, zeAllDrivers, &desc);
umf_ba_global_free(zeAllDrivers);
if (result != ZE_RESULT_SUCCESS) {
return ze2umf_result(result);
}

return UMF_RESULT_SUCCESS;
}

static umf_result_t ze_init(void *lib_handle, const char *lib_name) {
ze_result_t (*zeInitFunc)(ze_init_flag_t);
*(void **)&zeInitFunc =
utils_get_symbol_addr(lib_handle, "zeInit", lib_name);

if (!zeInitFunc) {
return UMF_RESULT_ERROR_DEPENDENCY_UNAVAILABLE;
}

ze_result_t result = zeInitFunc(ZE_INIT_FLAG_GPU_ONLY);
return ze2umf_result(result);
}

static void init_ze_global_state(void) {
#ifdef _WIN32
const char *lib_name = "ze_loader.dll";
Expand Down Expand Up @@ -266,6 +309,19 @@ static void init_ze_global_state(void) {
utils_close_library(lib_handle);
return;
}

if (ze_init_drivers(lib_handle, lib_name) != UMF_RESULT_SUCCESS) {
LOG_INFO("Initializing Level Zero through zeInitDrivers failed. "
"Falling back to zeInit.");

if (ze_init(lib_handle, lib_name) != UMF_RESULT_SUCCESS) {
LOG_FATAL("Failed to initialize Level Zero");
Init_ze_global_state_failed = true;
utils_close_library(lib_handle);
return;
}
}

ze_lib_handle = lib_handle;
}

Expand Down
Loading