Lightning-AI/pytorch-lightning · error · ValueError
Device should be CUDA, got {device} instead.
Error message
Device should be CUDA, got {device} instead. What it means
Trainer._init_debugging_flags validates fast_dev_run: when passed as an int it must be >= 0. Negative integers such as fast_dev_run=-1 are rejected immediately at Trainer construction because a negative run count is meaningless.
Source
Thrown at src/lightning/fabric/accelerators/cuda.py:36
from typing_extensions import override
from lightning.fabric.accelerators.accelerator import Accelerator
from lightning.fabric.accelerators.registry import _AcceleratorRegistry
from lightning.fabric.utilities.rank_zero import rank_zero_info
class CUDAAccelerator(Accelerator):
"""Accelerator for NVIDIA CUDA devices."""
@override
def setup_device(self, device: torch.device) -> None:
"""
Raises:
ValueError:
If the selected device is not of type CUDA.
"""
if device.type != "cuda":
raise ValueError(f"Device should be CUDA, got {device} instead.")
_check_cuda_matmul_precision(device)
torch.cuda.set_device(device)
@override
def teardown(self) -> None:
_clear_cuda_memory()
@staticmethod
@override
def parse_devices(devices: Union[int, str, list[int]]) -> Optional[list[int]]:
"""Accelerator device parsing logic."""
from lightning.fabric.utilities.device_parser import _parse_gpu_ids
return _parse_gpu_ids(devices, include_cuda=True)
@staticmethod
@override
def get_parallel_devices(devices: list[int]) -> list[torch.device]:View on GitHub (pinned to 9fed5c27d2)
Solutions
- Set fast_dev_run to a valid value: True, False, or a non-negative int like 1 or 5
- If computing it dynamically, clamp: fast_dev_run = max(0, int(value))
- Use fast_dev_run=0 or False to disable it instead of -1
Example fix
# before trainer = Trainer(fast_dev_run=-1) # after trainer = Trainer(fast_dev_run=False) # or 1, 5, True, 0
Defensive patterns
Strategy: validation
Validate before calling
def sanitize_fast_dev_run(v):
if isinstance(v, bool):
return v
if isinstance(v, int):
if v < 0:
raise ValueError("fast_dev_run must be >= 0")
return v
return bool(v) Type guard
def is_valid_fast_dev_run(v) -> bool:
return v is None or isinstance(v, bool) or (isinstance(v, int) and v >= 0) Prevention
- Treat fast_dev_run as a tri-state: True/False or a small positive int
- Clamp programmatically derived values with max(0, int(x))
- Validate sweep configs before constructing the Trainer
When it happens
Trigger: Constructing Trainer(fast_dev_run=-1) or any negative int; computing fast_dev_run programmatically (e.g. from a config or CLI arg) where a subtraction/default yields a negative value.
Common situations: Sweep/search configs generating fast_dev_run from expressions; typos; porting scripts where fast_dev_run was derived from a dataset size that can be 0 or negative; passing a float like -1.0 is not caught here (only int is checked) but negatives should be avoided regardless.
Understand the failure class
Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.
Related errors
- You requested to find {num_devices} devices but there are no
- `max_epochs` must be a non-negative integer or -1. You passe
- `max_steps` must be a non-negative integer or -1 (infinite s
- No `{step_name}()` method defined to run `Trainer.{trainer_m
- f"`Trainer(barebones=True, fast_dev_run={fast_dev_run!r})` w
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/c05d80e01a22c038.
Report an issue: GitHub.