{"record":{"id":"d5a753c6b4987430","repo":"Lightning-AI/pytorch-lightning","slug":"str-xla-available-d5a753","errorCode":null,"errorMessage":"{str(_XLA_AVAILABLE)}","messagePattern":"\\{str\\(_XLA_AVAILABLE\\)\\}","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/strategies/single_xla.py","lineNumber":39,"sourceCode":"from lightning.fabric.plugins import CheckpointIO, Precision, XLAPrecision\nfrom lightning.fabric.plugins.io.xla import XLACheckpointIO\nfrom lightning.fabric.strategies import _StrategyRegistry\nfrom lightning.fabric.strategies.single_device import SingleDeviceStrategy\nfrom lightning.fabric.utilities.types import _DEVICE\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[Accelerator] = None,\n        checkpoint_io: Optional[XLACheckpointIO] = None,\n        precision: Optional[XLAPrecision] = None,\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\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=precision,\n        )\n\n    @property\n    @override\n    def checkpoint_io(self) -> XLACheckpointIO:\n        plugin = self._checkpoint_io\n        if plugin is not None:","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/strategies/single_xla.py#L21-L57","documentation":"SingleXLAStrategy.__init__ checks the _XLA_AVAILABLE flag (a ModuleNotFoundError captured at import time) and re-raises it when torch_xla is not installed. Using any XLA strategy requires the torch_xla package.","triggerScenarios":"Instantiating SingleXLAStrategy (or XLA parallel variants, or Fabric(accelerator='tpu', ...)) in an environment without torch_xla installed; the captured import error message is surfaced verbatim.","commonSituations":"Running TPU/colab workflows in a CPU/GPU environment; missing or version-mismatched torch_xla installation; wrong environment/conda env activated.","solutions":["pip install lightning[xla] (or install a torch_xla build matching your torch version)","Verify import: python -c 'import torch_xla' and resolve any reported dependency errors","If TPUs were not intended, switch strategy/accelerator (e.g. single-device or DDP on GPU)"],"exampleFix":"# before\nstrategy = SingleXLAStrategy(device='xla:0')  # ModuleNotFoundError: No module named 'torch_xla'\n# after (shell)\n# pip install lightning[xla]\nstrategy = SingleXLAStrategy(device='xla:0')","handlingStrategy":"validation","validationCode":"try:\n    import torch_xla  # noqa: F401\n    xla_ok = True\nexcept ImportError:\n    xla_ok = False\nassert xla_ok, 'torch_xla required for XLA strategies; pip install lightning[xla]'","typeGuard":"def xla_available() -> bool:\n    try:\n        import torch_xla  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":null,"preventionTips":["Gate XLA/TPU code paths on torch_xla availability","Pin torch_xla versions compatible with your torch install"],"tags":["lightning","fabric","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"}