Skip to content

Commit e0262f5

Browse files
authored
[python][knobs] Implement _build hook (triton-lang#6710)
Right now there's a TODO in `_build` to expand support for compilers. With `knobs` a natural way to expand support is with a custom build implementation, hence this PR.
1 parent 5b7b86a commit e0262f5

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

python/triton/knobs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,22 @@ def scope(self) -> Generator[None, None, None]:
272272
self.__dict__.update(orig)
273273

274274

275+
class BuildImpl(Protocol):
276+
277+
def __call__(self, name: str, src: str, srcdir: str, library_dirs: list[str], include_dirs: list[str],
278+
libraries: list[str], /) -> str:
279+
...
280+
281+
275282
class build_knobs(base_knobs):
276283
"""Configuration controlling how the native compiler is invoked"""
277284
cc: env_opt_str = env_opt_str("CC")
278285

279286
cudacrt_path: env_opt_str = env_opt_str("TRITON_CUDACRT_PATH")
280287
cudart_path: env_opt_str = env_opt_str("TRITON_CUDART_PATH")
281288

289+
impl: Optional[BuildImpl] = None
290+
282291
@property
283292
def backend_dirs(self) -> set[str]:
284293
return {path for path in (self.cudacrt_path, self.cudart_path) if path is not None}

python/triton/runtime/build.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@
66
from .. import knobs
77

88

9-
def _build(name, src, srcdir, library_dirs, include_dirs, libraries):
9+
def _build(name: str, src: str, srcdir: str, library_dirs: list[str], include_dirs: list[str],
10+
libraries: list[str]) -> str:
11+
if impl := knobs.build.impl:
12+
return impl(name, src, srcdir, library_dirs, include_dirs, libraries)
1013
suffix = sysconfig.get_config_var('EXT_SUFFIX')
1114
so = os.path.join(srcdir, '{name}{suffix}'.format(name=name, suffix=suffix))
1215
# try to avoid setuptools if possible
1316
cc = os.environ.get("CC")
1417
if cc is None:
15-
# TODO: support more things here.
1618
clang = shutil.which("clang")
1719
gcc = shutil.which("gcc")
1820
cc = gcc if gcc is not None else clang
1921
if cc is None:
20-
raise RuntimeError("Failed to find C compiler. Please specify via CC environment variable.")
22+
raise RuntimeError(
23+
"Failed to find C compiler. Please specify via CC environment variable or set triton.knobs.build.impl.")
2124
# This function was renamed and made public in Python 3.10
2225
if hasattr(sysconfig, 'get_default_scheme'):
2326
scheme = sysconfig.get_default_scheme()
2427
else:
25-
scheme = sysconfig._get_default_scheme()
28+
scheme = sysconfig._get_default_scheme() # type: ignore
2629
# 'posix_local' is a custom scheme on Debian. However, starting Python 3.10, the default install
2730
# path changes to include 'local'. This change is required to use triton with system-wide python.
2831
if scheme == 'posix_local':

0 commit comments

Comments
 (0)