File size: 994 Bytes
2559788
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""Torch device probe used by every training script."""

from __future__ import annotations

from typing import Any


def probe_torch() -> dict[str, Any]:
    info: dict[str, Any] = {
        "torch_imported": False,
        "version": None,
        "cuda_compiled": None,
        "cuda_available": False,
        "device": "cpu",
        "gpu_name": None,
        "error": None,
    }
    try:
        import torch
    except Exception as exc:  # pragma: no cover - environment dependent
        info["error"] = f"{type(exc).__name__}: {exc}"
        return info
    info["torch_imported"] = True
    info["version"] = torch.__version__
    info["cuda_compiled"] = torch.version.cuda
    info["cuda_available"] = bool(torch.cuda.is_available())
    if info["cuda_available"]:
        info["device"] = "cuda"
        info["gpu_name"] = torch.cuda.get_device_name(0)
    return info


def torch_device():
    import torch

    return torch.device("cuda" if torch.cuda.is_available() else "cpu")