Lightning-AI/pytorch-lightning · error · MisconfigurationException

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

Error message

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

What it means

devices must be a meaningful value: an empty list, integer 0, or string '0' is rejected for the chosen accelerator. The connector requires at least one device to be requested.

Source

Thrown at src/lightning/pytorch/trainer/connectors/accelerator_connector.py:325

                            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 MisconfigurationException(
                f"`Trainer(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."""
        return _select_auto_accelerator()

    @staticmethod
    def _choose_gpu_accelerator_backend() -> str:
        if MPSAccelerator.is_available():
            return "mps"
        if CUDAAccelerator.is_available():
            return "cuda"
        raise MisconfigurationException("No supported gpu backend found!")

    def _set_parallel_devices_and_init_accelerator(self) -> None:

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Pass a valid count/index/list, e.g. devices=1 or devices='auto'
  2. If the machine truly has no devices of that accelerator, switch accelerator='cpu' with devices=1 or fix the environment (GPU visibility)

Example fix

# before
trainer = Trainer(accelerator="gpu", devices=0)
# after
trainer = Trainer(accelerator="gpu", devices=1)
Defensive patterns

Strategy: validation

Validate before calling

if devices in ([], 0, "0"):
    devices = "auto"  # or raise a clear early config error
trainer = Trainer(devices=devices)

Type guard

def invalid_devices(d) -> bool:
    return d in ([], 0, "0")

Prevention

When it happens

Trigger: Trainer(devices=0), Trainer(devices=[]), Trainer(devices='0') with any accelerator setting (e.g. accelerator='gpu').

Common situations: devices derived from CUDA_VISIBLE_DEVICES parsing that yields 0 GPUs; dynamic device counts on machines without the expected hardware; configs intended to disable training.

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/e0032425c6c5915d. Report an issue: GitHub.