Lightning-AI/pytorch-lightning · error · MisconfigurationException

GPU parallel_devices set through {self._strategy_flag.__clas

Error message

GPU parallel_devices set through {self._strategy_flag.__class__.__name__} class, but accelerator set to {self._accelerator_flag}, please choose one device type

What it means

The strategy instance lists CUDA devices in parallel_devices but the accelerator flag names a non-GPU accelerator (not 'auto'/'cuda'/'gpu'). The connector detects the contradiction between devices and accelerator.

Source

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

                self.checkpoint_io = self._strategy_flag._checkpoint_io
            if getattr(self._strategy_flag, "cluster_environment", None):
                if self._cluster_environment_flag:
                    raise MisconfigurationException(
                        "cluster_environment set through both strategy class and plugins, choose one"
                    )
                self._cluster_environment_flag = getattr(self._strategy_flag, "cluster_environment")

            if hasattr(self._strategy_flag, "parallel_devices") and self._strategy_flag.parallel_devices:
                if self._strategy_flag.parallel_devices[0].type == "cpu":
                    if self._accelerator_flag and self._accelerator_flag not in ("auto", "cpu"):
                        raise MisconfigurationException(
                            f"CPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,"
                            f" but accelerator set to {self._accelerator_flag}, please choose one device type"
                        )
                    self._accelerator_flag = "cpu"
                if self._strategy_flag.parallel_devices[0].type == "cuda":
                    if self._accelerator_flag and self._accelerator_flag not in ("auto", "cuda", "gpu"):
                        raise MisconfigurationException(
                            f"GPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,"
                            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

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Remove the explicit accelerator= flag (auto-detection will resolve to cuda)
  2. Or align accelerator with the devices: 'cuda'/'gpu' for CUDA parallel_devices

Example fix

# before
strategy = DDPStrategy(parallel_devices=[torch.device("cuda", 0)])
trainer = Trainer(strategy=strategy, accelerator="cpu")
# after
strategy = DDPStrategy(parallel_devices=[torch.device("cuda", 0)])
trainer = Trainer(strategy=strategy)
Defensive patterns

Strategy: validation

Validate before calling

if hasattr(strategy, "parallel_devices") and strategy.parallel_devices:
    dev_type = strategy.parallel_devices[0].type
    if dev_type == "cuda" and accelerator not in (None, "auto", "cuda", "gpu"):
        accelerator = "auto"

Type guard

def gpu_devices_accelerator_mismatch(strategy, accelerator) -> bool:
    devs = getattr(strategy, "parallel_devices", None)
    return bool(devs) and devs[0].type == "cuda" and accelerator not in (None, "auto", "cuda", "gpu")

Prevention

When it happens

Trigger: DDPStrategy(parallel_devices=[torch.device('cuda', i) for i in range(2)]) with Trainer(strategy=..., accelerator='cpu') or 'mps'/'tpu'.

Common situations: Defaulting accelerator='cpu' in configs while a strategy factory injects CUDA devices; running GPU-oriented configs on mismatched accelerator settings.

Related errors


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