Lightning-AI/pytorch-lightning · error · ValueError
You requested to find {num_devices} devices but there are no
Error message
You requested to find {num_devices} devices but there are no visible CUDA devices on this machine. What it means
_determine_batch_limits validates percentage-like Trainer arguments (limit_train_batches, limit_val_batches, limit_test_batches, limit_predict_batches, val_check_interval, overfit_batches). A value must be either a fraction in [0.0, 1.0] or an integral count (> 1 and a whole number). Anything else (e.g. 1.5, 2.5, -1) raises this MisconfigurationException.
Source
Thrown at src/lightning/fabric/accelerators/cuda.py:106
tests for each GPU on the system until the target number of usable devices is found.
A subset of GPUs on the system might be used by other processes, and if the GPU is configured to operate in
'exclusive' mode (configurable by the admin), then only one process is allowed to occupy it.
Args:
num_devices: The number of devices you want to request. By default, this function will return as many as there
are usable CUDA GPU devices available.
Warning:
If multiple processes call this function at the same time, there can be race conditions in the case where
both processes determine that the device is unoccupied, leading into one of them crashing later on.
"""
if num_devices == 0:
return []
visible_devices = _get_all_visible_cuda_devices()
if not visible_devices:
raise ValueError(
f"You requested to find {num_devices} devices but there are no visible CUDA devices on this machine."
)
if num_devices > len(visible_devices):
raise ValueError(
f"You requested to find {num_devices} devices but this machine only has {len(visible_devices)} GPUs."
)
available_devices = []
unavailable_devices = []
for gpu_idx in visible_devices:
try:
torch.tensor(0, device=torch.device("cuda", gpu_idx))
except RuntimeError:
unavailable_devices.append(gpu_idx)
continue
available_devices.append(gpu_idx)View on GitHub (pinned to 9fed5c27d2)
Solutions
- Use a fraction 0.0-1.0 for percentages (0.8, not 80)
- Use a whole integer for exact batch counts (limit_train_batches=100)
- Round fractional counts: int(count) or math.floor before passing
Example fix
# before trainer = Trainer(limit_train_batches=2.5) # after trainer = Trainer(limit_train_batches=0.5) # half the batches # or trainer = Trainer(limit_train_batches=2) # exactly 2 batches
Defensive patterns
Strategy: validation
Validate before calling
def validate_batches(v: float, name: str) -> Union[int, float]:
if 0 <= v <= 1:
return v
if v > 1 and float(v).is_integer():
return int(v)
raise ValueError(f"{name}={v!r} must be in [0.0, 1.0] or an int") Type guard
def is_valid_batch_limit(v) -> bool:
return isinstance(v, (int, float)) and (0 <= v <= 1 or (v > 1 and float(v).is_integer())) Prevention
- Remember: floats <=1.0 are fractions, ints are counts
- Convert percentages: divide by 100 (80% -> 0.8)
- Round derived counts with int() before passing
When it happens
Trigger: Trainer(limit_train_batches=2.5); Trainer(overfit_batches=-1); Trainer(val_check_interval=1.2) treated through this path; any float > 1 that is not a whole number, or a negative number.
Common situations: Confusing fraction vs count semantics (thinking 2.0 means '2x the dataset'); config files with floats from sweeps; converting '80%' to 80 instead of 0.8; arithmetic producing fractional counts like len(loader)/2 when odd.
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
- Device should be CUDA, got {device} instead.
- `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
- You requested to find {num_devices} devices but this machine
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/c2725ddd9854503f.
Report an issue: GitHub.