{"record":{"id":"95b6a670b1a312dd","repo":"Lightning-AI/pytorch-lightning","slug":"local-world-size-should-be-1-got-local-worl","errorCode":null,"errorMessage":"`local_world_size` should be >= 1, got {local_world_size}.","messagePattern":"`local_world_size` should be >= 1, got (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/utilities/data.py","lineNumber":453,"sourceCode":"    ) is not None:\n        objects[id(sampler)] = sampler\n    for obj in objects.values():\n        set_epoch = getattr(obj, \"set_epoch\", None)\n        if callable(set_epoch):\n            set_epoch(epoch)\n\n\ndef suggested_max_num_workers(local_world_size: int) -> int:\n    \"\"\"Suggests an upper bound of ``num_workers`` to use in a PyTorch :class:`~torch.utils.data.DataLoader` based on\n    the number of CPU cores available on the system and the number of distributed processes in the current machine.\n\n    Args:\n        local_world_size: The number of distributed processes running on the current machine. Set this to the number\n            of devices configured in Fabric/Trainer.\n\n    \"\"\"\n    if local_world_size < 1:\n        raise ValueError(f\"`local_world_size` should be >= 1, got {local_world_size}.\")\n    cpu_count = _num_cpus_available()\n    return max(1, cpu_count // local_world_size - 1)  # -1 to leave some resources for main process\n\n\ndef _num_cpus_available() -> int:\n    if hasattr(os, \"sched_getaffinity\"):\n        return len(os.sched_getaffinity(0))\n\n    cpu_count = os.cpu_count()\n    return 1 if cpu_count is None else cpu_count\n\n\nclass AttributeDict(dict):\n    \"\"\"A container to store state variables of your program.\n\n    This is a drop-in replacement for a Python dictionary, with the additional functionality to access and modify keys\n    through attribute lookup for convenience.\n","sourceCodeStart":435,"sourceCodeEnd":471,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/utilities/data.py#L435-L471","documentation":"`lightning.fabric.utilities.data.suggested_max_num_workers` validates its `local_world_size` argument (number of processes/devices on the current machine) and rejects values below 1, since a non-positive process count is meaningless for computing the suggested worker count `max(1, cpu_count // local_world_size - 1)`.","triggerScenarios":"Calling `suggested_max_num_workers(local_world_size=0)` or a negative value, e.g. by passing an uninitialized device count, `len(devices)` where devices is empty, or a variable computed before it was assigned.","commonSituations":"Scripts computing world size from CLI args or environment before validation (default 0), or passing a numpy/None-derived value; also unit tests of the helper itself.","solutions":["Ensure the value passed is >= 1 — typically the number of devices/processes per node (e.g. `len(fabric.device_ids)` or torch.distributed.get_world_size() // num_nodes).","Validate/initialize the variable holding local_world_size before the call (guard against 0/None defaults).","If computing from a devices list, check it is non-empty first."],"exampleFix":"# before\nworkers = suggested_max_num_workers(local_world_size=num_local_procs)  # 0 when unset\n\n# after\nnum_local_procs = num_local_procs or 1\nworkers = suggested_max_num_workers(local_world_size=num_local_procs)","handlingStrategy":"validation","validationCode":"local_world_size = max(1, int(local_world_size or 0))\nworkers = suggested_max_num_workers(local_world_size=local_world_size)","typeGuard":"def is_valid_world_size(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":null,"preventionTips":["Initialize world-size variables to 1, not 0.","Derive local_world_size from the actual devices list length after device setup."],"tags":["pytorch-lightning","validation","num-workers","world-size"],"backgroundTag":"invalid-argument-value","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}