{"record":{"id":"28fd5e77877fe18c","repo":"Lightning-AI/pytorch-lightning","slug":"num-nodes-must-be-a-positive-integer-but-got-n-28fd5e","errorCode":null,"errorMessage":"`num_nodes` must be a positive integer, but got {num_nodes}.","messagePattern":"`num_nodes` must be a positive integer, but got (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/trainer/connectors/accelerator_connector.py","lineNumber":314,"sourceCode":"                if self._strategy_flag.parallel_devices[0].type == \"cpu\":\n                    if self._accelerator_flag and self._accelerator_flag not in (\"auto\", \"cpu\"):\n                        raise MisconfigurationException(\n                            f\"CPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,\"\n                            f\" but accelerator set to {self._accelerator_flag}, please choose one device type\"\n                        )\n                    self._accelerator_flag = \"cpu\"\n                if self._strategy_flag.parallel_devices[0].type == \"cuda\":\n                    if self._accelerator_flag and self._accelerator_flag not in (\"auto\", \"cuda\", \"gpu\"):\n                        raise MisconfigurationException(\n                            f\"GPU parallel_devices set through {self._strategy_flag.__class__.__name__} class,\"\n                            f\" but accelerator set to {self._accelerator_flag}, please choose one device type\"\n                        )\n                    self._accelerator_flag = \"cuda\"\n                self._parallel_devices = self._strategy_flag.parallel_devices\n\n    def _check_device_config_and_set_final_flags(self, devices: Union[list[int], str, int], num_nodes: int) -> None:\n        if not isinstance(num_nodes, int) or num_nodes < 1:\n            raise ValueError(f\"`num_nodes` must be a positive integer, but got {num_nodes}.\")\n\n        self._num_nodes_flag = num_nodes\n        self._devices_flag = devices\n\n        if self._devices_flag in ([], 0, \"0\"):\n            accelerator_name = (\n                self._accelerator_flag.__class__.__qualname__\n                if isinstance(self._accelerator_flag, Accelerator)\n                else self._accelerator_flag\n            )\n            raise MisconfigurationException(\n                f\"`Trainer(devices={self._devices_flag!r})` value is not a valid input\"\n                f\" using {accelerator_name} accelerator.\"\n            )\n\n    @staticmethod\n    def _choose_auto_accelerator() -> str:\n        \"\"\"Choose the accelerator type (str) based on availability.\"\"\"","sourceCodeStart":296,"sourceCodeEnd":332,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/trainer/connectors/accelerator_connector.py#L296-L332","documentation":"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.","triggerScenarios":"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'.","commonSituations":"num_nodes computed from SLURM/环境 variables as strings or floats; arithmetic that can yield 0 on single-node runs.","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"],"exampleFix":"# before\ntrainer = Trainer(num_nodes=int(os.environ.get(\"WORLD_SIZE\", 0)))\n# after\nnum_nodes = max(1, int(os.environ.get(\"WORLD_SIZE\", 1)))\ntrainer = Trainer(num_nodes=num_nodes)","handlingStrategy":"validation","validationCode":"num_nodes = int(num_nodes)\nif num_nodes < 1:\n    raise ValueError(f\"num_nodes must be >= 1, got {num_nodes}\")\ntrainer = Trainer(num_nodes=num_nodes)","typeGuard":"def valid_num_nodes(n) -> bool:\n    return isinstance(n, int) and not isinstance(n, bool) and n >= 1","tryCatchPattern":null,"preventionTips":["Coerce env-var-derived node counts with int() and clamp to >=1","Default num_nodes=1 for single-node runs instead of computing it"],"tags":["pytorch-lightning","num-nodes","input-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}