-
Notifications
You must be signed in to change notification settings - Fork 259
refactor(pathfinder): unify dynamic-lib subprocess probing (production, test code) #1779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+330
−183
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
776e53c
refactor(pathfinder): unify dynamic-lib subprocess probing
rwgk 336a8b6
refactor(pathfinder-tests): avoid double parse
rwgk 77c04a5
refactor(pathfinder): streamline canary probe return
rwgk fd29ada
slim subprocess helpers (the removed check in production code were to…
rwgk 7533cb0
test(pathfinder): cover canary subprocess mode
rwgk 31b1410
Merge branch 'main' into pathfinder_subprocess_unification
rwgk df1ae30
docs(pathfinder): explain subprocess entrypoint scope
rwgk 79d6eeb
Minor simplification of mypy workaround
rwgk 3c95862
inline subprocess payload parse for simplicity
rwgk 4ad5192
refactor(pathfinder-tests): include not-found error details
rwgk e3c94e4
Merge branch 'main' into pathfinder_subprocess_unification
rwgk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
43 changes: 0 additions & 43 deletions
43
cuda_pathfinder/cuda/pathfinder/_dynamic_libs/canary_probe_subprocess.py
This file was deleted.
Oops, something went wrong.
127 changes: 127 additions & 0 deletions
127
cuda_pathfinder/cuda/pathfinder/_dynamic_libs/dynamic_lib_subprocess.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #!/usr/bin/env python | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import sys | ||
| from collections.abc import Sequence | ||
|
|
||
| from cuda.pathfinder._dynamic_libs.lib_descriptor import LIB_DESCRIPTORS | ||
| from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL | ||
| from cuda.pathfinder._dynamic_libs.platform_loader import LOADER | ||
| from cuda.pathfinder._dynamic_libs.subprocess_protocol import ( | ||
| MODE_CANARY, | ||
| MODE_LOAD, | ||
| STATUS_NOT_FOUND, | ||
| STATUS_OK, | ||
| VALID_MODES, | ||
| format_dynamic_lib_subprocess_payload, | ||
| ) | ||
|
|
||
| # NOTE: The main entrypoint (below) serves both production (canary probe) | ||
| # and tests (full loader). Keeping them together ensures a single subprocess | ||
| # protocol and CLI surface, so the test subprocess stays aligned with the | ||
| # production flow while avoiding a separate test-only module. | ||
| # Any production-code impact is negligible since the extra logic only runs | ||
| # in the subprocess entrypoint and only in test mode. | ||
|
|
||
|
|
||
| def _probe_canary_abs_path(libname: str) -> str | None: | ||
| desc = LIB_DESCRIPTORS.get(libname) | ||
| if desc is None: | ||
| raise ValueError(f"Unsupported canary library name: {libname!r}") | ||
| try: | ||
| loaded: LoadedDL | None = LOADER.load_with_system_search(desc) | ||
| except DynamicLibNotFoundError: | ||
| return None | ||
| if loaded is None: | ||
| return None | ||
| abs_path: str | None = loaded.abs_path | ||
| return abs_path | ||
|
|
||
|
|
||
| def _validate_abs_path(abs_path: str) -> None: | ||
| assert abs_path, f"empty path: {abs_path=!r}" | ||
| assert os.path.isabs(abs_path), f"not absolute: {abs_path=!r}" | ||
| assert os.path.isfile(abs_path), f"not a file: {abs_path=!r}" | ||
|
|
||
|
|
||
| def _load_nvidia_dynamic_lib_for_test(libname: str) -> str: | ||
| """Test-only loader used by the subprocess entrypoint.""" | ||
| # Keep imports inside the subprocess body so startup stays focused on the | ||
| # code under test rather than the parent test module. | ||
| from cuda.pathfinder import load_nvidia_dynamic_lib | ||
| from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import _load_lib_no_cache | ||
| from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import ( | ||
| SUPPORTED_LINUX_SONAMES, | ||
| SUPPORTED_WINDOWS_DLLS, | ||
| ) | ||
| from cuda.pathfinder._utils.platform_aware import IS_WINDOWS | ||
|
|
||
| loaded_dl_fresh = load_nvidia_dynamic_lib(libname) | ||
| if loaded_dl_fresh.was_already_loaded_from_elsewhere: | ||
| raise RuntimeError("loaded_dl_fresh.was_already_loaded_from_elsewhere") | ||
|
|
||
| abs_path = loaded_dl_fresh.abs_path | ||
| if not isinstance(abs_path, str): | ||
| raise RuntimeError(f"loaded_dl_fresh.abs_path is not a string: {abs_path!r}") | ||
| _validate_abs_path(abs_path) | ||
| assert loaded_dl_fresh.found_via is not None | ||
|
|
||
| loaded_dl_from_cache = load_nvidia_dynamic_lib(libname) | ||
| if loaded_dl_from_cache is not loaded_dl_fresh: | ||
| raise RuntimeError("loaded_dl_from_cache is not loaded_dl_fresh") | ||
|
|
||
| loaded_dl_no_cache = _load_lib_no_cache(libname) | ||
| supported_libs = SUPPORTED_WINDOWS_DLLS if IS_WINDOWS else SUPPORTED_LINUX_SONAMES | ||
| if not loaded_dl_no_cache.was_already_loaded_from_elsewhere and libname in supported_libs: | ||
| raise RuntimeError("not loaded_dl_no_cache.was_already_loaded_from_elsewhere") | ||
| abs_path_no_cache = loaded_dl_no_cache.abs_path | ||
| if not isinstance(abs_path_no_cache, str): | ||
| raise RuntimeError(f"loaded_dl_no_cache.abs_path is not a string: {abs_path_no_cache!r}") | ||
| if not os.path.samefile(abs_path_no_cache, abs_path): | ||
| raise RuntimeError(f"not os.path.samefile({abs_path_no_cache=!r}, {abs_path=!r})") | ||
| _validate_abs_path(abs_path_no_cache) | ||
| return abs_path | ||
|
|
||
|
|
||
| def probe_dynamic_lib_and_print_json(libname: str, mode: str) -> None: | ||
| if mode == MODE_CANARY: | ||
| abs_path = _probe_canary_abs_path(libname) | ||
| status = STATUS_OK if abs_path is not None else STATUS_NOT_FOUND | ||
| print(format_dynamic_lib_subprocess_payload(status, abs_path)) | ||
| return | ||
|
|
||
| if mode == MODE_LOAD: | ||
| # Test-only path: exercises full loader behavior in isolation. | ||
| try: | ||
| abs_path = _load_nvidia_dynamic_lib_for_test(libname) | ||
| except DynamicLibNotFoundError as exc: | ||
| error = { | ||
| "type": exc.__class__.__name__, | ||
| "message": str(exc), | ||
| } | ||
| print(format_dynamic_lib_subprocess_payload(STATUS_NOT_FOUND, None, error=error)) | ||
| return | ||
| print(format_dynamic_lib_subprocess_payload(STATUS_OK, abs_path)) | ||
| return | ||
|
|
||
| raise ValueError(f"Unsupported subprocess probe mode: {mode!r}") | ||
|
|
||
|
|
||
| def main(argv: Sequence[str] | None = None) -> int: | ||
| args = list(sys.argv[1:] if argv is None else argv) | ||
| if len(args) != 2 or args[0] not in VALID_MODES: | ||
| modes = ", ".join(VALID_MODES) | ||
| raise SystemExit( | ||
| f"Usage: python -m cuda.pathfinder._dynamic_libs.dynamic_lib_subprocess <mode> <libname>\nModes: {modes}" | ||
| ) | ||
| mode, libname = args | ||
| probe_dynamic_lib_and_print_json(libname, mode) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
cuda_pathfinder/cuda/pathfinder/_dynamic_libs/subprocess_protocol.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import sys | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Literal | ||
|
|
||
| MODE_CANARY: Literal["canary"] = "canary" | ||
| MODE_LOAD: Literal["load"] = "load" | ||
| VALID_MODES: tuple[Literal["canary"], Literal["load"]] = (MODE_CANARY, MODE_LOAD) | ||
|
|
||
| STATUS_OK: Literal["ok"] = "ok" | ||
| STATUS_NOT_FOUND: Literal["not-found"] = "not-found" | ||
|
|
||
| DYNAMIC_LIB_SUBPROCESS_MODULE = "cuda.pathfinder._dynamic_libs.dynamic_lib_subprocess" | ||
| DYNAMIC_LIB_SUBPROCESS_CWD = Path(__file__).resolve().parents[3] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class DynamicLibSubprocessPayload: | ||
| status: Literal["ok", "not-found"] | ||
| abs_path: str | None | ||
|
|
||
|
|
||
| def format_dynamic_lib_subprocess_payload( | ||
| status: Literal["ok", "not-found"], | ||
| abs_path: str | None, | ||
| *, | ||
| error: dict[str, str] | None = None, | ||
| ) -> str: | ||
| payload: dict[str, object] = {"status": status, "abs_path": abs_path} | ||
| if error is not None: | ||
| payload["error"] = error | ||
| return json.dumps(payload) | ||
|
|
||
|
|
||
| def build_dynamic_lib_subprocess_command(mode: str, libname: str) -> list[str]: | ||
| return [sys.executable, "-m", DYNAMIC_LIB_SUBPROCESS_MODULE, mode, libname] | ||
|
|
||
|
|
||
| def parse_dynamic_lib_subprocess_payload( | ||
| stdout: str, | ||
| *, | ||
| libname: str, | ||
| error_label: str, | ||
| ) -> DynamicLibSubprocessPayload: | ||
| # Use the final non-empty line in case earlier output lines are emitted. | ||
| lines = [line for line in stdout.splitlines() if line.strip()] | ||
| if not lines: | ||
| raise RuntimeError(f"{error_label} produced no stdout payload for {libname!r}") | ||
| try: | ||
| payload = json.loads(lines[-1]) | ||
| except json.JSONDecodeError: | ||
| raise RuntimeError(f"{error_label} emitted invalid JSON payload for {libname!r}: {lines[-1]!r}") from None | ||
| if not isinstance(payload, dict): | ||
| raise RuntimeError(f"{error_label} emitted unexpected payload for {libname!r}: {payload!r}") | ||
| status = payload.get("status") | ||
| abs_path = payload.get("abs_path") | ||
| if status == STATUS_OK: | ||
| if not isinstance(abs_path, str): | ||
| raise RuntimeError(f"{error_label} emitted unexpected payload for {libname!r}: {payload!r}") | ||
| return DynamicLibSubprocessPayload(status=STATUS_OK, abs_path=abs_path) | ||
| if status == STATUS_NOT_FOUND: | ||
| if abs_path is not None: | ||
| raise RuntimeError(f"{error_label} emitted unexpected payload for {libname!r}: {payload!r}") | ||
| return DynamicLibSubprocessPayload(status=STATUS_NOT_FOUND, abs_path=None) | ||
| raise RuntimeError(f"{error_label} emitted unexpected payload for {libname!r}: {payload!r}") |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels super weird to me to have in the actual library code.