compiler#

Kernel compilation helpers for building PyTorch extensions.

Selected through eval_config.kernel_compiler. Task authors normally do not use these directly: see kernelfoundry.TestBase.compile_torch_extension() for the high-level interface called from a task’s build method.

Functions

compile_glsl_to_spirv(src[, out, ...])

Compiles a GLSL compute shader to SPIR-V via glslangValidator.

extension_suffix()

File extension for a loadable Python extension module (.so on Linux and macOS, .pyd on Windows).

find_built_extension(build_dir, extension_name)

Locate the extension module a compiler just produced in build_dir.

Classes

BaseKernelCompiler(extension_name, src, ...)

Abstract base class for kernel compilers.

IcpxCompiler(*args[, ...])

Compiler class for SYCL programs using the Intel icpx compiler.

TorchCompiler(extension_name, src, ...[, ...])

Compiler class using the torch cpp_extension compiler.

kernelfoundry.compiler.compile_glsl_to_spirv(src: str | Path, out: str | Path | None = None, glslang_validator: str = 'glslangValidator', extra_args: list[str] | None = None) → Path[source]#

Compiles a GLSL compute shader to SPIR-V via glslangValidator.

Used from a Vulkan task’s build() method; the resulting .spv path is what kernelfoundry.eval_pipeline.utils.vulkan_support.create_vulkan_compute_context() expects.

Parameters:
  • src – Path to the .comp GLSL source file.

  • out – Output .spv path; defaults to src with its suffix replaced by .spv.

  • glslang_validator – Name/path of the glslangValidator executable.

  • extra_args – Additional CLI args to pass through to glslangValidator.

Returns:

The path to the compiled SPIR-V module.

Raises:

subprocess.CalledProcessError – If compilation fails, with the compiler’s stdout/stderr attached to the exception.

kernelfoundry.compiler.extension_suffix() → str[source]#

File extension for a loadable Python extension module (.so on Linux and macOS, .pyd on Windows).

kernelfoundry.compiler.find_built_extension(build_dir: str | PathLike, extension_name: str) → Path[source]#

Locate the extension module a compiler just produced in build_dir. Search by extension since torch.utils.cpp_extension.load writes <name>.so on Linux but <name>.pyd on Windows.

Raises:

FileNotFoundError – If no extension module is present

class kernelfoundry.compiler.BaseKernelCompiler(extension_name: str, src: str, build_dir: str, gpu_arch: str, timeout: int = 120, verbose: bool = False)[source]#

Abstract base class for kernel compilers.

__init__(extension_name: str, src: str, build_dir: str, gpu_arch: str, timeout: int = 120, verbose: bool = False)[source]#

Initialize the Compiler.

Parameters:
  • extension_name (str) – Name of the PyTorch extension to build.

  • src (str) – Path to the source file.

  • build_dir (str) – Directory to store the compiled outputs.

  • gpu_arch (str) – GPU architecture string.

  • timeout (int) – Timeout for each compilation step in seconds.

  • verbose (bool) – Whether to enable verbose output.

abstract compile()[source]#

Compile the source into a loadable PyTorch extension.

Returns:

Build artifacts and captured compiler output. The exact keys depend on the

concrete compiler; all include the collected stdout and stderr so build failures can be reported back to the caller.

Return type:

dict

class kernelfoundry.compiler.TorchCompiler(extension_name: str, src: str, build_dir: str, gpu_arch: str, timeout: int = 120, verbose: bool = False)[source]#

Compiler class using the torch cpp_extension compiler.

LOAD_FAILED_RETURNCODE = 87#

Exit code the build subprocess uses when the sources compiled but the module would not import. Distinct from 1 so the caller can tell the two failures apart; see compile().

classmethod failed_phase(returncode: int) → str | None[source]#

Which phase failed: "compile", "load", or None when nothing did.

compile()[source]#

Compile the source via torch.utils.cpp_extension.

Returns:

Build artifacts and captured compiler output.

Return type:

dict

class kernelfoundry.compiler.IcpxCompiler(*args, spirv_translator_options: str | None = None, backend_compile_options: str | None = None, **kwargs)[source]#

Compiler class for SYCL programs using the Intel icpx compiler.

__init__(*args, spirv_translator_options: str | None = None, backend_compile_options: str | None = None, **kwargs)[source]#

Initialize the Compiler.

Parameters:
  • extension_name (str) – Name of the PyTorch extension to build.

  • src (str) – Path to the source file.

  • build_dir (str) – Directory to store the compiled outputs.

  • gpu_arch (str) – GPU architecture string.

  • timeout (int) – Timeout for each compilation step in seconds.

  • verbose (bool) – Whether to enable verbose output.

compile()[source]#

Compile the SYCL program into a PyTorch extension.