Skip to content

Commit 100cb8e

Browse files
Andy-Jostclaude
andcommitted
Compile the build record; verify the compiler's cuda.h against the Python read; review follow-ups (NVIDIA#2783)
- cuda/core/_build_info.pyx replaces the generated _build_info.py. It exposes CUDA_VERSION from the cuda.h the compiler resolved and the major and floor from the Cython compile-time environment, so the record cannot disagree with the binaries. cuda/core/__init__.py reads the extension. - build_hooks.py passes the cuda-bindings header version as a third macro; versions.hpp fails the compile unless the resolved cuda.h has the same major.minor. The Python read of cuda.h stays for the decisions that precede any compile, and can no longer silently differ from the compiler's. - ci/tools/cuda_core_bindings_floor.py reads the floor from the wheel's METADATA (the cu<major> extra) instead of parsing a generated module. - __init__.py imports _bindings_floor directly; the merge tool keeps it at the top level. A missing _build_info now distinguishes "no build for this CUDA major" from "not built". - ci/tools/env-vars reads ci/versions.yml with yq. - cuda_python/setup.py no longer pins cuda-core. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 8d16c3b commit 100cb8e

15 files changed

Lines changed: 253 additions & 160 deletions

‎.gitignore‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ cuda_bindings/cuda/bindings/utils/_get_handle.pyx
4141

4242
# Version files from setuptools_scm
4343
_version.py
44-
# Generated by cuda_core/build_hooks.py at build time (see cuda/core/__init__.py).
45-
cuda_core/cuda/core/_build_info.py
4644

4745
# Distribution / packaging
4846
.Python

‎ci/tools/cuda_core_bindings_floor.py‎

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,50 +14,51 @@
1414
from the checkout, so a nightly job that tests a wheel built from another
1515
commit reads that wheel's floor.
1616
17-
Each build records its floor in the generated cuda/core/_build_info.py. A
18-
single-major build places the file at top level. The merged wheel places it
19-
under cuda/core/cu<major>/. This script parses the CUDA_BINDINGS_FLOOR literal
20-
out of it and never runs it.
17+
The floors are declared once, by the `cu12`/`cu13` extras in
18+
cuda_core/pyproject.toml, and the wheel's METADATA carries them as
19+
`Requires-Dist: cuda-bindings[all]<14,>=13.4.1; extra == "cu13"`. This script
20+
reads that line.
2121
"""
2222

2323
from __future__ import annotations
2424

2525
import argparse
26-
import ast
26+
import re
2727
import sys
2828
import zipfile
2929
from pathlib import Path
3030

31-
MODULE = "_build_info.py"
31+
# The cuda-bindings requirement of one `cu<major>` extra in METADATA. The name
32+
# is followed by `[`, an operator or whitespace so that other names do not match.
33+
_REQUIRES_DIST_RE = re.compile(
34+
r"^Requires-Dist:\s*cuda-bindings(?=[\[<>=!~\s])(?P<spec>[^;]*);\s*extra\s*==\s*['\"]cu(?P<major>\d+)['\"]"
35+
)
36+
_FLOOR_RE = re.compile(r">=\s*(\d+)\.(\d+)\.(\d+)")
3237

3338

34-
def _literal(source: str, name: str):
35-
"""The literal that `source` assigns to `name` at module level. Parses the source and never runs it."""
36-
for node in ast.parse(source, MODULE).body:
37-
if isinstance(node, ast.AnnAssign):
38-
targets = [node.target]
39-
elif isinstance(node, ast.Assign):
40-
targets = node.targets
41-
else:
39+
def floor_from_metadata(metadata: str, major: int) -> str:
40+
"""The floor that the `cu<major>` extra of a wheel's METADATA pins cuda-bindings to."""
41+
for line in metadata.splitlines():
42+
m = _REQUIRES_DIST_RE.match(line)
43+
if m is None or int(m.group("major")) != major:
4244
continue
43-
if node.value is not None and any(isinstance(t, ast.Name) and t.id == name for t in targets):
44-
return ast.literal_eval(node.value)
45-
raise SystemExit(f"{MODULE} does not assign {name}")
46-
47-
48-
def floor_from_source(source: str, major: int) -> str:
49-
if _literal(source, "CUDA_MAJOR") != major:
50-
raise SystemExit(f"{MODULE} records a CUDA {_literal(source, 'CUDA_MAJOR')} build, not CUDA {major}")
51-
return ".".join(str(part) for part in _literal(source, "CUDA_BINDINGS_FLOOR"))
45+
floor = _FLOOR_RE.search(m.group("spec"))
46+
if floor is None:
47+
raise SystemExit(f"METADATA pins cuda-bindings without a floor for the cu{major} extra: {line.strip()!r}")
48+
if int(floor.group(1)) != major:
49+
raise SystemExit(
50+
f"METADATA pins a cuda-bindings floor of another major for the cu{major} extra: {line.strip()!r}"
51+
)
52+
return ".".join(floor.groups())
53+
raise SystemExit(f"METADATA declares no cuda-bindings requirement for the cu{major} extra")
5254

5355

5456
def floor_from_wheel(wheel: Path, major: int) -> str:
5557
with zipfile.ZipFile(wheel) as zf:
56-
names = set(zf.namelist())
57-
for candidate in (f"cuda/core/cu{major}/{MODULE}", f"cuda/core/{MODULE}"):
58-
if candidate in names:
59-
return floor_from_source(zf.read(candidate).decode("utf-8"), major)
60-
raise SystemExit(f"{wheel.name} contains no build for CUDA {major}: it has no {MODULE}. Is it a cuda-core wheel?")
58+
metadata = [name for name in zf.namelist() if name.endswith(".dist-info/METADATA")]
59+
if len(metadata) != 1:
60+
raise SystemExit(f"{wheel.name} contains {len(metadata)} METADATA files, expected 1. Is it a wheel?")
61+
return floor_from_metadata(zf.read(metadata[0]).decode("utf-8"), major)
6162

6263

6364
def main(argv: list[str] | None = None) -> int:

‎ci/tools/env-vars‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ elif [[ "${1}" == "test" ]]; then
8181
TEST_CUDA_MINOR="$(cut -d '.' -f 2 <<< ${CUDA_VER})"
8282
# CI builds the prior-major half of the cuda-core wheel against the prev_build
8383
# toolkit in ci/versions.yml and the backport branch's bindings.
84-
BUILD_PREV_CUDA_VER="$(sed -n '/prev_build:/,/version:/s/.*version: *"\([^"]*\)".*/\1/p' ci/versions.yml)"
84+
if ! command -v yq >/dev/null 2>&1; then
85+
echo "Error: yq is required to read ci/versions.yml" >&2
86+
exit 1
87+
fi
88+
BUILD_PREV_CUDA_VER="$(yq -r '.cuda.prev_build.version' ci/versions.yml)"
8589
BUILD_PREV_CUDA_MINOR="$(cut -d '.' -f 2 <<< ${BUILD_PREV_CUDA_VER})"
8690

8791
if [[ ${BUILD_CUDA_MAJOR} != ${TEST_CUDA_MAJOR} ]]; then

‎ci/tools/merge_cuda_core_wheels.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,11 @@ def merge_wheels(wheels: list[Path], output_dir: Path, show_wheel_contents: bool
148148

149149
print("\n=== Removing files from cuda/core/ directory ===", file=sys.stderr)
150150
# Only what cuda/core/__init__.py uses before it rewrites __path__ to the
151-
# versioned subpackage stays at top level: it imports _version, then
152-
# redirects every later import into the versioned tree. Anything else
153-
# left at top level is a dead copy that nothing imports.
154-
items_to_keep = {"__init__.py", "_version.py", *versioned_dirs}
151+
# versioned subpackage stays at top level: it imports _version and the
152+
# build-independent floor logic in _bindings_floor, then redirects every
153+
# later import into the versioned tree. Anything else left at top level
154+
# is a dead copy that nothing imports.
155+
items_to_keep = {"__init__.py", "_version.py", "_bindings_floor.py", *versioned_dirs}
155156
all_items = os.scandir(base_wheel / base_dir)
156157
removed_count = 0
157158
for f in all_items:

‎ci/tools/tests/test_cuda_core_bindings_floor.py‎

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,63 +20,80 @@ def _load(name, path):
2020

2121
tool = _load("cuda_core_bindings_floor", TOOLS / "cuda_core_bindings_floor.py")
2222

23-
FLOORS = {12: (12, 9, 8), 13: (13, 4, 1)}
23+
# METADATA as setuptools writes it: pip's normalized specifier order, extras in quotes.
24+
METADATA = """\
25+
Metadata-Version: 2.4
26+
Name: cuda-core
27+
Version: 1.3.0
28+
Requires-Dist: cuda-pathfinder>=1.4.2
29+
Requires-Dist: numpy
30+
Provides-Extra: cu12
31+
Requires-Dist: cuda-bindings[all]<13,>=12.9.8; extra == "cu12"
32+
Requires-Dist: cuda-toolkit==12.*; extra == "cu12"
33+
Provides-Extra: cu13
34+
Requires-Dist: cuda-bindings[all]<14,>=13.4.1; extra == "cu13"
35+
Requires-Dist: cuda-toolkit==13.*; extra == "cu13"
36+
"""
37+
38+
39+
def _wheel(tmp_path, metadata=METADATA):
40+
path = tmp_path / "cuda_core-1.3.0-cp312-cp312-linux_x86_64.whl"
41+
with zipfile.ZipFile(path, "w") as zf:
42+
zf.writestr("cuda/core/__init__.py", "")
43+
if metadata is not None:
44+
zf.writestr("cuda_core-1.3.0.dist-info/METADATA", metadata)
45+
return path
2446

2547

26-
def _build_info(major):
27-
floor = FLOORS[major]
28-
return (
29-
"# Generated by build_hooks.py at build time. Do not edit or commit.\n"
30-
f"CUDA_MAJOR = {major}\n"
31-
f"CUDA_VERSION = {major * 1000 + floor[1] * 10} # the cuda.h this build compiled against\n"
32-
f"CUDA_BINDINGS_FLOOR = {floor!r}\n"
33-
f"CUDA_BINDINGS_BUILD_VERSION = '{major}.{floor[1]}.{floor[2]}'\n"
34-
)
48+
@pytest.mark.agent_authored(model="claude-fable-5-1")
49+
@pytest.mark.parametrize(("major", "floor"), [(12, "12.9.8"), (13, "13.4.1")])
50+
def test_reads_the_floor_of_each_extra(tmp_path, major, floor):
51+
assert tool.floor_from_wheel(_wheel(tmp_path), major) == floor
3552

3653

37-
def _wheel(tmp_path, entries):
38-
path = tmp_path / "cuda_core-1.3.0-cp312-cp312-linux_x86_64.whl"
39-
with zipfile.ZipFile(path, "w") as zf:
40-
for name, major in entries.items():
41-
zf.writestr(name, _build_info(major))
42-
return path
54+
@pytest.mark.agent_authored(model="claude-fable-5-1")
55+
def test_accepts_other_spellings_of_the_requirement():
56+
metadata = 'Requires-Dist: cuda-bindings>=13.4.1,==13.*; extra == "cu13"\n'
57+
assert tool.floor_from_metadata(metadata, 13) == "13.4.1"
58+
metadata = "Requires-Dist: cuda-bindings [all] >= 13.4.1, < 14 ; extra == 'cu13'\n"
59+
assert tool.floor_from_metadata(metadata, 13) == "13.4.1"
4360

4461

4562
@pytest.mark.agent_authored(model="claude-fable-5-1")
46-
@pytest.mark.parametrize("major", [12, 13])
47-
def test_reads_the_merged_wheel_layout(tmp_path, major):
48-
wheel = _wheel(tmp_path, {"cuda/core/cu12/_build_info.py": 12, "cuda/core/cu13/_build_info.py": 13})
49-
assert tool.floor_from_wheel(wheel, major) == ".".join(map(str, FLOORS[major]))
63+
def test_ignores_lookalike_names_and_other_extras():
64+
metadata = (
65+
'Requires-Dist: cuda-bindings-extra>=1.0; extra == "cu13"\n'
66+
'Requires-Dist: cuda-bindings[all]<13,>=12.9.8; extra == "cu12"\n'
67+
'Requires-Dist: cuda-bindings[all]<14,>=13.4.1; extra == "cu13"\n'
68+
)
69+
assert tool.floor_from_metadata(metadata, 13) == "13.4.1"
5070

5171

5272
@pytest.mark.agent_authored(model="claude-fable-5-1")
53-
def test_reads_a_single_major_wheel(tmp_path):
54-
wheel = _wheel(tmp_path, {"cuda/core/_build_info.py": 13})
55-
assert tool.floor_from_wheel(wheel, 13) == "13.4.1"
73+
def test_rejects_a_missing_extra(tmp_path):
74+
with pytest.raises(SystemExit, match="no cuda-bindings requirement for the cu11 extra"):
75+
tool.floor_from_wheel(_wheel(tmp_path), 11)
5676

5777

5878
@pytest.mark.agent_authored(model="claude-fable-5-1")
59-
def test_rejects_a_single_major_wheel_of_another_major(tmp_path):
60-
wheel = _wheel(tmp_path, {"cuda/core/_build_info.py": 13})
61-
with pytest.raises(SystemExit, match="records a CUDA 13 build, not CUDA 12"):
62-
tool.floor_from_wheel(wheel, 12)
79+
def test_rejects_a_requirement_without_a_floor():
80+
with pytest.raises(SystemExit, match="without a floor for the cu13 extra"):
81+
tool.floor_from_metadata('Requires-Dist: cuda-bindings==13.*; extra == "cu13"\n', 13)
6382

6483

6584
@pytest.mark.agent_authored(model="claude-fable-5-1")
66-
def test_rejects_a_wheel_without_the_build_record(tmp_path):
67-
wheel = _wheel(tmp_path, {})
68-
with pytest.raises(SystemExit, match="contains no build for CUDA 13"):
69-
tool.floor_from_wheel(wheel, 13)
85+
def test_rejects_a_floor_of_another_major():
86+
with pytest.raises(SystemExit, match="floor of another major for the cu13 extra"):
87+
tool.floor_from_metadata('Requires-Dist: cuda-bindings>=12.9.8,<14; extra == "cu13"\n', 13)
7088

7189

7290
@pytest.mark.agent_authored(model="claude-fable-5-1")
73-
def test_rejects_a_build_record_without_the_floor():
74-
with pytest.raises(SystemExit, match="does not assign CUDA_BINDINGS_FLOOR"):
75-
tool.floor_from_source("CUDA_MAJOR = 13\n", 13)
91+
def test_rejects_a_zip_that_is_not_a_wheel(tmp_path):
92+
with pytest.raises(SystemExit, match="contains 0 METADATA files"):
93+
tool.floor_from_wheel(_wheel(tmp_path, metadata=None), 13)
7694

7795

7896
@pytest.mark.agent_authored(model="claude-fable-5-1")
7997
def test_cli_prints_the_floor(tmp_path, capsys):
80-
wheel = _wheel(tmp_path, {"cuda/core/_build_info.py": 13})
81-
assert tool.main(["--wheel", str(wheel), "--major", "13"]) == 0
98+
assert tool.main(["--wheel", str(_wheel(tmp_path)), "--major", "13"]) == 0
8299
assert capsys.readouterr().out.strip() == "13.4.1"

‎cuda_core/build_hooks.py‎

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,6 @@ def _get_cuda_path() -> str:
133133

134134

135135
_PACKAGE_DIR = Path(__file__).parent / "cuda" / "core"
136-
137-
# Generated at build time by _write_build_info() and read by cuda/core/__init__.py.
138-
_BUILD_INFO_PATH = _PACKAGE_DIR / "_build_info.py"
139-
140-
141136
_PYPROJECT_PATH = Path(__file__).parent / "pyproject.toml"
142137

143138

@@ -495,8 +490,11 @@ def _determine_cuda_major_version() -> str:
495490
return cuda_major
496491

497492

498-
def _check_build_configuration(cuda_path: str, cuda_major: str) -> None:
499-
"""Reject build configurations that cuda.core does not support, then record the build.
493+
def _check_build_configuration(cuda_path: str, cuda_major: str) -> int:
494+
"""Reject build configurations that cuda.core does not support.
495+
496+
Returns the ``CUDA_VERSION`` of the header that the installed cuda-bindings
497+
was generated from, for _build_define_macros().
500498
501499
cuda.core supports one configuration per CUDA major series. The installed
502500
cuda-bindings is at least the floor of the series, the cu<major> extra in
@@ -553,35 +551,26 @@ def _check_build_configuration(cuda_path: str, cuda_major: str) -> None:
553551
"isolated build, constrain cuda-bindings with PIP_CONSTRAINT or build with --no-build-isolation."
554552
)
555553
print(f"Build configuration: CUDA {header[0]}.{header[1]} headers, cuda-bindings {bindings_version}")
556-
_write_build_info(major, cuda_version, floor_triple, bindings_version)
554+
return bindings_cuda_version
555+
557556

557+
def _build_define_macros(cuda_major: str, bindings_cuda_version: int | None = None) -> list:
558+
"""Preprocessor macros that carry the build decision into the C++. See _cpp/rt/versions.hpp.
558559
559-
def _build_define_macros(cuda_major: str) -> list:
560-
"""Preprocessor macros that carry the build decision into the C++. See _cpp/rt/versions.hpp."""
560+
``bindings_cuda_version`` is the header that the installed cuda-bindings was
561+
generated from, as _check_build_configuration() returns it. versions.hpp
562+
fails the compile unless the ``cuda.h`` the compiler resolved has the same
563+
major.minor, so the Python read of ``cuda.h`` can never silently disagree
564+
with the compiler's.
565+
"""
561566
major = int(cuda_major)
562-
return [
567+
macros = [
563568
("CUDA_CORE_BUILD_MAJOR", str(major)),
564569
("CUDA_CORE_MIN_CUDA_VERSION", str(_load_bindings_floor().cuda_version_of(_floor_for(major)))),
565570
]
566-
567-
568-
def _write_build_info(cuda_major: int, cuda_version: int, floor: tuple, bindings_version: str) -> None:
569-
"""Record what this build compiled against, for the import-time check.
570-
571-
cuda/core/__init__.py reads this module before it selects the versioned
572-
subpackage. It refuses an installed cuda-bindings that is older than the
573-
floor or generated from an older header minor than the build's. See
574-
_bindings_floor.check_installed_bindings. Like _version.py, the file is
575-
generated, gitignored, and shipped.
576-
"""
577-
_BUILD_INFO_PATH.write_text(
578-
"# Generated by build_hooks.py at build time. Do not edit or commit.\n"
579-
f"CUDA_MAJOR = {cuda_major}\n"
580-
f"CUDA_VERSION = {cuda_version} # the cuda.h this build compiled against\n"
581-
f"CUDA_BINDINGS_FLOOR = {tuple(floor)!r}\n"
582-
f"CUDA_BINDINGS_BUILD_VERSION = {bindings_version!r} # informational, not read at import\n",
583-
encoding="utf-8",
584-
)
571+
if bindings_cuda_version is not None:
572+
macros.append(("CUDA_CORE_BINDINGS_CUDA_VERSION", str(bindings_cuda_version)))
573+
return macros
585574

586575

587576
# used later by setup()
@@ -765,7 +754,7 @@ def module_names():
765754
# _get_cuda_path() and reads cuda.h, which must not run before the
766755
# pathfinder import has repaired PEP 517 namespace shadowing.
767756
cuda_major, config_key = _check_build_config(toolchain, debug, COMPILE_FOR_COVERAGE)
768-
_check_build_configuration(cuda_path, cuda_major)
757+
bindings_cuda_version = _check_build_configuration(cuda_path, cuda_major)
769758

770759
depends = _extension_depends()
771760
ext_modules = tuple(
@@ -779,8 +768,8 @@ def module_names():
779768
]
780769
+ all_include_dirs,
781770
# The C++ branches on the CUDA major series only. _cpp/rt/versions.hpp
782-
# re-checks cuda.h against both macros. See _check_build_configuration.
783-
define_macros=_build_define_macros(cuda_major),
771+
# checks the cuda.h the compiler resolved against these macros.
772+
define_macros=_build_define_macros(cuda_major, bindings_cuda_version),
784773
language="c++",
785774
extra_compile_args=extra_compile_args,
786775
extra_link_args=extra_link_args,
@@ -789,7 +778,12 @@ def module_names():
789778
)
790779

791780
nthreads = int(os.environ.get("CUDA_PYTHON_PARALLEL_LEVEL", os.cpu_count() // 2))
792-
compile_time_env = {"CUDA_CORE_BUILD_MAJOR": int(cuda_major)}
781+
# cuda/core/_build_info.pyx records both, next to the CUDA_VERSION of the
782+
# cuda.h the compiler resolved; cuda/core/__init__.py reads them at import.
783+
compile_time_env = {
784+
"CUDA_CORE_BUILD_MAJOR": int(cuda_major),
785+
"CUDA_CORE_BINDINGS_FLOOR": tuple(_floor_for(cuda_major)),
786+
}
793787
compiler_directives = {"embedsignature": True, "warn.deprecated.IF": False, "freethreading_compatible": True}
794788
_CythonOptions.warning_errors = True
795789
if COMPILE_FOR_COVERAGE:

0 commit comments

Comments
 (0)