-
Notifications
You must be signed in to change notification settings - Fork 51
[core] feat: improve variant resolution #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7b351c2
feat: improve kernel variant resolution.
sayakpaul 0991db8
add a test.
sayakpaul b633ae5
tighten tests
sayakpaul 7dd3fd9
Merge branch 'main' into improve-variant-resolution
sayakpaul e5b1fe9
use literal.
sayakpaul f3a1854
Merge branch 'main' into improve-variant-resolution
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import logging | ||
| from unittest.mock import patch | ||
|
|
||
| from packaging.version import Version | ||
|
|
||
| from kernels.backends import CANN, CPU, CUDA, XPU, Metal, Neuron, ROCm | ||
| from kernels.utils import _find_kernel_in_repo_path | ||
| from kernels.variants import _compatible_backend_variants | ||
|
|
||
|
|
||
| class TestCompatibleBackendVariants: | ||
| # Only CUDA for now. | ||
|
|
||
| def test_cuda_single_variant_minor_zero(self): | ||
| backend = CUDA(version=Version("12.0")) | ||
| assert _compatible_backend_variants(backend) == ["cu120"] | ||
|
|
||
| def test_cuda_returns_descending_minor_versions(self): | ||
| backend = CUDA(version=Version("12.9")) | ||
| variants = _compatible_backend_variants(backend) | ||
| assert variants == [f"cu12{i}" for i in range(9, -1, -1)] | ||
|
|
||
| def test_cuda_first_variant_is_exact_match(self): | ||
| backend = CUDA(version=Version("12.6")) | ||
| variants = _compatible_backend_variants(backend) | ||
| assert variants[0] == "cu126" | ||
|
|
||
| def test_cuda_last_variant_is_major_minor_zero(self): | ||
| backend = CUDA(version=Version("12.6")) | ||
| variants = _compatible_backend_variants(backend) | ||
| assert variants[-1] == "cu120" | ||
|
|
||
| def test_cuda_resolves_to_largest_available_minor(self): | ||
| # System has CUDA 12.9; available builds: 12.6, 12.8, 13.0. | ||
| # Expected: resolve to 12.8 (largest z <= 9 that is available). | ||
| backend = CUDA(version=Version("12.9")) | ||
| candidates = _compatible_backend_variants(backend) | ||
| available = {"cu126", "cu128", "cu130"} | ||
| resolved = next((v for v in candidates if v in available), None) | ||
| assert resolved == "cu128" | ||
|
|
||
| def test_cuda_falls_back_when_exact_not_available(self): | ||
| # System has CUDA 12.9; only 12.6 available for major 12. | ||
| backend = CUDA(version=Version("12.9")) | ||
| candidates = _compatible_backend_variants(backend) | ||
| available = {"cu126"} | ||
| resolved = next((v for v in candidates if v in available), None) | ||
| assert resolved == "cu126" | ||
|
|
||
| def test_cuda_no_match_when_only_higher_minor_available(self): | ||
| # System has CUDA 12.6; only 12.8 available — should not match. | ||
| backend = CUDA(version=Version("12.6")) | ||
| candidates = _compatible_backend_variants(backend) | ||
| available = {"cu128"} | ||
| resolved = next((v for v in candidates if v in available), None) | ||
| assert resolved is None | ||
|
|
||
| def test_cuda_no_match_for_different_major(self): | ||
| # System has CUDA 12.9; only 13.x builds available — should not match. | ||
| backend = CUDA(version=Version("12.9")) | ||
| candidates = _compatible_backend_variants(backend) | ||
| available = {"cu130", "cu131"} | ||
| resolved = next((v for v in candidates if v in available), None) | ||
| assert resolved is None | ||
|
|
||
| def test_cuda_major_version_preserved(self): | ||
| backend = CUDA(version=Version("11.8")) | ||
| variants = _compatible_backend_variants(backend) | ||
| assert all(v.startswith("cu11") for v in variants) | ||
|
|
||
| # Non-CUDA | ||
|
|
||
| def test_cpu_returns_single_variant(self): | ||
| assert _compatible_backend_variants(CPU()) == ["cpu"] | ||
|
|
||
| def test_metal_returns_single_variant(self): | ||
| assert _compatible_backend_variants(Metal()) == ["metal"] | ||
|
|
||
| def test_neuron_returns_single_variant(self): | ||
| assert _compatible_backend_variants(Neuron()) == ["neuron"] | ||
|
|
||
| def test_rocm_returns_single_variant(self): | ||
| backend = ROCm(version=Version("6.2")) | ||
| assert _compatible_backend_variants(backend) == ["rocm62"] | ||
|
|
||
| def test_xpu_returns_single_variant(self): | ||
| backend = XPU(version=Version("2024.2")) | ||
| assert _compatible_backend_variants(backend) == [backend.variant] | ||
|
|
||
| def test_cann_returns_single_variant(self): | ||
| backend = CANN(version=Version("8.0")) | ||
| assert _compatible_backend_variants(backend) == ["cann80"] | ||
|
|
||
|
|
||
| class TestVariantResolutionLogging: | ||
| def test_logs_when_resolved_to_lower_minor(self, tmp_path, caplog): | ||
| exact = "torch28-cxx11-cu129-x86_64-linux" | ||
| resolved = "torch28-cxx11-cu128-x86_64-linux" | ||
|
|
||
| build_dir = tmp_path / "build" / resolved | ||
| build_dir.mkdir(parents=True) | ||
| (build_dir / "__init__.py").touch() | ||
|
|
||
| cuda_129 = CUDA(version=Version("12.9")) | ||
| with ( | ||
| patch("kernels.utils._build_variants", return_value=[exact, resolved]), | ||
| patch("kernels.utils._select_backend", return_value=cuda_129), | ||
| caplog.at_level(logging.INFO), | ||
| ): | ||
| _find_kernel_in_repo_path(tmp_path, "test_kernel") | ||
|
|
||
| assert any( | ||
| "cu129" in r.message and resolved in r.message for r in caplog.records | ||
| ) | ||
|
|
||
| def test_no_log_when_exact_match(self, tmp_path, caplog): | ||
| exact = "torch28-cxx11-cu129-x86_64-linux" | ||
|
|
||
| build_dir = tmp_path / "build" / exact | ||
| build_dir.mkdir(parents=True) | ||
| (build_dir / "__init__.py").touch() | ||
|
|
||
| cuda_129 = CUDA(version=Version("12.9")) | ||
| with ( | ||
| patch("kernels.utils._build_variants", return_value=[exact]), | ||
| patch("kernels.utils._select_backend", return_value=cuda_129), | ||
| caplog.at_level(logging.INFO), | ||
| ): | ||
| _find_kernel_in_repo_path(tmp_path, "test_kernel") | ||
|
|
||
| assert not any("resolved to" in r.message for r in caplog.records) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably best to put a literal here as well on the RHS of the equality check?