Lightning-AI/pytorch-lightning · critical · ModuleNotFoundError
raise ModuleNotFoundError(str(_XLA_AVAILABLE))
Error message
raise ModuleNotFoundError(str(_XLA_AVAILABLE))
What it means
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.
Source
Thrown at src/lightning/pytorch/strategies/single_xla.py:45
from lightning.pytorch.plugins.precision.xla import XLAPrecision
from lightning.pytorch.strategies.single_device import SingleDeviceStrategy
from lightning.pytorch.trainer.states import TrainerFn
from lightning.pytorch.utilities import find_shared_parameters, set_shared_parameters
class SingleDeviceXLAStrategy(SingleDeviceStrategy):
"""Strategy for training on a single XLA device."""
def __init__(
self,
device: _DEVICE,
accelerator: Optional["pl.accelerators.Accelerator"] = None,
checkpoint_io: Optional[Union[XLACheckpointIO, _WrappingCheckpointIO]] = None,
precision_plugin: Optional[XLAPrecision] = None,
debug: bool = False,
):
if not _XLA_AVAILABLE:
raise ModuleNotFoundError(str(_XLA_AVAILABLE))
if isinstance(device, torch.device):
# unwrap the `torch.device` in favor of `xla_device`
device = device.index
import torch_xla.core.xla_model as xm
super().__init__(
accelerator=accelerator,
device=xm.xla_device(device),
checkpoint_io=checkpoint_io,
precision_plugin=precision_plugin,
)
self.debug = debug
@property
@override
def checkpoint_io(self) -> Union[XLACheckpointIO, _WrappingCheckpointIO]:
plugin = self._checkpoint_io
if plugin is not None:View on GitHub (pinned to 9fed5c27d2)
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
Example fix
# before strategy = SingleDeviceXLAStrategy() # ModuleNotFoundError # after # pip install lightning[xla] strategy = SingleDeviceXLAStrategy()
Defensive patterns
Strategy: validation
Validate before calling
from lightning.fabric.utilities.imports import _XLA_AVAILABLE assert _XLA_AVAILABLE, "pip install lightning[xla] before using SingleDeviceXLAStrategy"
Prevention
- Install lightning[xla] / matching torch_xla in all environments that import XLA strategies
When it happens
Trigger: Instantiating SingleDeviceXLAStrategy (e.g. accelerator='tpu' with devices=1, or explicitly) without torch_xla installed; environment with plain torch only.
Common situations: Trying single-TPU/GPU-with-XLA setups locally; CI environments lacking the xla extra.
Understand the failure class
Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.
Related errors
- str(_XLA_AVAILABLE)
- {str(_XLA_AVAILABLE)}
- {str(_XLA_AVAILABLE)}
- raise ModuleNotFoundError(str(_XLA_AVAILABLE))
- raise ModuleNotFoundError(str(_XLA_AVAILABLE))
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/1da873629de8006f.
Report an issue: GitHub.