Lazily load and cache the CUDA/ROCm runtime library, or None for CPU fallback.
()
| 34 | |
| 35 | |
| 36 | def _get_copy_lib() -> Optional[ctypes.CDLL]: |
| 37 | """Lazily load and cache the CUDA/ROCm runtime library, or None for CPU fallback.""" |
| 38 | global _copy_lib |
| 39 | if _copy_lib is _copy_lib_NOT_LOADED: |
| 40 | # Try to load GPU runtime libraries in priority order: CUDA first, then ROCm |
| 41 | # TODO: ROCm path to be validated on real device |
| 42 | for name, fallback in [ |
| 43 | ("cudart", "libcudart.so"), # NVIDIA CUDA Runtime |
| 44 | ("amdhip64", "libamdhip64.so"), # AMD ROCm HIP Runtime |
| 45 | ]: |
| 46 | try: |
| 47 | path = ctypes.util.find_library(name) |
| 48 | if path: |
| 49 | _copy_lib = ctypes.CDLL(path) |
| 50 | else: |
| 51 | _copy_lib = ctypes.CDLL(fallback) |
| 52 | break # Successfully loaded, stop trying |
| 53 | except OSError: |
| 54 | continue # Current library not available, try next |
| 55 | else: |
| 56 | # All GPU libraries failed to load, fall back to CPU |
| 57 | _copy_lib = None |
| 58 | return _copy_lib |
| 59 | |
| 60 | |
| 61 | def _tensor_from_ptr( |
no outgoing calls
no test coverage detected