Skip to content
This repository was archived by the owner on Apr 6, 2026. It is now read-only.

Commit 4e244cf

Browse files
authored
Add cuda-maxver option to the general section (#170)
This is to support kernels for the rare case where e.g. newer CUDA toolkits cause internal compiler errors.
1 parent 965a356 commit 4e244cf

5 files changed

Lines changed: 31 additions & 3 deletions

File tree

build2cmake/src/config/v2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub struct General {
4747
#[serde(default)]
4848
pub universal: bool,
4949

50+
pub cuda_maxver: Option<Version>,
51+
5052
pub cuda_minver: Option<Version>,
5153
}
5254

@@ -211,6 +213,7 @@ impl General {
211213
Self {
212214
name: general.name,
213215
universal,
216+
cuda_maxver: None,
214217
cuda_minver: None,
215218
}
216219
}

build2cmake/src/templates/cuda/preamble.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ if (NOT HIP_FOUND AND CUDA_FOUND)
4343
"Minimum required version is {{ cuda_minver }}.")
4444
endif()
4545
{% endif %}
46+
47+
{% if cuda_maxver %}
48+
if (CUDA_VERSION VERSION_MORE {{ cuda_maxver }})
49+
message(FATAL_ERROR "CUDA version ${CUDA_VERSION} is too new. "
50+
"Maximum version is {{ cuda_maxver }}.")
51+
endif()
52+
{% endif %}
53+
4654
elseif(HIP_FOUND)
4755
set(GPU_LANG "HIP")
4856

build2cmake/src/torch/cuda.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ fn write_cmake(
164164

165165
let cmake_writer = file_set.entry("CMakeLists.txt");
166166

167-
render_preamble(env, name, build.general.cuda_minver.as_ref(), cmake_writer)?;
167+
render_preamble(
168+
env,
169+
name,
170+
build.general.cuda_minver.as_ref(),
171+
build.general.cuda_maxver.as_ref(),
172+
cmake_writer,
173+
)?;
168174

169175
render_deps(env, build, cmake_writer)?;
170176

@@ -343,6 +349,7 @@ pub fn render_preamble(
343349
env: &Environment,
344350
name: &str,
345351
cuda_minver: Option<&Version>,
352+
cuda_maxver: Option<&Version>,
346353
write: &mut impl Write,
347354
) -> Result<()> {
348355
env.get_template("cuda/preamble.cmake")
@@ -351,6 +358,7 @@ pub fn render_preamble(
351358
context! {
352359
name => name,
353360
cuda_minver => cuda_minver.map(|v| v.to_string()),
361+
cuda_maxver => cuda_maxver.map(|v| v.to_string()),
354362
cuda_supported_archs => cuda_supported_archs(),
355363

356364
},

docs/writing-kernels.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ depends = [ "torch" ]
9696
Universal kernels do not use the other sections described below.
9797
A good example of a universal kernel is a Triton kernel.
9898
Default: `false`
99+
- `cuda-maxver`: the maximum CUDA toolkit version (inclusive). This option
100+
_must not_ be set under normal circumstances, since it can exclude Torch
101+
build variants that are [required for compliant kernels](https://github.com/huggingface/kernels/blob/main/docs/kernel-requirements.md).
102+
This option is provided for kernels that cause compiler errors on
103+
newer CUDA toolkit versions.
99104
- `cuda-minver`: the minimum required CUDA toolkit version. This option
100105
_must not_ be set under normal circumstances, since it can exclude Torch
101106
build variants that are [required for compliant kernels](https://github.com/huggingface/kernels/blob/main/docs/kernel-requirements.md).

lib/build.nix

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ rec {
6262
buildConfig: buildSets:
6363
let
6464
backends' = backends buildConfig;
65-
requiredCuda = buildConfig.general.cuda-minver or "11.8";
65+
minCuda = buildConfig.general.cuda-minver or "11.8";
66+
maxCuda = buildConfig.general.cuda-minver or "99.9";
67+
versionBetween =
68+
minver: maxver: ver:
69+
builtins.compareVersions ver minver >= 0 && builtins.compareVersions ver maxver <= 0;
6670
supportedBuildSet =
6771
buildSet:
6872
let
@@ -73,7 +77,7 @@ rec {
7377
|| (buildConfig.general.universal or false);
7478
cudaVersionSupported =
7579
buildSet.gpu != "cuda"
76-
|| (lib.strings.versionAtLeast buildSet.pkgs.cudaPackages.cudaMajorMinorVersion requiredCuda);
80+
|| versionBetween minCuda maxCuda buildSet.pkgs.cudaPackages.cudaMajorMinorVersion;
7781
in
7882
backendSupported && cudaVersionSupported;
7983
in

0 commit comments

Comments
 (0)