Return the torch module if available, else raise a friendly error. This prevents crashing on import if CUDA libs are missing.
()
| 20 | |
| 21 | |
| 22 | def get_torch(): |
| 23 | """ |
| 24 | Return the torch module if available, else raise a friendly error. |
| 25 | |
| 26 | This prevents crashing on import if CUDA libs are missing. |
| 27 | """ |
| 28 | if TORCH_AVAILABLE: |
| 29 | return _torch |
| 30 | else: |
| 31 | error_message = ( |
| 32 | "Torch is not available or failed to import.\n" |
| 33 | "Original error:\n" |
| 34 | f"{_torch_import_error}\n\n" |
| 35 | "If you are on a CPU-only system, make sure you install the CPU-only torch wheel:\n" |
| 36 | " pip install torch torchvision -f https://download.pytorch.org/whl/cpu\n" |
| 37 | "Or check your CUDA installation if using GPU torch.\n" |
| 38 | ) |
| 39 | raise ImportError(error_message) from _torch_import_error |
no outgoing calls