Skip to content

Commit f1ae40e

Browse files
committed
fixup! feat(core.utils): honor cross-patch sharing; clarify size cap + name_expressions
1 parent 1b24442 commit f1ae40e

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

cuda_core/cuda/core/utils/_program_cache.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,6 @@ def _nvvm_fingerprint() -> str:
373373
return f"lib={lib_major}.{lib_minor};ir={major}.{minor}.{debug_major}.{debug_minor}"
374374

375375

376-
def _cuda_core_version() -> str:
377-
from cuda.core._version import __version__
378-
379-
return str(__version__)
380-
381-
382376
# ProgramOptions fields that reference external files whose *contents* the
383377
# cache key cannot observe without reading the filesystem. Callers that set
384378
# any of these must supply an ``extra_digest`` covering the dependency surface
@@ -453,7 +447,7 @@ def make_program_cache_key(
453447
code_type: str,
454448
options: ProgramOptions,
455449
target_type: str,
456-
name_expressions: Sequence[str] = (),
450+
name_expressions: Sequence[str | bytes | bytearray] = (),
457451
extra_digest: bytes | None = None,
458452
) -> bytes:
459453
"""Build a stable cache key from compile inputs.
@@ -472,6 +466,11 @@ def make_program_cache_key(
472466
One of ``"ptx"``, ``"cubin"``, ``"ltoir"``.
473467
name_expressions:
474468
Optional iterable of mangled-name lookups. Order is not significant.
469+
Elements may be ``str``, ``bytes``, or ``bytearray``; ``"foo"`` and
470+
``b"foo"`` produce distinct keys because ``Program.compile`` records
471+
the original Python object as the ``ObjectCode.symbol_mapping`` key,
472+
and ``get_kernel`` lookups must use the same type the cache key
473+
recorded.
475474
extra_digest:
476475
Caller-supplied bytes mixed into the key. Required whenever
477476
:class:`cuda.core.ProgramOptions` sets any option that pulls in
@@ -669,9 +668,6 @@ def _probe(label: str, fn):
669668
return None
670669

671670
_update("schema", str(_KEY_SCHEMA_VERSION).encode("ascii"))
672-
cuda_core_ver = _probe("cuda_core", _cuda_core_version)
673-
if cuda_core_ver is not None:
674-
_update("cuda_core", cuda_core_ver.encode("ascii"))
675671
if backend == "nvrtc":
676672
nvrtc_ver = _probe("nvrtc", _nvrtc_version)
677673
if nvrtc_ver is not None:
@@ -775,9 +771,16 @@ class SQLiteProgramCache(ProgramCacheResource):
775771
Filesystem path to the sqlite3 database. The parent directory is
776772
created if missing.
777773
max_size_bytes:
778-
Optional size cap in bytes. When the sum of stored payload sizes
774+
Optional cap on the sum of stored payload sizes. When that total
779775
exceeds the cap, the least-recently-used entries are evicted until
780-
the total is at or below the cap. ``None`` means unbounded.
776+
the logical total is at or below the cap; ``None`` means unbounded.
777+
Real on-disk usage tracks the logical total *at quiescent points*:
778+
WAL frames and freed pages are reclaimed opportunistically via
779+
``wal_checkpoint(TRUNCATE)`` + ``VACUUM`` after each eviction, but
780+
``sqlite3`` skips both under active readers or writers. With
781+
concurrent access, the on-disk file can grow above the cap until
782+
readers release; :class:`FileStreamProgramCache` is the right
783+
backend for multi-process workloads with strict on-disk bounds.
781784
"""
782785

783786
def __init__(

cuda_core/tests/test_program_cache.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,26 @@ def test_make_program_cache_key_accepts_side_effect_options_for_ptx(option_kw):
855855
_make_key(code=".version 7.0", code_type="ptx", options=_opts(**option_kw)) # no raise
856856

857857

858+
@pytest.mark.parametrize(
859+
"code_type, code, target_type",
860+
[
861+
pytest.param("c++", "a", "cubin", id="nvrtc"),
862+
pytest.param("ptx", ".version 7.0", "cubin", id="linker"),
863+
pytest.param("nvvm", "abc", "ptx", id="nvvm"),
864+
],
865+
)
866+
def test_make_program_cache_key_survives_cuda_core_version_change(code_type, code, target_type, monkeypatch):
867+
"""The docstring promises cross-patch sharing within a schema version, so
868+
cuda.core's own ``__version__`` must NOT be mixed into the digest."""
869+
import cuda.core._version as _version_mod
870+
871+
monkeypatch.setattr(_version_mod, "__version__", "0.0.0")
872+
k_a = _make_key(code=code, code_type=code_type, target_type=target_type)
873+
monkeypatch.setattr(_version_mod, "__version__", "999.999.999")
874+
k_b = _make_key(code=code, code_type=code_type, target_type=target_type)
875+
assert k_a == k_b
876+
877+
858878
def test_make_program_cache_key_driver_version_does_not_perturb_ptx_under_nvjitlink(monkeypatch):
859879
"""nvJitLink does NOT route PTX compilation through cuLink, so a
860880
changing driver version must not invalidate PTX cache keys when
@@ -899,7 +919,6 @@ def _broken():
899919
"probe_name, code_type, code",
900920
[
901921
pytest.param("_nvrtc_version", "c++", "a", id="nvrtc"),
902-
pytest.param("_cuda_core_version", "c++", "a", id="cuda_core"),
903922
pytest.param("_linker_backend_and_version", "ptx", ".ptx", id="linker"),
904923
],
905924
)

0 commit comments

Comments
 (0)