Coverage for cuda/core/_bindings_floor.py: 100.00%

82 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-12-03 01:06 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4 

5"""The cuda-bindings version floor: how it is read and how it is enforced. 

6 

7cuda.core supports two CUDA major series at a time. For each series, it 

8requires a minimum cuda-bindings version, the *floor*, at build time and at 

9run time. The floor is the oldest cuda-bindings release of that series that 

10meets two conditions. It was generated from the ``cuda.h`` major.minor that the 

11cuda.core wheels are built against. It provides every cuda-bindings API and fix 

12that cuda.core needs. A new cuda-bindings release does not move the floor, and 

13a cuda.core release does not. See 

14https://github.com/NVIDIA/cuda-python/issues/2783 and the support policy in the 

15documentation. 

16 

17Exactly one place declares the floors: the ``minimal-sysctk12`` and 

18``minimal-sysctk13`` extras in ``pyproject.toml``. Each pins 

19``cuda-bindings>=<floor>,<<next major>``. The ``cu12`` and ``cu13`` extras 

20include them through a ``cuda-core[minimal-sysctk<major>]`` self-reference, so 

21that pip and uv see the floor whichever extra a dependent requests. Everything 

22else reads the floor of each ``cu<major>`` extra, following that 

23self-reference, through :func:`floors_from_extras`: 

24 

25- The build backend, ``build_hooks.py``, checks the installed cuda-bindings 

26 against the floor. It checks that the ``cuda.h`` that it compiles against is 

27 the one that cuda-bindings was generated from. It passes the floor to the 

28 ``_build_info`` extension module, which records it next to the header the 

29 compiler used. 

30- ``cuda/core/__init__.py`` checks the installed cuda-bindings against that 

31 record with :func:`check_installed_bindings`. 

32- The documentation reads the floors into substitutions in ``docs/source/conf.py``. 

33- The ``check-bindings-config`` pre-commit hook checks that each registered 

34 toolkit in ``ci/versions.yml`` is not below its corresponding floor 

35 major.minor. The separate ``check-cuda-core-bindings-floor`` hook validates 

36 the extras, checks that each ``cu<major>`` extra includes its 

37 ``minimal-sysctk<major>`` extra, and prevents documentation from hardcoding 

38 the floors. 

39 

40The build and the tools above load this module by file path. It therefore uses 

41the standard library only and must not import anything from ``cuda``. 

42""" 

43 

44from __future__ import annotations 

45 

46import re 

47from collections.abc import Mapping, Sequence 

48 

49__all__ = [ 

50 "SUPPORT_URL", 

51 "bindings_requirement", 

52 "check_installed_bindings", 

53 "cuda_version_of", 

54 "floors_from_extras", 

55 "format_version", 

56 "release_triple", 

57] 

58 

59_RELEASE_RE = re.compile(r"^(\d+)\.(\d+)\.(\d+)") 

60_EXTRA_RE = re.compile(r"^cu(\d+)$") 

61# A cuda-bindings requirement, with or without extras such as `[all]`, up to 

62# an optional environment marker. After the name, the lookahead requires `[`, an 

63# operator, a marker, whitespace or the end, so that other names do not match. 

64_REQUIREMENT_RE = re.compile( 

65 r"^\s*cuda-bindings(?=[\[<>=!~;\s]|$)\s*(?:\[[^\]]*\])?\s*(?P<specifiers>[^;]*?)\s*(?:;.*)?$" 

66) 

67# A self-reference such as `cuda-core[minimal-sysctk13]`, up to an optional environment marker. 

68_SELF_REFERENCE_RE = re.compile(r"^\s*cuda-core\s*\[(?P<extras>[^\]]*)\]\s*(?:;.*)?$") 

69_FLOOR_SPEC_RE = re.compile(r"^>=(\d+)\.(\d+)\.(\d+)$") 

70# The upper bound: `<14`, `<14.0` or `<14.0.0`, or the equivalent `==13.*`. 

71_UPPER_SPEC_RE = re.compile(r"^<(\d+)(?:\.0)*$") 

72_MAJOR_SPEC_RE = re.compile(r"^==(\d+)\.\*$") 

73 

74# The support policy section the error messages refer to. 

75SUPPORT_URL = "https://nvidia.github.io/cuda-python/cuda-core/latest/support.html#cuda-core-bindings-floor" 

76 

77 

78def release_triple(version: str) -> tuple[int, int, int] | None: 

79 """The leading ``major.minor.patch`` of a version string, or None. 

80 

81 The function ignores pre-release and development suffixes, so ``13.4.1``, 

82 ``13.4.1a0`` and ``13.4.2.dev249+gabcdef`` yield (13, 4, 1), (13, 4, 1) 

83 and (13, 4, 2). A string without three leading integers yields None, for 

84 example the ``0.1.dev1`` that setuptools-scm reports for a shallow clone. 

85 """ 

86 m = _RELEASE_RE.match(version.strip()) 1bvwxyABtCuzDEqHIJKLMNO

87 if m is None: 1bvwxyABtCuzDEqHIJKLMNO

88 return None 1DEHJO

89 return int(m.group(1)), int(m.group(2)), int(m.group(3)) 1bvwxyABtCuzqIKLMN

90 

91 

92def format_version(triple: tuple[int, ...]) -> str: 

93 return ".".join(str(part) for part in triple) 1zqQPG

94 

95 

96def cuda_version_of(triple: tuple[int, int, int]) -> int: 

97 """The ``CUDA_VERSION`` macro value for the major.minor of a version triple, for example 13040.""" 

98 return triple[0] * 1000 + triple[1] * 10 1bvwxyABCuzDEqaG

99 

100 

101def floors_from_extras(extras: Mapping[str, Sequence[str]]) -> dict[int, tuple[int, int, int]]: 

102 """The floor per CUDA major, from the ``cu<major>`` extras of ``pyproject.toml``. 

103 

104 ``extras`` is the parsed ``[project.optional-dependencies]`` table. Each 

105 ``cu<N>`` extra must list exactly one versioned cuda-bindings requirement, 

106 either itself or through a ``cuda-core[<extra>]`` self-reference such as 

107 ``cuda-core[minimal-sysctk<N>]``. An unversioned requirement such as 

108 ``cuda-bindings[all]`` does not count. The requirement has two 

109 specifiers, in either order. One is the floor, ``>=<N>.<minor>.<patch>``. 

110 The other is an upper bound that excludes the next major, ``<<N+1>`` or 

111 ``==<N>.*``. Anything else raises ValueError with the name of the extra, so 

112 a typo fails the build and the pre-commit hook instead of a silent shift of 

113 the floor. 

114 """ 

115 floors: dict[int, tuple[int, int, int]] = {} 1adcefoprsmlknghijF

116 for extra in extras: 1adcefoprsmlknghijF

117 m = _EXTRA_RE.match(extra) 1adcefoprsmlknghijF

118 if m is None: 1adcefoprsmlknghijF

119 continue 1adcfoF

120 major = int(m.group(1)) 1adcefoprsmlknghij

121 matches = [ 1adcefoprsmlknghij

122 (r, rm) 

123 for r in _expand_self_references(extras, extra) 

124 if (rm := _REQUIREMENT_RE.match(r)) is not None and rm.group("specifiers") 

125 ] 

126 if len(matches) != 1: 1adcefoprsmlknghij

127 raise ValueError( 1oprs

128 f"the {extra!r} extra must list exactly one cuda-bindings requirement, found {len(matches)}" 

129 ) 

130 requirement, requirement_match = matches[0] 1adcefmlknghij

131 specifiers = [s.strip() for s in requirement_match.group("specifiers").split(",") if s.strip()] 1adcefmlknghij

132 floor_matches = [fm for s in specifiers if (fm := _FLOOR_SPEC_RE.match(s)) is not None] 1adcefmlknghij

133 confined = [cm for s in specifiers if (cm := _confined_major(s)) is not None] 1adcefmlknghij

134 if len(specifiers) != 2 or len(floor_matches) != 1 or len(confined) != 1: 1adcefmlknghij

135 raise ValueError( 1fmlkn

136 f"the {extra!r} extra must pin cuda-bindings as '>=<floor>,<<next major>', " 

137 f"for example '>=13.4.1,<14', but it lists {requirement!r}" 

138 ) 

139 floor = (int(floor_matches[0].group(1)), int(floor_matches[0].group(2)), int(floor_matches[0].group(3))) 1adceghij

140 if floor[0] != major or confined[0] != major: 1adceghij

141 raise ValueError( 1ghij

142 f"the {extra!r} extra pins cuda-bindings {requirement!r}, which does not confine it to CUDA {major}" 

143 ) 

144 floors[major] = floor 1adce

145 if not floors: 1adceF

146 raise ValueError("pyproject.toml declares no cu<major> extra") 1F

147 return dict(sorted(floors.items())) 1adce

148 

149 

150def _expand_self_references(extras: Mapping[str, Sequence[str]], extra: str, seen: tuple[str, ...] = ()) -> list[str]: 

151 """The requirements of ``extra``, with each ``cuda-core[...]`` replaced by the requirements of the extras it names.""" 

152 seen = (*seen, extra) 1adcefoprsmlknghij

153 requirements = [] 1adcefoprsmlknghij

154 for requirement in extras.get(extra, ()): 1adcefoprsmlknghij

155 m = _SELF_REFERENCE_RE.match(requirement) 1adcefoprsmlknghij

156 if m is None: 1adcefoprsmlknghij

157 requirements.append(requirement) 1adcefoprsmlknghij

158 continue 1adcefoprsmlknghij

159 for name in (name.strip() for name in m.group("extras").split(",")): 1acfop

160 if name and name not in seen: 1acfop

161 requirements.extend(_expand_self_references(extras, name, seen)) 1acfop

162 return requirements 1adcefoprsmlknghij

163 

164 

165def _confined_major(specifier: str) -> int | None: 

166 """The one major a specifier confines cuda-bindings to: ``<14`` -> 13, ``==13.*`` -> 13, else None.""" 

167 if (m := _UPPER_SPEC_RE.match(specifier)) is not None: 1adcefmlknghij

168 return int(m.group(1)) - 1 1adceflkghi

169 if (m := _MAJOR_SPEC_RE.match(specifier)) is not None: 1adcefmlknghij

170 return int(m.group(1)) 1dkj

171 return None 1adcefmlknghij

172 

173 

174def bindings_requirement(floor: tuple[int, int, int], below: tuple[int, ...] | None = None) -> str: 

175 """The pip requirement for cuda-bindings at or above ``floor`` and below ``below``, without extras. 

176 

177 ``below`` defaults to the next major: ``(13, 4, 1)`` gives ``cuda-bindings>=13.4.1,<14``. 

178 With ``below=(13, 5)`` it gives ``cuda-bindings>=13.4.1,<13.5``. 

179 """ 

180 upper = format_version(below) if below is not None else str(floor[0] + 1) 1G

181 return f"cuda-bindings>={format_version(floor)},<{upper}" 1G

182 

183 

184def header_minor(cuda_version: int) -> tuple[int, int]: 

185 """The (major, minor) of a ``CUDA_VERSION`` macro value: 13040 -> (13, 4).""" 

186 return cuda_version // 1000, cuda_version // 10 % 100 1bvwxytuqPaR

187 

188 

189def check_installed_bindings( 

190 installed_version: str, 

191 installed_cuda_version: int, 

192 build_cuda_major: int, 

193 build_cuda_version: int, 

194 build_floor: tuple[int, int, int], 

195 core_version: str, 

196) -> tuple[int, int, int]: 

197 """Validate the installed cuda-bindings against a build and return its triple. 

198 

199 ``installed_version`` and ``installed_cuda_version`` are the ``__version__`` 

200 and ``driver.CUDA_VERSION`` of the installed cuda-bindings. The latter is 

201 the ``cuda.h`` that it was generated from, for example 13040. 

202 ``build_cuda_major``, ``build_cuda_version`` and ``build_floor`` are the 

203 build's record in the ``_build_info`` extension module. 

204 

205 Raises ImportError with an actionable message unless the installed 

206 cuda-bindings passes all of these checks: 

207 

208 - It is a release. 

209 - It is of the major that this build was compiled for. 

210 - It is at least the floor. 

211 - It was generated from a ``cuda.h`` minor at least as new as the one that 

212 the build compiled against. The driver function table that the C++ layer 

213 looks up in cuda-bindings is keyed by the macros of that header, so an 

214 older minor may lack entries. 

215 

216 The check compares headers as ``CUDA_VERSION`` values, not version strings, 

217 because a development build of cuda-bindings carries the previous release's 

218 version string. 

219 """ 

220 installed = release_triple(installed_version) 1bvwxyABtCuzDEq

221 if installed is None: 1bvwxyABtCuzDEq

222 raise ImportError( 1DE

223 f"cuda.core requires a cuda-bindings {build_cuda_major}.x release, " 

224 f"but the installed cuda-bindings version is {installed_version}" 

225 ) 

226 major = installed[0] 1bvwxyABtCuzq

227 if major != build_cuda_major: 1bvwxyABtCuzq

228 raise ImportError( 1ABC

229 f"This cuda.core {core_version} build is for CUDA {build_cuda_major}, but the installed " 

230 f"cuda-bindings is {installed_version}. Install cuda-bindings {build_cuda_major}.x with: " 

231 f'pip install "cuda-bindings=={build_cuda_major}.*". If a cuda.core build for CUDA {major} exists, ' 

232 "install it instead." 

233 ) 

234 if installed < tuple(build_floor): 1bvwxytuzq

235 floor = format_version(build_floor) 1zq

236 raise ImportError( 1zq

237 f"cuda.core {core_version} requires cuda-bindings >= {floor} for CUDA {major}, but cuda-bindings " 

238 f'{installed_version} is installed. Upgrade with: pip install -U "cuda-bindings>={floor},<{major + 1}"' 

239 ) 

240 built_against, generated_from = header_minor(build_cuda_version), header_minor(installed_cuda_version) 1bvwxytuq

241 if generated_from < built_against: 1bvwxytuq

242 needed = f"{built_against[0]}.{built_against[1]}" 1tu

243 raise ImportError( 1tu

244 f"cuda.core {core_version} was compiled against CUDA {needed} headers and needs cuda-bindings " 

245 f"{needed} or newer, but cuda-bindings {installed_version} is installed. This does not require a " 

246 f"newer CUDA driver or toolkit: cuda.core supports older CUDA {major}.x versions at run time, " 

247 f'see {SUPPORT_URL}. Upgrade with: pip install -U "cuda-bindings>={needed}.0,<{major + 1}"' 

248 ) 

249 return installed 1bvwxytq