From b9b131403d3f18e2df3f2e05921a5c481d43789e Mon Sep 17 00:00:00 2001 From: Yevhenii Havrylko Date: Tue, 27 Aug 2024 09:27:23 -0400 Subject: [PATCH] Add `intel-sycl-rt` wheel support (#1857) Add support to https://pypi.org/project/intel-sycl-rt/ wheel package that is described #1717 --------- Signed-off-by: Anatoly Myachev Co-authored-by: Anatoly Myachev --- python/triton/runtime/build.py | 9 +++- third_party/intel/backend/driver.py | 64 +++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/python/triton/runtime/build.py b/python/triton/runtime/build.py index ce5d91c237..960009595e 100644 --- a/python/triton/runtime/build.py +++ b/python/triton/runtime/build.py @@ -53,8 +53,9 @@ def _build(name, src, srcdir, library_dirs, include_dirs, libraries): cxx = os.environ.get("CXX") if cxx is None: clangpp = shutil.which("clang++") + gxx = shutil.which("g++") icpx = shutil.which("icpx") - cxx = icpx if icpx is not None else clangpp + cxx = icpx or clangpp or gxx if cxx is None: raise RuntimeError("Failed to find C++ compiler. Please specify via CXX environment variable.") import numpy as np @@ -63,6 +64,8 @@ def _build(name, src, srcdir, library_dirs, include_dirs, libraries): cc_cmd = [cxx] if icpx is not None: cc_cmd += ["-fsycl"] + else: + cc_cmd += ["--std=c++17"] else: cc_cmd = [cc] @@ -71,6 +74,10 @@ def _build(name, src, srcdir, library_dirs, include_dirs, libraries): cc_cmd += [f'-l{lib}' for lib in libraries] cc_cmd += [f"-L{dir}" for dir in library_dirs] cc_cmd += [f"-I{dir}" for dir in include_dirs if dir is not None] + + if os.getenv("VERBOSE"): + print(" ".join(cc_cmd)) + ret = subprocess.check_call(cc_cmd) if ret == 0: return so diff --git a/third_party/intel/backend/driver.py b/third_party/intel/backend/driver.py index 60a0d9be66..4bee3a0a7b 100644 --- a/third_party/intel/backend/driver.py +++ b/third_party/intel/backend/driver.py @@ -1,26 +1,76 @@ +import importlib.metadata import os import hashlib +import shutil import tempfile from pathlib import Path from triton.runtime.build import _build from triton.runtime.cache import get_cache_manager from triton.backends.compiler import GPUTarget from triton.backends.driver import DriverBase +from packaging.version import Version +from packaging.specifiers import SpecifierSet ze_root = os.getenv("ZE_PATH", default="/usr/local") include_dir = [os.path.join(ze_root, "include")] -oneapi_root = os.getenv("ONEAPI_ROOT") -if oneapi_root: - include_dir += [ - os.path.join(oneapi_root, "compiler/latest/include"), - os.path.join(oneapi_root, "compiler/latest/include/sycl") - ] + +def find_sycl(include_dir: list[str]) -> tuple[list[str], list[str]]: + """ + Looks for the sycl library in known places. + + Arguments: + include_dir: list of include directories to pass to compiler. + + Returns: + enriched include_dir and library_dir. + + Raises: + AssertionError: if library was not found. + """ + library_dir = [] + include_dir = include_dir.copy() + assertion_message = ("sycl headers not found, please install `icpx` compiler, " + "or provide `ONEAPI_ROOT` environment " + "or install `intel-sycl-rt>=2025.0.0` wheel") + + if shutil.which("icpx"): + # only `icpx` compiler knows where sycl runtime binaries and header files are + return include_dir, library_dir + + oneapi_root = os.getenv("ONEAPI_ROOT") + if oneapi_root: + include_dir += [ + os.path.join(oneapi_root, "compiler/latest/include"), + os.path.join(oneapi_root, "compiler/latest/include/sycl") + ] + return include_dir, library_dir + + try: + sycl_rt = importlib.metadata.metadata("intel-sycl-rt") + except importlib.metadata.PackageNotFoundError: + raise AssertionError(assertion_message) + + if Version(sycl_rt.get("version", "0.0.0")) in SpecifierSet("<2025.0.0a1"): + raise AssertionError(assertion_message) + + for f in importlib.metadata.files("intel-sycl-rt"): + # sycl/sycl.hpp and sycl/CL/sycl.hpp results in both folders + # being add: include and include/sycl. + if f.name == "sycl.hpp": + include_dir += [f.locate().parent.parent.resolve().as_posix()] + if f.name == "libsycl.so": + library_dir += [f.locate().parent.resolve().as_posix()] + + return include_dir, library_dir + + +include_dir, library_dir = find_sycl(include_dir) dirname = os.path.dirname(os.path.realpath(__file__)) include_dir += [os.path.join(dirname, "include")] -library_dir = [os.path.join(dirname, "lib")] +library_dir += [os.path.join(dirname, "lib")] libraries = ['ze_loader', 'sycl']