Lightning-AI/pytorch-lightning · error · ValueError
`num_nodes` must be a positive integer, but got {num_nodes}.
Error message
`num_nodes` must be a positive integer, but got {num_nodes}. What it means
Trainer validates that num_nodes is a positive integer (>=1). Anything else — zero, negatives, floats, strings — raises ValueError during connector init. This is plain input validation before any distributed setup.
Source
Thrown at src/lightning/pytorch/trainer/connectors/accelerator_connector.py:314
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
)
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."""View on GitHub (pinned to 9fed5c27d2)
Solutions
- Coerce and validate: int(num_nodes) with a >=1 guard before constructing Trainer
- Default to 1 on single-node runs instead of computing 0
Example fix
# before
trainer = Trainer(num_nodes=int(os.environ.get("WORLD_SIZE", 0)))
# after
num_nodes = max(1, int(os.environ.get("WORLD_SIZE", 1)))
trainer = Trainer(num_nodes=num_nodes) Defensive patterns
Strategy: validation
Validate before calling
num_nodes = int(num_nodes)
if num_nodes < 1:
raise ValueError(f"num_nodes must be >= 1, got {num_nodes}")
trainer = Trainer(num_nodes=num_nodes) Type guard
def valid_num_nodes(n) -> bool:
return isinstance(n, int) and not isinstance(n, bool) and n >= 1 Prevention
- Coerce env-var-derived node counts with int() and clamp to >=1
- Default num_nodes=1 for single-node runs instead of computing it
When it happens
Trigger: Trainer(num_nodes=0), Trainer(num_nodes=-1), Trainer(num_nodes=2.0), or num_nodes sourced from an unparsed env var/config string like '2'.
Common situations: num_nodes computed from SLURM/环境 variables as strings or floats; arithmetic that can yield 0 on single-node runs.
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
- Device should be CPU, got {device} instead.
- `devices` selected with `CPUAccelerator` should be an int >
- Device should be CUDA, got {device} instead.
- You requested to find {num_devices} devices but there are no
- 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/28fd5e77877fe18c.
Report an issue: GitHub.