Skip to content

[UR] Fix Loader Negative Prefilters #17528

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

Merged
merged 2 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
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
29 changes: 26 additions & 3 deletions unified-runtime/scripts/core/manifests.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
#
# Copyright (C) 2025 Intel Corporation
#
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# See YaML.md for syntax definition
#
# NOTE: device_types must be explictly listed. Refrain from using
# $X_DEVICE_TYPE_ALL. See https://github.com/intel/llvm/issues/17527.
--- #--------------------------------------------------------------------------
type: header
desc: "Intel $OneApi Unified Runtime adapter manifests"
Expand All @@ -7,7 +18,11 @@ type: manifest
name: opencl
backend: $X_ADAPTER_BACKEND_OPENCL
device_types:
- $X_DEVICE_TYPE_ALL
- $X_DEVICE_TYPE_CPU
- $X_DEVICE_TYPE_GPU
- $X_DEVICE_TYPE_FPGA
- $X_DEVICE_TYPE_MCA
- $X_DEVICE_TYPE_VPU
--- #--------------------------------------------------------------------------
type: manifest
name: cuda
Expand All @@ -25,13 +40,21 @@ type: manifest
name: level_zero
backend: $X_ADAPTER_BACKEND_LEVEL_ZERO
device_types:
- $X_DEVICE_TYPE_ALL
- $X_DEVICE_TYPE_CPU
- $X_DEVICE_TYPE_GPU
- $X_DEVICE_TYPE_FPGA
- $X_DEVICE_TYPE_MCA
- $X_DEVICE_TYPE_VPU
--- #--------------------------------------------------------------------------
type: manifest
name: level_zero_v2
backend: $X_ADAPTER_BACKEND_LEVEL_ZERO
device_types:
- $X_DEVICE_TYPE_ALL
- $X_DEVICE_TYPE_CPU
- $X_DEVICE_TYPE_GPU
- $X_DEVICE_TYPE_FPGA
- $X_DEVICE_TYPE_MCA
- $X_DEVICE_TYPE_VPU
--- #--------------------------------------------------------------------------
type: manifest
name: native_cpu
Expand Down
58 changes: 33 additions & 25 deletions unified-runtime/source/loader/ur_adapter_registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ namespace fs = filesystem;

namespace ur_loader {

struct ur_device_tuple {
ur_adapter_backend_t backend;
ur_device_type_t device;
};

// Helper struct representing a ONEAPI_DEVICE_SELECTOR filter term.
struct FilterTerm {
std::string backend;
Expand All @@ -37,7 +42,7 @@ struct FilterTerm {
{"native_cpu", UR_ADAPTER_BACKEND_NATIVE_CPU},
};

bool matchesBackend(const ur_adapter_manifest &manifest) const {
bool matchesBackend(const ur_adapter_backend_t &match_backend) const {
if (backend.front() == '*') {
return true;
}
Expand All @@ -49,7 +54,7 @@ struct FilterTerm {
backend);
return false;
}
if (backendIter->second == manifest.backend) {
if (backendIter->second == match_backend) {
return true;
}
return false;
Expand All @@ -60,12 +65,7 @@ struct FilterTerm {
{"gpu", UR_DEVICE_TYPE_GPU},
{"fpga", UR_DEVICE_TYPE_FPGA}};

bool matchesDevices(const ur_adapter_manifest &manifest) const {
// If the adapter can report all device types then it matches.
if (std::find(manifest.device_types.begin(), manifest.device_types.end(),
UR_DEVICE_TYPE_ALL) != manifest.device_types.end()) {
return true;
}
bool matchesDevices(const ur_device_type_t &match_device) const {
for (auto deviceString : devices) {
// We don't have a way to determine anything about device indices or
// sub-devices at this stage so just match any numeric value we get.
Expand All @@ -79,20 +79,19 @@ struct FilterTerm {
deviceString);
continue;
}
if (std::find(manifest.device_types.begin(), manifest.device_types.end(),
deviceIter->second) != manifest.device_types.end()) {
if (deviceIter->second == match_device) {
return true;
}
}
return false;
}

bool matches(const ur_adapter_manifest &manifest) const {
if (!matchesBackend(manifest)) {
bool matches(const ur_device_tuple &device_tuple) const {
if (!matchesBackend(device_tuple.backend)) {
return false;
}

return matchesDevices(manifest);
return matchesDevices(device_tuple.device);
}
};

Expand Down Expand Up @@ -280,22 +279,31 @@ class AdapterRegistry {
if (PositiveFilter) {
positiveFilters.push_back({backend, termPair.second});
} else {
// To preserve the behaviour of the original pre-filter implementation,
// we interpret all negative filters as backend only. This isn't
// correct, see https://github.com/intel/llvm/issues/17086
negativeFilters.push_back({backend, {"*"}});
negativeFilters.push_back({backend, termPair.second});
}
}

// If ONEAPI_DEVICE_SELECTOR only specified negative filters then we
// implicitly add a positive filter accepting all backends and devices.
if (positiveFilters.empty()) {
positiveFilters.push_back({"*", {"*"}});
}

for (const auto &manifest : ur_adapter_manifests) {
auto matchesFilter = [manifest](const FilterTerm &f) -> bool {
return f.matches(manifest);
};
if (std::any_of(positiveFilters.begin(), positiveFilters.end(),
matchesFilter) &&
std::none_of(negativeFilters.begin(), negativeFilters.end(),
matchesFilter)) {
adapterNames.insert(manifest.library);
// Check each device in the manifest.
for (const auto &device : manifest.device_types) {
ur_device_tuple single_device = {manifest.backend, device};

auto matchesFilter = [single_device](const FilterTerm &f) -> bool {
return f.matches(single_device);
};

if (std::any_of(positiveFilters.begin(), positiveFilters.end(),
matchesFilter) &&
std::none_of(negativeFilters.begin(), negativeFilters.end(),
matchesFilter)) {
adapterNames.insert(manifest.library);
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions unified-runtime/source/loader/ur_manifests.hpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.