Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

## [1.6.0] - 2026-11-13
- Extended Julia functionality:
- Implemented Julia observers for Metal, Nvidia and AMD energy measurements.
- Improved timing stability.
- Include the new tests of #407, additional new and updated tests.
- Fixed issue #423.
- Improved performance and data handling.
- Improved NVML and AMD observers.
- Improved signal handling.
Expand Down
105 changes: 82 additions & 23 deletions kernel_tuner/backends/julia.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Kernel Tuner backend for running Julia CUDA.jl kernels via JuliaCall.
"""Kernel Tuner backend for running Julia GPU kernels (KernelAbstractions.jl) via JuliaCall.

This backend allows Julia kernels to be compiled, launched, and observed from Python using Kernel Tuner.

Requirements:
pip install juliacall
and in Julia: ] add CUDA / ROCBackend / oneAPI / Metal (will be automatically installed if not present)
and in Julia: ] add CUDACore NVML / AMDGPU / oneAPI / Metal (will be automatically installed if not present)
An installed CUDA.jl is used as is, as it contains CUDACore and NVML.
When installing CUDACore, a compatible local CUDA toolkit is used if available instead of downloading one,
see `KERNEL_TUNER_JULIA_LOCAL_CUDA_TOOLKIT` in julia_helper.py.

Notes:
- The kernel string should contain a valid Julia GPU kernel function definition.
Expand Down Expand Up @@ -56,7 +59,7 @@
from kernel_tuner.observers.julia import JuliaRuntimeObserver
from kernel_tuner.util import SkippableFailure

from .julia_helper import backend_map, detect_julia_gpu_backends
from .julia_helper import CUDA_TOOLKIT_JLLS, backend_map, detect_julia_gpu_backends, local_cuda_toolkit_preferences


jl = None
Expand Down Expand Up @@ -176,11 +179,11 @@
f"""
global dest_tmp, src_tmp # for memcpy_htod
module KernelTunerHelper
using {self.backend_mod_name}
using {self.backend_using}
const kt_julia_backend = {self.backend_mod_instname}()
const GPUArrayType = {self.GPUArrayType}
include("{str(Path(__file__).parent / "julia_helper.jl")}")
include("{str(Path(__file__).parent / str("julia_helper_ext_" + self.backend_mod_name.lower() + ".jl"))}")
include("{str(Path(__file__).parent / f"julia_helper_ext_{self.helper_ext}.jl")}")
end
"""
)
Expand All @@ -201,11 +204,29 @@
if backend_name not in backend_map:
raise ValueError(f"Unknown backend: {backend_name}")
info = backend_map[backend_name]
self.backend_name = backend_name
self.helper_ext = info["helper_ext"]
backend_pkg = info["pkg"]

if backend_pkg is not None:
# Ensure the package is installed
self.check_package_and_install(backend_pkg, allow_install=backend_name != "INTEL") # avoid Intel false positives
packages = [backend_pkg] + info.get("extra_pkgs", [])
preferences = None
if backend_name == "CUDA":
if self.package_installed("CUDA"):
# CUDA.jl contains CUDACore and NVML, use it as is (see julia_load_cuda.jl)
packages = []
elif not self.package_installed(backend_pkg):
# CUDA.jl downloads a CUDA toolkit on installation, prefer a compatible local toolkit instead
preferences = local_cuda_toolkit_preferences()
installed = self.check_package_and_install(
packages, allow_install=backend_name != "INTEL", preferences=preferences
) # avoid Intel false positives

Check warning on line 224 in kernel_tuner/backends/julia.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Move this trailing comment on the previous empty line.

See more on https://sonarcloud.io/project/issues?id=KernelTuner_kernel_tuner&issues=AaHVxHVMbD__uChzIWhs&open=AaHVxHVMbD__uChzIWhs&pullRequest=424
if not installed:
if self.backend_requested:
raise ImportError(f"Julia backend {backend_name} requested, but {backend_pkg} is not installed.")
warn(f"Julia backend {backend_name} detected, but {backend_pkg} is not installed. Using the CPU backend.")
return self.initialize_backend(device, "CPU")

# # Set debug level if needed
# if backend_name == "cuda":
Expand All @@ -218,8 +239,15 @@
# KernelAbstractions is a dependency of the backend packages, but must be a direct dependency to be loaded
self.check_package_and_install("KernelAbstractions")
jl.seval("using KernelAbstractions")
self.backend_using = info["module"]
if backend_pkg is not None:
jl.seval(f"using {info['module']}")
if info.get("loader") is not None:
# the loader binds the module in Main, so other modules defined in Main use it relatively
jl.include(str(Path(__file__).parent / info["loader"]))
jl.seval(f"using .{info['module']}")
self.backend_using = f"..{info['module']}"
else:
jl.seval(f"using {info['module']}")
backend_mod = getattr(jl.Main, self.backend_mod_name)
self.backend_mod = backend_mod
jl.seval(f"tmp_arr = {info['GPUArrayType']}(Float32.(zeros(2)))")
Expand All @@ -229,7 +257,7 @@

# Select device
try:
if int(device) == 0 and backend_pkg != "CUDA":
if int(device) == 0 and backend_name != "CUDA":
# Julia uses 1-based indexing, but the CUDA backend uses 0-based so we skip that
device = 1
jl.seval(info["device_select"](int(device)))
Expand Down Expand Up @@ -378,7 +406,7 @@
else:
module_code = f"""
module KernelTunerUserKernel
using {self.backend_mod_name}
using {self.backend_using}
{kernel_code}
end
"""
Expand Down Expand Up @@ -456,7 +484,7 @@

def kernel_finished(self):
"""Returns True if the kernel has finished, False otherwise."""
if self.backend_mod_name == "CUDA":
if self.backend_name == "CUDA":
if self._end_event is not None:
return bool(self.backend_mod.isdone(self._end_event))
return True
Expand Down Expand Up @@ -539,24 +567,53 @@
# Helper functions
# -------------------------

def check_package_and_install(self, package, allow_install=True):
"""Checks if the Julia package is available, and installs it if not."""
@staticmethod
def package_installed(package):
"""Checks if the Julia package is a direct dependency of the environment, without loading it."""
return bool(jl.seval(f'Base.find_package("{package}") !== nothing'))

@staticmethod
def package_available(package):
"""Checks if the Julia package can be imported."""
try:
jl.seval(f"import {package}")
return True
except Exception:
if not allow_install:
warn(f"{package} not found in your Julia environment. Run `using Pkg; Pkg.add(\"{package}\")` to install it.")
return
try:
warn(f"{package} not found, attempting to install it directly.")
import juliapkg
return False

def check_package_and_install(self, packages, allow_install=True, preferences=None):
"""Checks if the Julia package(s) are available, and installs the missing ones if not.

Returns True if the packages are available, False if they are missing and installation is not allowed.

``preferences`` optionally maps package names to Julia preferences that are set before installing, e.g. to
configure the CUDA toolkit JLLs. Packages that only appear in ``preferences`` are added as dependencies.
"""
packages = [packages] if isinstance(packages, str) else list(packages)
missing = [package for package in packages if not self.package_available(package)]
if not missing:
return True
add_command = "using Pkg; Pkg.add([" + ", ".join(f'\"{package}\"' for package in missing) + "])"
if not allow_install:
warn(f"{', '.join(missing)} not found in your Julia environment. Run `{add_command}` to install it.")
return False
try:
warn(f"{', '.join(missing)} not found, attempting to install it directly.")
import juliapkg

for package in missing:
juliapkg.add(package)
juliapkg.resolve()
for package, package_preferences in (preferences or {}).items():
# the preferences must be in place before installation, as CUDA.jl selects the toolkit at that point
juliapkg.add(package, CUDA_TOOLKIT_JLLS.get(package), preferences=package_preferences)
juliapkg.resolve()
for package in missing:
jl.seval(f"import {package}")
except Exception as e:
raise ImportError(
f'{package} not found in your Julia environment. Run `using Pkg; Pkg.add(\"{package}\")` to install it.'
) from e
return True
except Exception as e:
raise ImportError(
f"{', '.join(missing)} not found in your Julia environment. Run `{add_command}` to install it."
) from e

def setup_streams(self, backend_name: str):
"""Set up stream and event attributes for observers."""
Expand Down Expand Up @@ -605,7 +662,9 @@

def verify_backends_with_options(self, compiler_options=None):
"""Verify that the requested backend is available."""
self.backend_requested = False
if compiler_options is not None and len(compiler_options) == 1:
self.backend_requested = True
if compiler_options[0].upper() not in self.available_backends:
raise ValueError(
f"Requested Julia backend '{compiler_options[0]}' not available. "
Expand Down
Loading
Loading