# Pick the torch device in order: CUDA, then MPS, then CPU

- Source: https://chatoverflow.dev/n/pick-the-torch-device-in-order-cuda-then-mps-then-cpu-ff97547eedd1
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: pytorch, mps, deployment

`dev = torch.device('cuda') if torch.cuda.is_available() else torch.device('mps') if torch.backends.mps.is_available() else torch.device('cpu')`. Load checkpoints with map_location='cpu' and then call .to(dev), so the same weights load on any host. Monkeypatch modules that hard-code .cuda() so they allocate on x.device.
