skip to content

When a Transformers v5 pipeline() is built without device, device_map or dtype, where does the model run and in what precision?

level: middleimportance: should knowfreq 20%

answer

  1. first accelerator wins
  2. opt out to CPU explicitly
  3. Accelerate places layers across devices
  4. auto reads the checkpoint's config
  5. torch_dtype is the old spelling

basics

~20 s

In Transformers v5 a pipeline() with no device lands on the first available accelerator (CUDA, MPS, XPU), else CPU, and dtype='auto' loads the precision recorded in the checkpoint. device_map='auto' instead lets Accelerate spread layers across devices.

solid answer

~40 s

In Transformers v5 (checked against 5.18), leaving `device` unset places the pipeline on the first available accelerator (CUDA, Apple `mps`, XPU and others) and falls back to CPU only when there is none; `device="cpu"` or `-1` forces CPU, and `device=0` or `"cuda:1"` picks one. `device_map="auto"` is different: it is forwarded to `from_pretrained` and lets Accelerate split the layers across GPUs and then CPU or disk, which is how a model larger than one GPU loads; the pipeline then feeds inputs to the map's first device, and you should not also pass `device`. `dtype` defaults to `"auto"`: the `dtype` (or legacy `torch_dtype`) entry in `config.json`, else the first floating-point weight's dtype, so an fp32-saved checkpoint loads in fp32. Pass `dtype=torch.bfloat16` to choose; `torch_dtype` is the deprecated spelling.

code

python · 18 lines
python
import torch
from transformers import pipeline

# Fits one device: say which one and which precision
clf = pipeline(
    "text-classification",
    model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
    device="cpu",
    dtype=torch.float32,
)

# Too large for one GPU: let Accelerate place the layers, and pass no device
gen = pipeline(
    "text-generation",
    model="HuggingFaceTB/SmolLM3-3B",
    device_map="auto",
    dtype=torch.bfloat16,
)

go deeper

for a junior

Recall that device chooses where a pipeline runs, that device='cpu' forces CPU, and that dtype sets the precision of the loaded weights.

for a middle

Explain the defaults: an unset device picks the first accelerator, dtype='auto' follows config.json or the first float weight, and device_map='auto' hands layer placement to Accelerate.

for a senior

Show production judgment: make device and dtype explicit so laptops, CI and GPU nodes behave alike, never combine device with device_map, and watch the log for the float32 fallback.

for a principal

Weigh single-device loads against Accelerate dispatch for a fleet: offloading trades throughput for fitting, so decide when a smaller dtype or a bigger device beats a model split across CPU and disk.

## Two questions every load answers Whenever Hugging Face Transformers builds a pipeline it must decide **where** the weights live (which device) and **in what numeric precision** (which `dtype`). Both have defaults in the `pipeline()` signature of Transformers v5, checked here against 5.18: `device=None`, `device_map=None`, `dtype="auto"`. Knowing what those defaults resolve to is the difference between a job that uses the GPU you paid for and one that quietly runs at twice the memory it needs. ## `device`: one device for the whole model When `device` is left unset and the model was not dispatched by Accelerate, the pipeline treats it as ordinal `0` and maps that onto the first accelerator backend it finds available (CUDA, XPU, Apple MPS and several vendor backends), falling back to CPU only when none is present. The model is moved there for you. - `device="cpu"` or `device=-1` forces CPU, useful on a CI runner that has a GPU the job must not touch. - `device=1` or `device="cuda:1"` selects a specific accelerator; `device="mps"` selects Apple silicon. - A `torch.device` object is accepted as is. The whole model sits on that one device, so `device` only works when the weights fit there. ## `device_map`: let Accelerate place the layers `device_map` is not a pipeline feature: `pipeline()` forwards it to `from_pretrained` through `model_kwargs`. With the Accelerate library installed, `device_map="auto"` computes a placement that fills the fastest devices (GPUs) first and puts remaining layers in CPU RAM or on disk. That is how you load a checkpoint that does not fit on one GPU, at the cost of slower forward passes whenever layers live off the GPU. How much memory a model needs is a sizing question; the pipeline's part is placement. Rules that bite: 1. **Do not combine `device` and `device_map`.** The documentation says they conflict; `pipeline()` logs that `device` will override `device_map` with unexpected results, and a model Accelerate has dispatched (one carrying an `hf_device_map`) refuses a `device` argument with a `ValueError`. 2. **Inputs go to the first mapped device.** With an `hf_device_map` and no `device`, the pipeline sends input tensors to the first device in that map. 3. **The pipeline never moves a dispatched model** with `.to()`; doing so would undo the map. ## `dtype`: precision of the loaded weights `dtype="auto"` reads the `dtype` entry, or the legacy `torch_dtype` entry, in the checkpoint's `config.json`. If neither is present it uses the dtype of the first floating-point weight in the checkpoint. In other words, **you get whatever precision the author saved**, which is not necessarily the precision the model was trained in. | You pass | Weights load as | |---|---| | nothing (`"auto"`) | config.json's dtype, else the first float weight's dtype | | `torch.bfloat16` or `"bfloat16"` | bfloat16, ignoring the config | | `torch.float16` or `"float16"` | float16 | | `torch.float32` or `"float32"` | float32 | Two further details of the pipeline's loader: - If loading in the chosen dtype raises on the target device (the source's own example is bf16 on a consumer GPU), the pipeline retries the load in `torch.float32` and logs that it is falling back to float32. The job keeps running, now at fp32's memory cost. - Passing an explicit `dtype` argument and also a `dtype` inside `model_kwargs` raises a `ValueError`; pick one place. ## Spellings that changed in v5 - `torch_dtype` is the deprecated spelling of `dtype`. It still works, the pipeline warns that it is deprecated, and when both are given an explicit `dtype` wins. - The `load_in_4bit` and `load_in_8bit` loading arguments were removed in v5. Quantized loading goes through `quantization_config` (for example `BitsAndBytesConfig(load_in_4bit=True)`), which a pipeline receives inside `model_kwargs`. What quantization costs in quality is a separate subject. ## Reading back what you got After the first load, check rather than assume. `pipe.device` is the device the pipeline sends inputs to; `pipe.model.dtype` is the precision the weights actually loaded in, which reveals a silent float32 fallback; and for a dispatched model, `pipe.model.hf_device_map` shows which layers landed on which GPU, in CPU RAM or on disk. Logging these three values at job start turns a mysterious slowdown or memory spike into a one-line diagnosis. ## A batch-job checklist - Choose the device explicitly (`device=0` or `device="cpu"`) so one script behaves the same on a laptop, a CI runner and a GPU node. - Use `device_map="auto"` only when the model does not fit one device, and never together with `device`. - Set `dtype` explicitly when memory matters, and check the log for the float32 fallback warning after the first load.

  • Why might a pipeline loaded with device_map='auto' run far slower than expected?
    When the weights do not fit on the GPUs, Accelerate places the overflow layers in CPU RAM or on disk, and every forward pass then pays for moving data across that boundary. The placement is visible in the model's `hf_device_map`; entries of `cpu` or `disk` explain the slowdown. The remedies are a smaller `dtype`, a quantized load or a bigger device; the memory arithmetic belongs to GPU sizing.
  • A script written for Transformers v4 passes torch_dtype=torch.float16 to pipeline(). What happens under v5?
    It still runs: v5 maps `torch_dtype` onto `dtype` and logs that `torch_dtype` is deprecated. Rename it to `dtype=torch.float16`. If both are passed, the explicit `dtype` wins. Omitting the argument altogether means `dtype="auto"`, which follows whatever precision the checkpoint records.

  • A pipeline always runs on CPU unless you pass device=0.
  • Passing device_map='auto' and device=0 together gives the best of both.
  • dtype='auto' picks the fastest precision the GPU supports.
  • torch_dtype is the current argument name in Transformers v5.
  • Asking for bfloat16 guarantees bfloat16 weights even when loading in it fails.

Assumes Transformers v5 (5.18): dtype defaults to 'auto' with torch_dtype as a deprecated alias, and an unset device picks the first available accelerator.