Preload a single library.
(module_name: str, lib_name: str)
| 10 | |
| 11 | |
| 12 | def _preload_cuda_lib(module_name: str, lib_name: str): |
| 13 | """Preload a single library.""" |
| 14 | try: |
| 15 | module = importlib.import_module(module_name) |
| 16 | except ImportError: |
| 17 | return |
| 18 | else: |
| 19 | # TODO: update the logic to handle CUDA 13, |
| 20 | # using as reference https://github.com/pytorch/pytorch/pull/163661. |
| 21 | |
| 22 | # Resolve the library directory robustly |
| 23 | if module_file_path := getattr(module, "__file__", None): |
| 24 | lib_dir = Path(module_file_path).parent |
| 25 | elif paths := getattr(module, "__path__", None): |
| 26 | # Implicit namespace packages have __path__ but no __file__ |
| 27 | lib_dir = Path(list(paths)[0]) |
| 28 | else: |
| 29 | return |
| 30 | # Find the first file matching the pattern |
| 31 | if lib_path := next((lib_dir / "lib").glob(lib_name), None): |
| 32 | with contextlib.suppress(OSError): |
| 33 | ctypes.CDLL(str(lib_path)) |
| 34 | |
| 35 | |
| 36 | def _preload_cuda_deps(): |
no outgoing calls
no test coverage detected