Repository navigation
feat: arch/cc compatibility check. #774
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e25af4d
feat: arch/cc compatibility check./
sayakpaul 8e8726e
get rid of unneeded stuff.
sayakpaul 63234c4
add a minimal test to mimic remy's issue: https://github.com/huggingf…
sayakpaul 487024b
ptx
sayakpaul 5f1e7e9
descriptive name.
sayakpaul b120618
raise and remove unpythonic bits.
sayakpaul ddeee38
test
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| from kernels_data import Backend, Metadata | ||
|
|
||
| from kernels.compat import has_torch | ||
|
|
||
|
|
||
| def _parse_cuda_arch(arch: str) -> tuple[int, int, str, bool] | None: | ||
| """Parse a CUDA arch into its capability, suffix, and PTX flag. | ||
|
|
||
| CUDA archs are compute capabilities like `9.0`, optionally with an | ||
| architecture-specific (`9.0a`) or family-specific (`10.0f`) suffix | ||
| and/or a `+PTX` flag (`9.0+PTX`). Returns `None` when the arch string | ||
| is not in this format. | ||
| """ | ||
| ptx = False | ||
| if arch.endswith("+PTX"): | ||
| ptx = True | ||
| arch = arch[: -len("+PTX")] | ||
|
|
||
| suffix = "" | ||
| if arch.endswith(("a", "f")): | ||
| suffix = arch[-1] | ||
| arch = arch[:-1] | ||
|
|
||
| arch_major, sep, arch_minor = arch.partition(".") | ||
| if not sep or not arch_major.isdigit() or not arch_minor.isdigit(): | ||
| return None | ||
|
|
||
| return int(arch_major), int(arch_minor), suffix, ptx | ||
|
|
||
|
|
||
| def _cuda_arch_supports(arch: str, capability: tuple[int, int]) -> bool | None: | ||
| """Check whether a single declared CUDA arch supports a compute capability. | ||
|
|
||
| Returns `None` when the arch string is not in a known format. | ||
| """ | ||
| parsed = _parse_cuda_arch(arch) | ||
| if parsed is None: | ||
| return None | ||
|
|
||
| arch_major, arch_minor, suffix, ptx = parsed | ||
| major, minor = capability | ||
|
|
||
| if suffix == "a": | ||
| # Architecture-specific builds only run on that exact capability. | ||
| return (major, minor) == (arch_major, arch_minor) | ||
|
|
||
| if suffix == "f": | ||
| # Family-specific builds run on capabilities of the same generation | ||
| # with the same or a newer minor version. | ||
| return major == arch_major and minor >= arch_minor | ||
|
|
||
| if ptx: | ||
| # PTX is JIT-compiled for the current device, so the build also runs | ||
| # on any newer capability. | ||
| return (major, minor) >= (arch_major, arch_minor) | ||
|
|
||
| # Base builds run on capabilities of the same generation with the same | ||
| # or a newer minor version. | ||
| return major == arch_major and minor >= arch_minor | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
|
|
||
|
|
||
| def _supports_cuda_capability(archs: list[str], capability: tuple[int, int]) -> bool: | ||
| supports = [_cuda_arch_supports(arch, capability) for arch in archs] | ||
| if all(support is None for support in supports): | ||
| # None of the arch strings are in a known format (e.g. produced by a | ||
| # newer kernel-builder), so compatibility cannot be determined. | ||
| return True | ||
| return any(supports) | ||
|
|
||
|
|
||
| def _check_arch_incompatibility(metadata: Metadata, variant: str) -> None: | ||
| """Check a kernel build against the architecture of the current device. | ||
|
|
||
| Raises `RuntimeError` when the build does not support the device. | ||
| """ | ||
| archs = metadata.backend.archs | ||
| if not archs or not has_torch: | ||
| return | ||
|
|
||
| import torch | ||
|
|
||
| reason = None | ||
| backend_type = metadata.backend.backend_type | ||
| if backend_type == Backend.CUDA: | ||
| if torch.version.cuda is None or not torch.cuda.is_available(): | ||
| return | ||
| major, minor = torch.cuda.get_device_capability() | ||
| if not _supports_cuda_capability(archs, (major, minor)): | ||
| reason = ( | ||
| f"CUDA capability {major}.{minor} of the current device is not " | ||
| f"supported by the architectures of the build: {', '.join(archs)}" | ||
| ) | ||
| elif backend_type == Backend.ROCm: | ||
| if torch.version.hip is None or not torch.cuda.is_available(): | ||
| return | ||
| gcn_arch = torch.cuda.get_device_properties(torch.cuda.current_device()).gcnArchName | ||
| # Strip feature flags, e.g. `gfx90a:sramecc+:xnack-` -> `gfx90a`. | ||
| gcn_arch = gcn_arch.split(":")[0] | ||
| if gcn_arch not in archs: | ||
| reason = ( | ||
| f"ROCm arch {gcn_arch} of the current device is not supported " | ||
| f"by the architectures of the build: {', '.join(archs)}" | ||
| ) | ||
|
|
||
| if reason is None: | ||
| return | ||
|
|
||
| raise RuntimeError( | ||
| f"Kernel '{metadata.name}' variant '{variant}' does not support the " | ||
| f"current device: {reason}. The declared architectures can be " | ||
| "incomplete (e.g. when a kernel has a Triton fallback), pass " | ||
| "`check_arch=False` to skip this check. Warning: the kernel may " | ||
| "fail or crash the process at run time when it does not support " | ||
| "the device." | ||
| ) | ||
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
Oops, something went wrong.
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.
Still debating if this is the right place to include notes about it given it's supposed to be "basic" in nature. Open to suggestions.