Skip to content

Commit c460771

Browse files
committed
Test native cuRAND availability in a fresh sample process
— codex GPT-6 on behalf of Ralf — PyTorch can load cuRAND into the pytest parent on Windows without making that DLL discoverable by sample subprocesses. Probe with the current interpreter in a fresh process so parent-only libraries do not admit samples that cannot run. Preserve diagnostics for unexpected loader errors. Cover the process boundary with real child imports and retain local Toolkit sample execution. Signed-off-by: Ralf W. Grosse-Kunstleve <[email protected]>
1 parent c17a310 commit c460771

2 files changed

Lines changed: 57 additions & 32 deletions

File tree

‎cuda_core/tests/example_tests/test_run_samples.py‎

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -462,25 +462,36 @@ def test_cupy_cuda_requirement_accepts_conda_cupy_distribution(tmp_path: Path, i
462462
assert requested == ["cupy-cuda13x", "cupy"]
463463

464464

465-
@pytest.mark.parametrize("available", [True, False], ids=["local-library", "missing-library"])
466-
@pytest.mark.agent_authored(model="gpt-6")
467-
def test_unversioned_curand_requirement_accepts_native_library(
468-
tmp_path: Path, installed_distributions, monkeypatch: pytest.MonkeyPatch, available: bool
469-
) -> None:
470-
pathfinder = ModuleType("cuda.pathfinder")
465+
@pytest.fixture
466+
def native_pathfinder(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
467+
modules = tmp_path / "native_modules"
468+
cuda = modules / "cuda"
469+
cuda.mkdir(parents=True)
470+
(cuda / "__init__.py").write_text("", encoding="utf-8")
471+
monkeypatch.setenv("PYTHONPATH", str(modules))
472+
473+
def provide(body: str) -> None:
474+
(cuda / "pathfinder.py").write_text(
475+
"class DynamicLibNotFoundError(RuntimeError):\n pass\n\n"
476+
"def load_nvidia_dynamic_lib(name):\n"
477+
" assert name == 'curand'\n"
478+
f" {body}\n",
479+
encoding="utf-8",
480+
)
471481

472-
class DynamicLibNotFoundError(RuntimeError):
473-
pass
482+
return provide
474483

475-
def load_library(name: str) -> object:
476-
assert name == "curand"
477-
if not available:
478-
raise DynamicLibNotFoundError("cuRAND is not installed")
479-
return object()
480484

481-
pathfinder.DynamicLibNotFoundError = DynamicLibNotFoundError
482-
pathfinder.load_nvidia_dynamic_lib = load_library
483-
monkeypatch.setattr(run_samples._runner.importlib, "import_module", lambda _name: pathfinder)
485+
@pytest.mark.parametrize("available", [True, False], ids=["local-library", "parent-only-library"])
486+
@pytest.mark.agent_authored(model="gpt-6")
487+
def test_unversioned_curand_requirement_checks_the_sample_process(
488+
tmp_path: Path, installed_distributions, monkeypatch: pytest.MonkeyPatch, native_pathfinder, available: bool
489+
) -> None:
490+
native_pathfinder("return object()" if available else "raise DynamicLibNotFoundError('cuRAND is not installed')")
491+
parent = ModuleType("cuda.pathfinder")
492+
parent.DynamicLibNotFoundError = RuntimeError
493+
parent.load_nvidia_dynamic_lib = lambda _name: object()
494+
monkeypatch.setitem(sys.modules, "cuda.pathfinder", parent)
484495

485496
missing = run_samples.missing_dependencies(_sample(tmp_path, "['nvidia-curand']"))
486497

@@ -489,30 +500,35 @@ def load_library(name: str) -> object:
489500

490501
@pytest.mark.agent_authored(model="gpt-6")
491502
def test_native_curand_check_tolerates_missing_pathfinder(
492-
tmp_path: Path, installed_distributions, monkeypatch: pytest.MonkeyPatch
503+
tmp_path: Path, installed_distributions, native_pathfinder
493504
) -> None:
494-
def import_module(name: str) -> ModuleType:
495-
raise ModuleNotFoundError(name=name)
496-
497-
monkeypatch.setattr(run_samples._runner.importlib, "import_module", import_module)
498-
499505
missing = run_samples.missing_dependencies(_sample(tmp_path, "['nvidia-curand']"))
500506

501507
assert len(missing) == 1
502508
assert "no provider distribution installed" in missing[0]
503509

504510

511+
@pytest.mark.agent_authored(model="gpt-6")
512+
def test_native_curand_check_propagates_unexpected_errors(native_pathfinder) -> None:
513+
native_pathfinder("raise RuntimeError('broken library probe')")
514+
515+
with pytest.raises(subprocess.CalledProcessError) as exc:
516+
run_samples._runner._native_curand_available()
517+
518+
assert "RuntimeError: broken library probe" in exc.value.stderr
519+
520+
505521
@pytest.mark.parametrize(
506522
"requirement", ["nvidia-curand>=11", "nvidia-curand[extra]", "nvidia-curand @ https://example.com/curand.whl"]
507523
)
508524
@pytest.mark.agent_authored(model="gpt-6")
509525
def test_curand_constraints_require_a_provider_distribution(
510526
tmp_path: Path, installed_distributions, monkeypatch: pytest.MonkeyPatch, requirement: str
511527
) -> None:
512-
def unexpected_import(name: str) -> ModuleType:
528+
def unexpected_probe() -> bool:
513529
raise AssertionError("constrained requirements must use distribution metadata")
514530

515-
monkeypatch.setattr(run_samples._runner.importlib, "import_module", unexpected_import)
531+
monkeypatch.setattr(run_samples._runner, "_native_curand_available", unexpected_probe)
516532

517533
missing = run_samples.missing_dependencies(_sample(tmp_path, f"[{requirement!r}]"))
518534

‎cuda_python_test_helpers/cuda_python_test_helpers/sample_runner.py‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,25 @@ def _distribution_mismatches(requirement: Requirement, dist: importlib.metadata.
336336

337337

338338
def _native_curand_available() -> bool:
339-
try:
340-
pathfinder = importlib.import_module("cuda.pathfinder")
341-
except ModuleNotFoundError as exc:
342-
if exc.name in {"cuda", "cuda.pathfinder"}:
343-
return False
339+
# Samples cannot inherit libraries loaded into the parent by packages such as PyTorch.
340+
code = f"""\
341+
try:
342+
import cuda.pathfinder as pathfinder
343+
except ModuleNotFoundError as exc:
344+
if exc.name not in {{"cuda", "cuda.pathfinder"}}:
344345
raise
345-
try:
346-
pathfinder.load_nvidia_dynamic_lib("curand")
347-
except pathfinder.DynamicLibNotFoundError:
346+
raise SystemExit({EXIT_WAIVED})
347+
try:
348+
pathfinder.load_nvidia_dynamic_lib("curand")
349+
except pathfinder.DynamicLibNotFoundError:
350+
raise SystemExit({EXIT_WAIVED})
351+
"""
352+
result = subprocess.run( # noqa: S603 - current interpreter and fixed probe code
353+
[sys.executable, "-c", code], capture_output=True, text=True, check=False
354+
)
355+
if result.returncode == EXIT_WAIVED:
348356
return False
357+
result.check_returncode()
349358
return True
350359

351360

0 commit comments

Comments
 (0)