Spaces:
Sleeping
Sleeping
Download src/transformer_analysis/device_utils.py from angerami/transformer-weights: direct link, hf CLI and curl.
- Browser
- Download file 599 Bytes
-
https://huggingface.co/spaces/angerami/transformer-weights/resolve/main/src/transformer_analysis/device_utils.py
- Command line
-
hf download hf://spaces/angerami/transformer-weights/src/transformer_analysis/device_utils.py
-
curl -L -o device_utils.py https://huggingface.co/spaces/angerami/transformer-weights/resolve/main/src/transformer_analysis/device_utils.py
599 Bytes
| import torch | |
| def get_device(device=None): | |
| """ | |
| Pick the best available device. | |
| Priority: explicit override -> CUDA (Colab / lab box) -> MPS (Apple Silicon) | |
| -> CPU. Forward-only eval on MPS is solid in torch >= 2. | |
| Args: | |
| device: Manual override ('cuda', 'mps', 'cpu', or None for auto) | |
| Returns: | |
| torch.device | |
| """ | |
| if device is not None: | |
| return torch.device(device) | |
| if torch.cuda.is_available(): | |
| return torch.device("cuda") | |
| if torch.backends.mps.is_available(): | |
| return torch.device("mps") | |
| return torch.device("cpu") | |