huggingface/transformers · error · ValueError

You can't pass device_map as a negative int. If you want to

Error message

You can't pass device_map as a negative int. If you want to put the model on the cpu, pass device_map = 'cpu' 

What it means

Error "You can't pass device_map as a negative int. If you want to put the model on the cpu, pass device_map = 'cpu' " thrown in huggingface/transformers.

Source

Thrown at src/transformers/integrations/accelerate.py:125

    # 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`"
            )
    return device_map


def compute_module_sizes(
    model: "PreTrainedModel",

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass device_map='cpu' to place the model on CPU.
  2. Use a non-negative int or a valid device string.

When it happens

Trigger: Raised in device_map validation when device_map is a negative integer.

Common situations: Passing device_map=-1 intending CPU; CPU must be requested with device_map='cpu'.


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/9bea08347ecaad47. Report an issue: GitHub.