Skip to content

build: add opt-in Cython generated-source cache - #2933

Merged
juenglin merged 1 commit into
NVIDIA:mainfrom
juenglin:opt-in-cython-cache
Sep 29, 2026
Merged

juenglin merged 1 commit into
NVIDIA:mainfrom
juenglin:opt-in-cython-cache

Conversation

@juenglin

@juenglin juenglin commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on top of #2903 (toolchain-override-backend). Merge after #2903.

Summary

Adds an opt-in Cython generated-source cache behind CUDA_PYTHON_CYTHON_CACHE_DIR for cuda.bindings and cuda.core builds. When unset, cythonize() is called without cache= and builds are unchanged. POSIX only; on Windows the helper returns None with a warning.

Two problems it works around:

  • Cache namespace (_cython_cache_path): Cython's native fingerprint omits compiler_directives ([BUG] Cache fingerprinting ignores compiler_directives cython/cython#7532). The helper namespaces the cache by package, Python version, and a SHA-256 of output-affecting config (compiler_directives, compile_time_env, language_level, cplus, debug, cuda_major). The helper and its workaround-specific tests can be removed once #7532 is fixed in a released Cython version covered by cuda-python's minimum.
  • Stable dependency paths (_stable_cython_alias): Cython hashes the absolute path of each resolved .pxd, and PEP 517 installs land under randomized temp prefixes. Atomic package-local directory symlinks (.cython-stdlib, and .cython-bindings for cuda.core) give Cython stable relative include paths; created before cythonize() and removed in finally.

Sync mechanism

The two helpers are vendored in both build_hooks.py files (PEP 517 isolation forbids a shared import). Drift is enforced by the existing pre-commit hook toolshed/check_build_hooks_sync.py, which now checks one merged "shared build helpers" block (toolchain + cache helpers) instead of the toolchain-only block. There is no runtime drift test.

Tests

Shared tests live in cuda_python_test_helpers/cuda_python_test_helpers/cython_cache.py (mixins, miss/hit smoke test, cross-isolated-env cache-hit regression) and are exercised by both packages' tests/test_build_hooks.py. POSIX-only tests skip on Windows; a Windows-only test asserts the set-env warn + None path.

Notes

  • cuda.core keeps its existing per-configuration build_dir (cu{major}-{toolchain}-{debug|opt}[-cov]); only cache= / include_path were added.
  • CUDA_PYTHON_CYTHON_CACHE_DIR is intentionally not documented in environment_variables.rst; it joins CUDA_PYTHON_TOOLCHAIN under the "no support guarantee" comment in both build_hooks.py files.
  • Expected to be opted into only by persistent (e.g. RISC-V) runners; ephemeral runners gain little since Cython's cache is disk-only.

@copy-pr-bot

copy-pr-bot Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions github-actions Bot added cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module labels Sep 22, 2026
@juenglin juenglin added CI/CD CI/CD infrastructure enhancement Any code-related improvements labels Sep 22, 2026
@juenglin juenglin added this to the cuda.bindings next milestone Sep 22, 2026
@juenglin juenglin self-assigned this Sep 22, 2026
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test 41c8db9

@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test c929252

@juenglin
juenglin requested a review from rwgk September 22, 2026 21:55
@juenglin
juenglin marked this pull request as ready for review September 22, 2026 21:56
@github-actions

This comment has been minimized.

@rwgk rwgk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving, based on several stages of reviewing with codex gpt-5.6-sol medium. There are no findings anymore.

Add an opt-in Cython cache behind CUDA_PYTHON_CYTHON_CACHE_DIR for
cuda.bindings and cuda.core builds. When unset, cythonize() is called
without cache= and builds are unchanged. POSIX only; Windows returns
None with a warning.

_cython_cache_path namespaces the cache by package, Python version, and
a SHA-256 of output-affecting config (compiler_directives,
compile_time_env, language_level, cplus, debug, cuda_major). This works
around cython/cython#7532 (Cython omits compiler_directives from its native
fingerprint); the helper and its tests can be removed once that issue is
fixed in a released Cython version covered by cuda-python's minimum.

_stable_cython_alias creates package-local directory symlinks
(.cython-stdlib, and .cython-bindings for cuda.core) before cythonize()
and removes them in finally, giving Cython stable relative include paths
across PEP 517 builds that install deps under randomized temp prefixes.

The two helpers are vendored in both build_hooks.py files (PEP 517
isolation forbids a shared import) and kept in sync by the existing
pre-commit hook toolshed/check_build_hooks_sync.py, now covering one
merged "shared build helpers" block instead of the toolchain-only block.
No runtime drift test.

Tests live in cuda_python_test_helpers/cython_cache.py (shared mixins +
miss/hit smoke test + cross-isolated-env cache-hit regression) and are
exercised by both packages' tests/test_build_hooks.py. POSIX-only tests
skip on Windows; a Windows-only test asserts the set-env warn+None path.
@juenglin
juenglin changed the base branch from toolchain-override-backend to main September 29, 2026 17:13
@juenglin

Copy link
Copy Markdown
Contributor Author

/ok to test ba7e14f

@juenglin
juenglin enabled auto-merge (squash) September 29, 2026 18:19
@juenglin
juenglin merged commit e593284 into NVIDIA:main Sep 29, 2026
221 of 223 checks passed
@github-actions

Copy link
Copy Markdown
Contributor
Doc Preview CI
Preview removed because the pull request was closed or merged.

leofang added a commit to leofang/cuda-python that referenced this pull request Sep 29, 2026
Resolves conflicts with NVIDIA#2933 (Cython generated-source cache). Ralf's
_cython_cache_path and _stable_cython_alias helpers were vendored in
both build_hooks.py files behind the # --- begin/end shared build helpers
markers his PR renamed. Moved both into cuda_bindings/_build_shared.py
alongside the toolchain block; both build_hooks.py now import them
via `from _build_shared import ...` (no vendoring, no sync check).

Conflict resolution:
- cuda_bindings/build_hooks.py, cuda_core/build_hooks.py: keep the
  from-_build_shared import structure; extend the import list with
  _cython_cache_path and _stable_cython_alias.
- cuda_bindings/_build_shared.py: add the two helpers plus the imports
  they need (contextlib, hashlib, uuid, warn).
- cuda_bindings/build_hooks.py: drop hashlib (moved), keep contextlib
  (still used for contextlib.suppress), re-add Path from pathlib for
  the Cython.__file__ path handling in _build_cuda_bindings.
- cuda_core/build_hooks.py: drop contextlib, hashlib, uuid, warn (all
  moved).
- .pre-commit-config.yaml: keep my removal of the check-build-hooks-sync
  entry (script is deleted).
- toolshed/check_build_hooks_sync.py: keep my deletion (single source
  of truth needs no drift check).
- Auto-merged test files needed no manual touch; both suites now include
  Ralf's cython_cache tests and pass (33 bindings, 71 core).

Verified after resolution:
- ruff check + format: clean.
- SPDX: clean.
- Sdists for both packages contain the resolved 8579-byte
  _build_shared.py matching the canonical.
Andy-Jost added a commit to Andy-Jost/cuda-python that referenced this pull request Sep 30, 2026
…untime-floor

Conflicts: the toolchain and Cython-cache helpers from NVIDIA#2903 and NVIDIA#2933 landed
next to this branch's floor and header-check helpers in both build_hooks.py
files and their tests. Both sets are kept. cuda.core's build now stamps the
build configuration (main) and checks the cuda-bindings floor and header
(this branch) at the same point; the cuda-bindings tests file merges main's
toolchain tests with this branch's header-check tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI/CD CI/CD infrastructure cuda.bindings Everything related to the cuda.bindings module cuda.core Everything related to the cuda.core module enhancement Any code-related improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants