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
15 changes: 8 additions & 7 deletions kernels/src/kernels/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _parse_version(legacy_regex: re.Pattern, new_regex: re.Pattern, s: str, lega


# The version part of a backend in the new variant format, e.g. `12.8`.
_BACKEND_VERSION_REGEX = re.compile(r"(\d+)\.(\d+)")
_BACKEND_VERSION_REGEX = re.compile(r"(?P<major>\d+)\.(?P<minor>\d+)")


def _parse_backend_version(legacy_regex: re.Pattern, name: str, parts: list[str], legacy: bool) -> Version | None:
Expand All @@ -48,7 +48,7 @@ def _parse_backend_version(legacy_regex: re.Pattern, name: str, parts: list[str]
m = _BACKEND_VERSION_REGEX.fullmatch(parts[1])
if not m:
return None
return Version(f"{m.group(1)}.{m.group(2)}")
return Version(f"{m['major']}.{m['minor']}")


def _format_version(legacy_prefix: str, new_prefix: str, version: Version, legacy: bool) -> str:
Expand Down Expand Up @@ -83,7 +83,7 @@ def variant_str(self) -> str:
@dataclass(unsafe_hash=True)
class CANN:
# Legacy variant string: `cannxy`, new variant string: `cann-x.y`.
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"cann(\d+)(\d+)")
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"cann(?P<major>\d+)(?P<minor>\d+)")

version: Version
legacy: bool = field(default=True, compare=False)
Expand Down Expand Up @@ -125,7 +125,7 @@ def parse(parts: list[str]) -> "CPU":
@dataclass(unsafe_hash=True)
class CUDA:
# Legacy variant string: `cuxy`, new variant string: `cuda-x.y`.
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"cu(\d+)(\d+)")
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"cu(?P<major>\d+)(?P<minor>\d+)")

version: Version
legacy: bool = field(default=True, compare=False)
Expand Down Expand Up @@ -184,8 +184,9 @@ def parse(parts: list[str]) -> "Neuron":

@dataclass(unsafe_hash=True)
class ROCm:
# Legacy variant string: `rocmxy`, new variant string: `rocm-x.y`.
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"rocm(\d+)(\d+)")
# Legacy variant string: `rocmxy`, new variant string: `rocm-x.y`. The
# legacy minor is a single digit, except for 7.14 (`rocm714`).
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"rocm(?P<major>7(?=14$)|\d+)(?P<minor>\d+)")

version: Version
legacy: bool = field(default=True, compare=False)
Expand Down Expand Up @@ -227,7 +228,7 @@ def parse(parts: list[str]) -> "TPU":
@dataclass(unsafe_hash=True)
class XPU:
# Legacy variant string: `xpuxy`, new variant string: `xpu-x.y`.
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"xpu(\d+)(\d+)")
_LEGACY_VARIANT_REGEX: ClassVar[re.Pattern] = re.compile(r"xpu(?P<major>\d+)(?P<minor>\d+)")

version: Version
legacy: bool = field(default=True, compare=False)
Expand Down
4 changes: 3 additions & 1 deletion kernels/src/kernels/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ def install(self, *, api: HfApi) -> LocalKernel:

def resolve_local_kernel(repo_path: Path, *, backend: str | None) -> LocalKernel:
variant_path = None
trace = []

for base_path in [repo_path, repo_path / "build"]:
variants = get_variants_local(base_path)
variant, trace = resolve_variant(variants, backend)
variant, base_trace = resolve_variant(variants, backend)
trace += base_trace
if variant is not None:
variant_path = base_path / variant.variant_str
break
Expand Down
12 changes: 12 additions & 0 deletions kernels/tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
_locked_revision,
resolve_hub_cache_kernel,
resolve_hub_kernel,
resolve_local_kernel,
)
from kernels.variants import parse_variant

Expand Down Expand Up @@ -555,3 +556,14 @@ def test_locked_revision_requires_lock():
)
def test_resolver_protocol_conformance(resolver):
assert isinstance(resolver, Resolver)


def test_resolve_local_kernel_keeps_rejection_reasons(tmp_path):
(tmp_path / "torch-xpu").mkdir()

with pytest.raises(FileNotFoundError) as exc_info:
resolve_local_kernel(tmp_path, backend="cpu")

# The `build/` pass finds no variants and must not drop the reasons from
# the pass over `tmp_path`.
assert "torch-xpu: " in str(exc_info.value)
3 changes: 3 additions & 0 deletions kernels/tests/test_variants.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ def test_arch_variant_backend_python_and_system():
[
("cu128", "cuda-12.8", CUDA(Version("12.8"))),
("rocm71", "rocm-7.1", ROCm(Version("7.1"))),
("rocm714", "rocm-7.14", ROCm(Version("7.14"))),
("rocm100", "rocm-10.0", ROCm(Version("10.0"))),
("rocm200", "rocm-20.0", ROCm(Version("20.0"))),
("xpu20253", "xpu-2025.3", XPU(Version("2025.3"))),
("cann81", "cann-8.1", CANN(Version("8.1"))),
],
Expand Down
Loading