Download utils/device.py from yusiwen/dl-from-scratch: direct link, hf CLI and curl.
- Browser
- Download file 294 Bytes
-
https://huggingface.co/yusiwen/dl-from-scratch/resolve/main/utils/device.py
- Command line
-
hf download hf://yusiwen/dl-from-scratch/utils/device.py
-
curl -L -o device.py https://huggingface.co/yusiwen/dl-from-scratch/resolve/main/utils/device.py
294 Bytes
| import torch | |
| def get_device(): | |
| """Auto-detect best available device: CUDA → MPS → CPU.""" | |
| if torch.cuda.is_available(): | |
| return torch.device("cuda") | |
| elif torch.backends.mps.is_available(): | |
| return torch.device("mps") | |
| else: | |
| return torch.device("cpu") | |