huggingface/transformers · error · ValueError
When passing device_map as a string, the value needs to be a
Error message
When passing device_map as a string, the value needs to be a device name (e.g. cpu, cuda:0) or 'auto', 'balanced', 'balanced_low_0', 'sequential' but found {device_map}. What it means
Error "When passing device_map as a string, the value needs to be a device name (e.g. cpu, cuda:0) or 'auto', 'balanced', 'balanced_low_0', 'sequential' but found {device_map}." thrown in huggingface/transformers.
Source
Thrown at src/transformers/integrations/accelerate.py:119
raise RuntimeError(
"You are using `from_pretrained` with a meta device context manager or `torch.set_default_device('meta')`.\n"
"This is an anti-pattern as `from_pretrained` wants to load existing weights.\nIf you want to initialize an "
"empty model on the meta device, use the context manager or global device with `from_config`, or `ModelClass(config)`"
)
device_map = device_in_context
# change device_map into a map if we passed an int, a str or a torch.device
if isinstance(device_map, torch.device):
device_map = {"": device_map}
elif isinstance(device_map, str) and device_map not in ["auto", "balanced", "balanced_low_0", "sequential"]:
try:
if device_map == "cuda":
# setting to the local rank
local_rank = int(os.environ.get("LOCAL_RANK", 0))
device_map = f"cuda:{local_rank}"
device_map = {"": torch.device(device_map)}
except RuntimeError:
raise ValueError(
"When passing device_map as a string, the value needs to be a device name (e.g. cpu, cuda:0) or "
f"'auto', 'balanced', 'balanced_low_0', 'sequential' but found {device_map}."
)
elif isinstance(device_map, int):
if device_map < 0:
raise ValueError(
"You can't pass device_map as a negative int. If you want to put the model on the cpu, pass device_map = 'cpu' "
)
else:
device_map = {"": device_map}
if device_map is not None:
if is_deepspeed_zero3_enabled():
raise ValueError("DeepSpeed Zero-3 is not compatible with passing a `device_map`.")
if not is_accelerate_available():
raise ValueError(
"Using a `device_map`, `tp_plan`, `torch.device` context manager or setting `torch.set_default_device(device)` "
"requires `accelerate`. You can install it with `pip install accelerate`"View on GitHub (pinned to a597f97485)
Solutions
- Pass device_map as 'auto', 'balanced', 'balanced_low_0', 'sequential', or a device name like 'cpu'/'cuda:0'.
- Pass a dict mapping module names to devices for fine-grained placement.
When it happens
Trigger: Raised in device_map validation when a string device_map is not a device name or a known strategy ('auto', 'balanced', etc.).
Common situations: Typo in device_map string, e.g. device_map='gpu' or 'balance', instead of a valid strategy or device name.
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/577908e749a6e0e0.
Report an issue: GitHub.