{"record":{"id":"1da873629de8006f","repo":"Lightning-AI/pytorch-lightning","slug":"raise-modulenotfounderror-str-xla-available-1da873","errorCode":null,"errorMessage":"raise ModuleNotFoundError(str(_XLA_AVAILABLE))","messagePattern":"raise ModuleNotFoundError\\(str\\(_XLA_AVAILABLE\\)\\)","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"critical","filePath":"src/lightning/pytorch/strategies/single_xla.py","lineNumber":45,"sourceCode":"from lightning.pytorch.plugins.precision.xla import XLAPrecision\nfrom lightning.pytorch.strategies.single_device import SingleDeviceStrategy\nfrom lightning.pytorch.trainer.states import TrainerFn\nfrom lightning.pytorch.utilities import find_shared_parameters, set_shared_parameters\n\n\nclass SingleDeviceXLAStrategy(SingleDeviceStrategy):\n    \"\"\"Strategy for training on a single XLA device.\"\"\"\n\n    def __init__(\n        self,\n        device: _DEVICE,\n        accelerator: Optional[\"pl.accelerators.Accelerator\"] = None,\n        checkpoint_io: Optional[Union[XLACheckpointIO, _WrappingCheckpointIO]] = None,\n        precision_plugin: Optional[XLAPrecision] = None,\n        debug: bool = False,\n    ):\n        if not _XLA_AVAILABLE:\n            raise ModuleNotFoundError(str(_XLA_AVAILABLE))\n        if isinstance(device, torch.device):\n            # unwrap the `torch.device` in favor of `xla_device`\n            device = device.index\n        import torch_xla.core.xla_model as xm\n\n        super().__init__(\n            accelerator=accelerator,\n            device=xm.xla_device(device),\n            checkpoint_io=checkpoint_io,\n            precision_plugin=precision_plugin,\n        )\n        self.debug = debug\n\n    @property\n    @override\n    def checkpoint_io(self) -> Union[XLACheckpointIO, _WrappingCheckpointIO]:\n        plugin = self._checkpoint_io\n        if plugin is not None:","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/strategies/single_xla.py#L27-L63","documentation":"SingleDeviceXLAStrategy (single-process XLA) requires torch_xla. Its __init__ raises ModuleNotFoundError with Lightning's _XLA_AVAILABLE message when the package is missing, before any XLA setup is attempted.","triggerScenarios":"Instantiating SingleDeviceXLAStrategy (e.g. accelerator='tpu' with devices=1, or explicitly) without torch_xla installed; environment with plain torch only.","commonSituations":"Trying single-TPU/GPU-with-XLA setups locally; CI environments lacking the xla extra.","solutions":["pip install lightning[xla] or a torch_xla wheel matching your torch/torchvision versions","Verify import torch_xla succeeds","If XLA wasn't intended, use SingleDeviceStrategy (CPU/GPU) instead"],"exampleFix":"# before\nstrategy = SingleDeviceXLAStrategy()  # ModuleNotFoundError\n\n# after\n# pip install lightning[xla]\nstrategy = SingleDeviceXLAStrategy()","handlingStrategy":"validation","validationCode":"from lightning.fabric.utilities.imports import _XLA_AVAILABLE\nassert _XLA_AVAILABLE, \"pip install lightning[xla] before using SingleDeviceXLAStrategy\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Install lightning[xla] / matching torch_xla in all environments that import XLA strategies"],"tags":["xla","tpu","missing-dependency"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}