Lightning-AI/pytorch-lightning · error · ValueError

`Fabric(devices={self._devices_flag!r})` value is not a vali

Error message

`Fabric(devices={self._devices_flag!r})` value is not a valid input using {accelerator_name} accelerator.

What it means

Fabric rejects empty/falsy device specifications: `devices=[]`, `devices=0`, or `devices="0"` are not valid for any accelerator. The connector checks this right after storing the flags and names the resolved accelerator in the message.

Source

Thrown at src/lightning/fabric/connector.py:311

                            f" but accelerator set to {self._accelerator_flag}, please choose one device type"
                        )
                    self._accelerator_flag = "cuda"
                self._parallel_devices = self._strategy_flag.parallel_devices

    def _check_device_config_and_set_final_flags(self, devices: Union[list[int], str, int], num_nodes: int) -> None:
        if not isinstance(num_nodes, int) or num_nodes < 1:
            raise ValueError(f"`num_nodes` must be a positive integer, but got {num_nodes}.")

        self._num_nodes_flag = num_nodes
        self._devices_flag = devices

        if self._devices_flag in ([], 0, "0"):
            accelerator_name = (
                self._accelerator_flag.__class__.__qualname__
                if isinstance(self._accelerator_flag, Accelerator)
                else self._accelerator_flag
            )
            raise ValueError(
                f"`Fabric(devices={self._devices_flag!r})` value is not a valid input"
                f" using {accelerator_name} accelerator."
            )

    @staticmethod
    def _choose_auto_accelerator() -> str:
        """Choose the accelerator type (str) based on availability when ``accelerator='auto'``."""
        if XLAAccelerator.is_available():
            return "tpu"
        if MPSAccelerator.is_available():
            return "mps"
        if CUDAAccelerator.is_available():
            return "cuda"
        return "cpu"

    @staticmethod
    def _choose_gpu_accelerator_backend() -> str:
        if MPSAccelerator.is_available():

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass a concrete value: devices=1 (or devices="auto" to let Fabric pick)
  2. If GPU count is 0 unexpectedly, check CUDA_VISIBLE_DEVICES and torch.cuda.is_available() before choosing devices
  3. Fall back to CPU explicitly: accelerator="cpu", devices=1 when no GPUs are available

Example fix

# before
fabric = Fabric(accelerator="gpu", devices=torch.cuda.device_count())  # 0 on CPU box

# after
ndev = torch.cuda.device_count()
fabric = Fabric(accelerator="gpu" if ndev else "cpu", devices=ndev or 1)
Defensive patterns

Strategy: validation

Validate before calling

if devices in ([], 0, "0", None):
    ndev = torch.cuda.device_count()
    accelerator = "gpu" if ndev else "cpu"
    devices = ndev or 1
fabric = Fabric(accelerator=accelerator, devices=devices)

Type guard

def is_valid_devices(d) -> bool:
    return d not in ([], 0, "0") and d is not None

Prevention

When it happens

Trigger: Fabric(devices=0), Fabric(devices=[]), or Fabric(devices="0") with any accelerator (e.g. accelerator="gpu", devices=0).

Common situations: devices computed from GPU count on a machine with no visible GPUs (torch.cuda.device_count() == 0) yielding 0; CUDA_VISIBLE_DEVICES="" in slurm/docker; config templates defaulting devices to 0 or an empty list.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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