Skip to content

Commit 16a3a57

Browse files
authored
WIP: path_finder Search Priority v2 (#604)
* WIP (search priority updated in README.md but not in code) * Completely replace cuda_paths.py to achieve the desired Search Priority (see updated README.md). * Define `IS_WINDOWS = sys.platform == "win32"` in supported_libs.py * Use os.path.samefile() to resolve issues with doubled backslashes. * `load_in_subprocess(): Pass current environment * Add run_python_code_safely.py as generated by perplexity, plus ruff format, bandit nosec * Replace subprocess.run with run_python_code_safely * Factor out `class Worker` to fix pickle issue. * ChatGPT revisions based on Deep research: https://chatgpt.com/share/681914ce-f274-8008-9e9f-4538716b4ed7 * Fix race condition in result queue handling by using timeout-based get() The previous implementation checked result_queue.empty() before calling get(), which introduces a classic race condition: the queue may become non-empty immediately after the check, resulting in missed results or misleading errors. This patch replaces the empty() check with result_queue.get(timeout=1.0), allowing the parent process to robustly wait for results with a bounded delay. Also switches from ctx.SimpleQueue() to ctx.Queue() for compatibility with timeout-based get(), which SimpleQueue does not support on Python ≤3.12. Note: The race condition was discovered by Gemini 2.5 * Resolve SIM108 * Change to "nppc" as ANCHOR_LIBNAME * Implement CUDA_PYTHON_CUDA_HOME_PRIORITY first, last, with default first * Remove retry_with_anchor_abs_path() and make retry_with_cuda_home_priority_last() the default. * Update README.md to reflect new search priority * SUPPORTED_LINUX_SONAMES does not need updates for CTK 12.9.0 * The only addition to SUPPORTED_WINDOWS_DLLS for CTK 12.9.0 is nvvm70.dll * Make OSError in load_dl_windows.py abs_path_for_dynamic_library() more informative. * run_cuda_bindings_path_finder.py: optionally use args as libnames (to aid debugging) * Bug fix in load_dl_windows.py: ctypes.windll.kernel32.LoadLibraryW() returns an incompatible `handle`. Use win32api.LoadLibraryEx() instead to ensure self-consistency. * Remove _find_nvidia_dynamic_library.retry_with_anchor_abs_path() method. Move run_python_code_safely.py to test/ directory.
1 parent cb31b55 commit 16a3a57

11 files changed

+205
-827
lines changed

cuda_bindings/cuda/bindings/_path_finder/README.md

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,27 @@ strategy for locating NVIDIA shared libraries:
2424
The absolute path of the already loaded library will be returned, along
2525
with the handle to the library.
2626

27-
1. **Python Package Ecosystem**
28-
- Scans `sys.path` to find libraries installed via NVIDIA Python wheels.
27+
1. **NVIDIA Python wheels**
28+
- Scans all site-packages to find libraries installed via NVIDIA Python wheels.
2929

30-
2. **Conda Environments**
31-
- Leverages Conda-specific paths through our fork of `get_cuda_paths()`
32-
from numba-cuda.
33-
34-
3. **Environment variables**
35-
- Relies on `CUDA_HOME`/`CUDA_PATH` environment variables if set.
36-
37-
4. **System Installations**
38-
- Checks traditional system locations through these paths:
39-
- Linux: `/usr/local/cuda/lib64`
40-
- Windows: `C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y\bin`
41-
(where X.Y is the CTK version)
42-
- **Notably does NOT search**:
43-
- Versioned CUDA directories like `/usr/local/cuda-12.3`
44-
- Distribution-specific packages (RPM/DEB)
45-
EXCEPT Debian's `nvidia-cuda-toolkit`
46-
47-
5. **OS Default Mechanisms**
30+
2. **OS default mechanisms / Conda environments**
4831
- Falls back to native loader:
4932
- `dlopen()` on Linux
5033
- `LoadLibraryW()` on Windows
34+
- CTK installations with system config updates are expected to be discovered:
35+
- Linux: Via `/etc/ld.so.conf.d/*cuda*.conf`
36+
- Windows: Via `C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vX.Y\bin` on system `PATH`
37+
- Conda installations are expected to be discovered:
38+
- Linux: Via `$ORIGIN/../lib` on `RPATH` (of the `python` binary)
39+
- Windows: Via `%CONDA_PREFIX%\Library\bin` on system `PATH`
40+
41+
3. **Environment variables**
42+
- Relies on `CUDA_HOME` or `CUDA_PATH` environment variables if set
43+
(in that order).
5144

5245
Note that the search is done on a per-library basis. There is no centralized
5346
mechanism that ensures all libraries are found in the same way.
5447

55-
## Implementation Philosophy
56-
57-
The current implementation balances stability and evolution:
58-
59-
- **Baseline Foundation:** Uses a fork of numba-cuda's `cuda_paths.py` that has been
60-
battle-tested in production environments.
61-
62-
- **Validation Infrastructure:** Comprehensive CI testing matrix being developed to cover:
63-
- Various Linux/Windows environments
64-
- Python packaging formats (wheels, conda)
65-
- CUDA Toolkit versions
66-
67-
- **Roadmap:** Planned refactoring to:
68-
- Unify library discovery logic
69-
- Improve maintainability
70-
- Better enforce search priority
71-
- Expand platform support
72-
7348
## Maintenance Requirements
7449

7550
These key components must be updated for new CUDA Toolkit releases:

0 commit comments

Comments
 (0)