Lightning-AI/pytorch-lightning · error · TypeError

Device IDs (GPU/TPU) must be an int, a string, a sequence of

Error message

Device IDs (GPU/TPU) must be an int, a string, a sequence of ints, but you passed {device_ids!r}.

What it means

If devices is neither None, a sequence/tuple, an int, nor a str, the parser rejects it with TypeError showing the repr of the value. Typical offenders are floats (devices=1.0), dicts, or arbitrary objects passed through config systems.

Source

Thrown at src/lightning/fabric/utilities/device_parser.py:206

    Args:
        device_ids: gpus/tpu_cores parameter as passed to the Trainer

    Raises:
        TypeError:
            If ``device_ids`` of GPU/TPUs aren't ``int``, ``str`` or sequence of ``int```

    """
    msg = "Device IDs (GPU/TPU) must be an int, a string, a sequence of ints, but you passed"
    if device_ids is None:
        raise TypeError(f"{msg} None")
    if isinstance(device_ids, (MutableSequence, tuple)):
        for id_ in device_ids:
            id_type = type(id_)  # because `isinstance(False, int)` -> True
            if id_type is not int:
                raise TypeError(f"{msg} a sequence of {type(id_).__name__}.")
    elif type(device_ids) not in (int, str):
        raise TypeError(f"{msg} {device_ids!r}.")


def _select_auto_accelerator() -> str:
    """Choose the accelerator type (str) based on availability."""
    from lightning.fabric.accelerators.cuda import CUDAAccelerator
    from lightning.fabric.accelerators.mps import MPSAccelerator
    from lightning.fabric.accelerators.xla import XLAAccelerator

    if XLAAccelerator.is_available():
        return "tpu"
    if MPSAccelerator.is_available():
        return "mps"
    if CUDAAccelerator.is_available():
        return "cuda"
    return "cpu"

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Coerce to a supported type: int(devices) for counts, str for comma-separated ids, or list of ints
  2. Fix your config schema so devices is int|str|list[int]
  3. Use devices='auto' when unsure

Example fix

# before
Fabric(accelerator="gpu", devices=float(cfg.n_gpus))  # 2.0

# after
Fabric(accelerator="gpu", devices=int(cfg.n_gpus))
Defensive patterns

Strategy: type-guard

Validate before calling

if isinstance(devices, float):
    devices = int(devices)
assert isinstance(devices, (int, str, list, tuple)), f"unsupported devices value: {devices!r}"

Type guard

def is_supported_devices(d) -> bool:
    if isinstance(d, bool): return False
    if type(d) is int or type(d) is str: return True
    return isinstance(d, (list, tuple)) and all(type(x) is int for x in d)

Prevention

When it happens

Trigger: devices=1.0 (float from a config cast); devices={'gpu': 2}; devices=torch.device('cuda'); any custom object reaching the devices parameter of Fabric/Trainer.

Common situations: YAML/Hydra configs that coerce counts to floats (devices: 2.0); wrapping devices in a dict or dataclass field and passing it unmodified; passing a torch.device object where an id/count is expected.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/4f7a52cd454bd7b0. Report an issue: GitHub.