Skip to content

Commit c7d7397

Browse files
committed
Centralize guard rails default mode.
Keep diagnostics and tests aligned with the configured default so future rollout changes only need one constant update. Made-with: Cursor
1 parent 95a2576 commit c7d7397

2 files changed

Lines changed: 21 additions & 11 deletions

File tree

cuda_pathfinder/cuda/pathfinder/_process_wide_compatibility_guard_rails.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
_T = TypeVar("_T")
5151
_COMPATIBILITY_GUARD_RAILS_ENV_VAR = "CUDA_PATHFINDER_COMPATIBILITY_GUARD_RAILS"
5252
_COMPATIBILITY_GUARD_RAILS_MODES = ("off", "best_effort", "strict")
53+
_COMPATIBILITY_GUARD_RAILS_DEFAULT_MODE = "strict"
54+
assert _COMPATIBILITY_GUARD_RAILS_DEFAULT_MODE in _COMPATIBILITY_GUARD_RAILS_MODES
5355

5456

5557
class _ProcessWideGuardRailsApi(Protocol):
@@ -80,13 +82,14 @@ class _PublicPathfinderModule(Protocol):
8082
def _compatibility_guard_rails_mode() -> str:
8183
value = os.environ.get(_COMPATIBILITY_GUARD_RAILS_ENV_VAR)
8284
if not value:
83-
return "strict"
85+
return _COMPATIBILITY_GUARD_RAILS_DEFAULT_MODE
8486
if value in _COMPATIBILITY_GUARD_RAILS_MODES:
8587
return value
8688
allowed_values = ", ".join(repr(mode) for mode in _COMPATIBILITY_GUARD_RAILS_MODES)
8789
raise RuntimeError(
8890
f"Invalid {_COMPATIBILITY_GUARD_RAILS_ENV_VAR}={value!r}. "
89-
f"Allowed values: {allowed_values}. Unset or empty defaults to 'strict'."
91+
f"Allowed values: {allowed_values}. "
92+
f"Unset or empty defaults to {_COMPATIBILITY_GUARD_RAILS_DEFAULT_MODE!r}."
9093
)
9194

9295

cuda_pathfinder/tests/test_compatibility_guard_rails.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,27 +261,33 @@ def fail_raw_fallback(_libname: str) -> LoadedDL:
261261

262262

263263
@pytest.mark.parametrize("env_value", [None, ""])
264-
def test_public_apis_default_to_strict_when_env_var_is_unset_or_empty(monkeypatch, tmp_path, env_value):
265-
lib_path = _touch(tmp_path / "no-cuda-h" / "targets" / "x86_64-linux" / "lib" / "libnvrtc.so.12")
264+
def test_public_apis_default_mode_applies_when_env_var_is_unset_or_empty(monkeypatch, tmp_path, env_value):
265+
guarded_lib_path = _touch(tmp_path / "no-cuda-h" / "targets" / "x86_64-linux" / "lib" / "libnvrtc.so.12")
266+
raw_loaded = _loaded_dl("/opt/mock/libnvrtc.so.12", found_via="system-search")
266267

267-
monkeypatch.setattr(compatibility_module, "_load_nvidia_dynamic_lib", lambda _libname: _loaded_dl(lib_path))
268+
monkeypatch.setattr(
269+
compatibility_module, "_load_nvidia_dynamic_lib", lambda _libname: _loaded_dl(guarded_lib_path)
270+
)
271+
monkeypatch.setattr(process_wide_module, "_load_nvidia_dynamic_lib", lambda _libname: raw_loaded)
268272
monkeypatch.setattr(
269273
pathfinder,
270274
"process_wide_compatibility_guard_rails",
271275
CompatibilityGuardRails(driver_cuda_version=_driver_cuda_version(13000)),
272276
)
273277

274-
def fail_raw_fallback(_libname: str) -> LoadedDL:
275-
pytest.fail("strict mode must not fall back to raw loading")
276-
277-
monkeypatch.setattr(process_wide_module, "_load_nvidia_dynamic_lib", fail_raw_fallback)
278278
if env_value is None:
279279
monkeypatch.delenv(COMPATIBILITY_GUARD_RAILS_ENV_VAR, raising=False)
280280
else:
281281
monkeypatch.setenv(COMPATIBILITY_GUARD_RAILS_ENV_VAR, env_value)
282282

283-
with pytest.raises(CompatibilityInsufficientMetadataError, match="cuda.h"):
284-
pathfinder.load_nvidia_dynamic_lib("nvrtc")
283+
default_mode = process_wide_module._COMPATIBILITY_GUARD_RAILS_DEFAULT_MODE
284+
if default_mode == "strict":
285+
with pytest.raises(CompatibilityInsufficientMetadataError, match="cuda.h"):
286+
pathfinder.load_nvidia_dynamic_lib("nvrtc")
287+
return
288+
289+
loaded = pathfinder.load_nvidia_dynamic_lib("nvrtc")
290+
assert loaded is raw_loaded
285291

286292

287293
def test_public_apis_best_effort_fall_back_on_insufficient_metadata(monkeypatch, tmp_path):
@@ -329,6 +335,7 @@ def test_public_apis_reject_invalid_guard_rails_mode(monkeypatch):
329335
assert "'off'" in message
330336
assert "'best_effort'" in message
331337
assert "'strict'" in message
338+
assert f"defaults to {process_wide_module._COMPATIBILITY_GUARD_RAILS_DEFAULT_MODE!r}" in message
332339

333340

334341
def test_public_apis_share_process_wide_guard_rails_state(monkeypatch, tmp_path):

0 commit comments

Comments
 (0)