Skip to content

Commit

Permalink
implement device detection in SYCL
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuullll committed Nov 27, 2024
1 parent f3d24c7 commit 885424e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 72 deletions.
26 changes: 24 additions & 2 deletions WebUI/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "electron";
import path from "node:path";
import fs from "fs";
import { exec, spawn, ChildProcess } from "node:child_process";
import { exec, spawn, ChildProcess, spawnSync } from "node:child_process";
import { randomUUID } from "node:crypto";
import koffi from 'koffi';
import sudo from "sudo-prompt";
Expand Down Expand Up @@ -542,9 +542,31 @@ function wakeupApiService() {
const additionalEnvVariables = {
"SYCL_ENABLE_DEFAULT_CONTEXTS": "1",
"SYCL_CACHE_PERSISTENT": "1",
"PYTHONIOENCODING": "utf-8"
"PYTHONIOENCODING": "utf-8",
"ONEAPI_DEVICE_SELECTOR": "level_zero:*"
};

// Filter out unsupported devices
try {
const lsLevelZeroDevices = path.resolve(path.join(baseDir, "service/tools/ls_level_zero.exe"));
// copy ls_level_zero.exe to env/Library/bin for SYCL environment
const dest = path.resolve(path.join(pythonExe, "../Library/bin/ls_level_zero.exe"));
fs.copyFileSync(lsLevelZeroDevices, dest);
const ls = spawnSync(dest);
logger.info(`ls_level_zero.exe stdout: ${ls.stdout.toString()}`);
const devices = JSON.parse(ls.stdout.toString());
const supportedIDs = [];
for (const device of devices) {
if (device.name.toLowerCase().includes("arc") || device.device_id === 0xE20B) {
supportedIDs.push(device.id);
}
}
additionalEnvVariables["ONEAPI_DEVICE_SELECTOR"] = "level_zero:" + supportedIDs.join(",");
logger.info(`Set ONEAPI_DEVICE_SELECTOR=${additionalEnvVariables["ONEAPI_DEVICE_SELECTOR"]}`);
} catch (error) {
logger.error(`Failed to detect Level Zero devices: ${error}`);
}

spawnAPI(pythonExe, wordkDir, additionalEnvVariables);
}

Expand Down
38 changes: 0 additions & 38 deletions service/device_detect.py

This file was deleted.

27 changes: 27 additions & 0 deletions service/tools/ls_level_zero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <sycl/sycl.hpp>
#include <iostream>

using namespace sycl;

int main() {
std::cout << "[";
unsigned int id = 0;
for (const auto &plt : platform::get_platforms()) {
if (plt.get_backend() != backend::ext_oneapi_level_zero)
continue;

for (const auto &dev : plt.get_devices()) {
if (id > 0)
std::cout << ", ";
std::string name = dev.get_info<info::device::name>();
std::cout << "{\"id\": " << id << ", \"name\": \"" << name << "\"";
if (dev.has(aspect::ext_intel_device_id)) {
int device_id = dev.get_info<ext::intel::info::device::device_id>();
std::cout << ", \"device_id\": " << device_id;
}
std::cout << "}";
id++;
}
}
std::cout << "]" << std::endl;
}
32 changes: 0 additions & 32 deletions service/web_api.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
import sys

# Try to filter out unsupported devices
try:
# Create a subprocess to import IPEX and list devices
import subprocess
import os

script_dir = os.path.dirname(os.path.abspath(__file__))
device_detect_script = os.path.join(script_dir, "device_detect.py")

# Run the subprocess
result = subprocess.run(
[sys.executable, device_detect_script],
capture_output=True,
text=True,
)

# Check if the subprocess ran successfully
if result.returncode != 0:
raise Exception(f"Device detection failed: {result.stderr}")

# Get the supported device IDs
supported_ids = result.stdout.strip()
if not supported_ids:
raise Exception("No supported devices found")

# Set the environment variable to filter devices
os.environ["ONEAPI_DEVICE_SELECTOR"] = f"*:{supported_ids}"
print(f"Set ONEAPI_DEVICE_SELECTOR={os.environ['ONEAPI_DEVICE_SELECTOR']}")
except: # noqa: E722
print("Warning: Device detection failed, using all devices")
pass

# Credit to https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14186
# Related issues:
# + https://github.com/XPixelGroup/BasicSR/issues/649
Expand Down

0 comments on commit 885424e

Please sign in to comment.