Skip to main content

How to Choose the Device a Hugging Face Pipeline Runs On

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A Hugging Face pipeline can run its model on the main processor (CPU) or on a graphics processor (GPU), which is usually much faster. Transformers 5 picks the GPU for you when there is one, and the device argument overrides it. This guide shows what the pipeline picks on an Apple M1 Pro, how to force the CPU, and which setting fails on a Mac.

Syntax​

Syntax
pipeline("<task>", model="<model>", revision="<commit>", device=<device>)
deviceWhere the model runs
Not set"the first available accelerator (CUDA, MPS, XPU, ...)", and the CPU only when there's none
"cpu" or -1The CPU
0, 1, …That number of accelerator, of whichever kind the computer has
"cuda", "mps", "cuda:1"Exactly that device. It must exist, or loading fails

The quotes come from the pipeline's documentation in Transformers 5.24.1: when device is left unset, "the pipeline is placed on the first available accelerator (CUDA, MPS, XPU, ...) and falls back to CPU only when none is available". pipeline.device tells you where it ended up.

The Example​

The same small sentiment model loaded twice, once with no device and once with device="cpu", printing where each one runs.

Prerequisites​

  • uv. This guide uses uv 0.13.10. The project uses Python 3.14, which uv downloads if needed.

Step 1: Create the Project​

Terminal
uv init --python 3.14 device-demo
cd device-demo
uv add transformers torch

Step 2: Write the Script​

devices.py
from transformers import pipeline

MODEL = "distilbert/distilbert-base-uncased-finetuned-sst-2-english"
REVISION = "714eb0fa89d2f80546fda750413ed43d93601a13"

automatic = pipeline("sentiment-analysis", model=MODEL, revision=REVISION)
print("No device given ->", automatic.device)

on_cpu = pipeline("sentiment-analysis", model=MODEL, revision=REVISION, device="cpu")
print('device="cpu" ->', on_cpu.device)
print(on_cpu("I really like this phone."))

The devices.py script in a macOS-style code window: a sentiment pipeline built without a device and its device printed, then one built with device="cpu", its device printed, and a classification of I really like this phone.

Step 3: Run It​

Terminal
uv run python devices.py
Output
W1201 05:03:02.138000 51198 .venv/lib/python3.14/site-packages/torch/distributed/elastic/multiprocessing/redirects.py:35] NOTE: Redirects are currently not supported in MacOs.
Loading weights: 100%|██████████████████████████████████████████| 104/104 [00:00<00:00, 5367.12it/s]
No device given -> mps:0
Loading weights: 100%|██████████████████████████████████████████| 104/104 [00:00<00:00, 7920.25it/s]
device="cpu" -> cpu
[{'label': 'POSITIVE', 'score': 0.9990968704223633}]

The terminal after uv run python devices.py: No device given -> mps:0, then device="cpu" -> cpu, then a POSITIVE label with a score of 0.999.

Without a device, the pipeline went to mps:0, the Mac's GPU. With device="cpu", it stayed on the processor, and still classified the sentence. On a PC with a working NVIDIA GPU, the first line would say cuda:0. The command works unchanged in PowerShell.

device=0 asks for "accelerator number 0" without naming its kind. The pipeline's code checks CUDA first and Apple's MPS last, so on this Mac, device=0 gave mps:0 too.

Step 4: See What device="cuda" Does on a Mac​

Code written on a PC with an NVIDIA GPU often says device="cuda":

devices_cuda.py
from transformers import pipeline

classifier = pipeline(
"sentiment-analysis",
model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
revision="714eb0fa89d2f80546fda750413ed43d93601a13",
device="cuda",
)
print(classifier.device)
Terminal
uv run python devices_cuda.py
Output (last lines)
File "/Users/you/source/device-demo/.venv/lib/python3.14/site-packages/torch/cuda/__init__.py", line 609, in _lazy_init
raise AssertionError("Torch not compiled with CUDA enabled")
AssertionError: Torch not compiled with CUDA enabled

The terminal after uv run python devices_cuda.py: the end of a traceback from torch/cuda/init.py, AssertionError: Torch not compiled with CUDA enabled.

The Mac build of PyTorch has no CUDA, so naming "cuda" fails while the model is being moved there. Leave device unset, or use 0, and the same code runs on whatever the computer has.

Common Mistakes​

MistakeWhat happensDo this instead
Hard-coding device="cuda"AssertionError: Torch not compiled with CUDA enabled on a Mac or a computer without CUDALeave device unset
Expecting a GPU on Windows after uv add torchThe PyTorch build from PyPI has no CUDA on Windows, so the pipeline uses the CPUInstall a CUDA build of PyTorch
Passing device together with device_mapTransformers warns: "Both device and device_map are specified. device will override device_map. You will most likely encounter unexpected behavior."Use one of them

FAQ​

What does the :0 in mps:0 mean?​

It's the device's number. A computer can have several GPUs, numbered from 0, so mps:0 is the first (and on a Mac, only) Apple GPU.

How do I check whether PyTorch can see a GPU at all?​

torch.backends.mps.is_available() on a Mac and torch.cuda.is_available() for NVIDIA GPUs return True or False.

Conclusion​

Left to itself, the pipeline ran on mps:0, the Mac's GPU. device="cpu" kept it on the processor, device=0 meant the first accelerator of any kind, and device="cuda" failed on a Mac. Leave device unset unless you need a specific one.

Additional Resources​