{"record":{"id":"e72c263799dfd68f","repo":"Lightning-AI/pytorch-lightning","slug":"cannot-determine-world-size-environment-variable","errorCode":null,"errorMessage":"Cannot determine world size. Environment variable `JSM_NAMESPACE_SIZE` not found. Make sure you run your executable with `jsrun`.","messagePattern":"Cannot determine world size\\. Environment variable `JSM_NAMESPACE_SIZE` not found\\. Make sure you run your executable with `jsrun`\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/plugins/environments/lsf.py","lineNumber":96,"sourceCode":"    @property\n    @override\n    def main_port(self) -> int:\n        \"\"\"The main port is calculated from the LSF job ID.\"\"\"\n        return self._main_port\n\n    @staticmethod\n    @override\n    def detect() -> bool:\n        \"\"\"Returns ``True`` if the current process was launched using the ``jsrun`` command.\"\"\"\n        required_env_vars = {\"LSB_JOBID\", \"LSB_DJOB_RANKFILE\", \"JSM_NAMESPACE_LOCAL_RANK\", \"JSM_NAMESPACE_SIZE\"}\n        return required_env_vars.issubset(os.environ.keys())\n\n    @override\n    def world_size(self) -> int:\n        \"\"\"The world size is read from the environment variable ``JSM_NAMESPACE_SIZE``.\"\"\"\n        world_size = os.environ.get(\"JSM_NAMESPACE_SIZE\")\n        if world_size is None:\n            raise ValueError(\n                \"Cannot determine world size. Environment variable `JSM_NAMESPACE_SIZE` not found.\"\n                \" Make sure you run your executable with `jsrun`.\"\n            )\n        return int(world_size)\n\n    @override\n    def set_world_size(self, size: int) -> None:\n        log.debug(\"LSFEnvironment.set_world_size was called, but setting world size is not allowed. Ignored.\")\n\n    @override\n    def global_rank(self) -> int:\n        \"\"\"The world size is read from the environment variable ``JSM_NAMESPACE_RANK``.\"\"\"\n        global_rank = os.environ.get(\"JSM_NAMESPACE_RANK\")\n        if global_rank is None:\n            raise ValueError(\n                \"Cannot determine global rank. Environment variable `JSM_NAMESPACE_RANK` not found.\"\n                \" Make sure you run your executable with `jsrun`.\"\n            )","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/plugins/environments/lsf.py#L78-L114","documentation":"`LSFEnvironment.world_size()` reads `JSM_NAMESPACE_SIZE`, set by IBM Spectrum Launch (jsrun) on LSF clusters. If the variable is absent — the process was not launched via `jsrun` — it raises ValueError telling you to run your executable with jsrun.","triggerScenarios":"Accessing `world_size` (directly or via strategy startup) with `LSFEnvironment` while `JSM_NAMESPACE_SIZE` is unset: e.g. running `python train.py` directly on a login node instead of `jsrun --np ... train.py`, or a mis-set-up jsrun invocation.","commonSituations":"IBM LSF/HPC clusters (Summit-class systems); debugging scripts outside the scheduler; sbatch/jsrun wrappers that drop environment variables; typos in jsrun launch commands.","solutions":["Launch through jsrun: `jsrun -n <ranks> -a <gpu_per_rank> -c <cpus> python train.py`","If you didn't intend LSF, use the correct environment (e.g. LightningEnvironment/manual) for your launcher","Verify with `echo $JSM_NAMESPACE_SIZE` inside the same job context"],"exampleFix":"# before (login node / plain python)\npython train.py  # JSM_NAMESPACE_SIZE unset -> ValueError\n\n# after\njsrun -n 4 -a 1 -c 6 -g 1 python train.py","handlingStrategy":"validation","validationCode":"import os\nassert os.environ.get('JSM_NAMESPACE_SIZE'), 'launch with jsrun (JSM_NAMESPACE_SIZE unset)'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always launch LSF jobs via jsrun","Check JSM_* variables in job preamble"],"tags":["lsf","jsrun","hpc","missing-env-var","distributed"],"backgroundTag":"missing-env-var","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}