Lightning-AI/pytorch-lightning · error · ValueError
You requested to find {num_devices} devices but this machine
Error message
You requested to find {num_devices} devices but this machine only has {len(visible_devices)} GPUs. What it means
The Trainer profiler parameter accepts a Profiler instance or a string. Strings are lowercased and looked up in a fixed registry (simple, advanced, pytorch, xla). Any string not in PROFILERS raises MisconfigurationException listing the allowed keys.
Source
Thrown at src/lightning/fabric/accelerators/cuda.py:110
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)
if len(available_devices) == num_devices:
# exit early if we found the right number of GPUs
break
View on GitHub (pinned to 9fed5c27d2)
Solutions
- Use one of the registered names: 'simple', 'advanced', 'pytorch', 'xla' (lowercase)
- Pass a Profiler instance for anything custom: from lightning.pytorch.profilers import PyTorchProfiler; Trainer(profiler=PyTorchProfiler())
- Omit profiler (or profiler=None) to disable profiling
Example fix
# before trainer = Trainer(profiler="PyTorch") # after trainer = Trainer(profiler="pytorch") # or from lightning.pytorch.profilers import PyTorchProfiler trainer = Trainer(profiler=PyTorchProfiler())
Defensive patterns
Strategy: type-guard
Validate before calling
ALLOWED_PROFILERS = {"simple", "advanced", "pytorch", "xla"}
def valid_profiler(p):
if isinstance(p, str):
assert p.lower() in ALLOWED_PROFILERS, f"profiler must be one of {ALLOWED_PROFILERS}"
return p Type guard
from lightning.pytorch.profilers import Profiler
ALLOWED = {"simple", "advanced", "pytorch", "xla"}
def is_valid_profiler(p) -> bool:
return p is None or isinstance(p, Profiler) or (isinstance(p, str) and p.lower() in ALLOWED) Prevention
- Use lowercase strings: 'simple', 'advanced', 'pytorch', 'xla'
- Pass a Profiler instance for custom setups
- Fail fast on config load by checking against the allowed set
When it happens
Trigger: Trainer(profiler='tensorboard'), Trainer(profiler='PyTorchProfiler'), Trainer(profiler='') or any misspelled/unregistered profiler name.
Common situations: Typos and case/spelling mistakes; assuming a profiler exists that this Lightning version doesn't register; passing the class name string instead of the class; older/newer versions where the available profiler set differs.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Found sort_by_key: {self._sort_by_key}. Should be within {se
- Found invalid table_kwargs key: {key}. This is already a pos
- You selected an invalid strategy name: `strategy={strategy!r
- You selected an invalid accelerator name: `accelerator={acce
- f"`Trainer(barebones=True, profiler={profiler!r})` was passe
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/d5f307672b7fdd01.
Report an issue: GitHub.