{"record":{"id":"37aa10ae362325ea","repo":"Lightning-AI/pytorch-lightning","slug":"xla-available","errorCode":null,"errorMessage":"{_XLA_AVAILABLE}","messagePattern":"\\{_XLA_AVAILABLE\\}","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"critical","filePath":"src/lightning/pytorch/plugins/precision/xla.py","lineNumber":43,"sourceCode":"from lightning.pytorch.plugins.precision.precision import Precision\nfrom lightning.pytorch.utilities.exceptions import MisconfigurationException\n\n\nclass XLAPrecision(Precision):\n    \"\"\"Plugin for training with XLA.\n\n    Args:\n        precision: Full precision (32-true) or half precision (16-true, bf16-true).\n\n    Raises:\n        ValueError:\n            If unsupported ``precision`` is provided.\n\n    \"\"\"\n\n    def __init__(self, precision: _PRECISION_INPUT = \"32-true\") -> None:\n        if not _XLA_AVAILABLE:\n            raise ModuleNotFoundError(str(_XLA_AVAILABLE))\n\n        supported_precision = get_args(_PRECISION_INPUT)\n        if precision not in supported_precision:\n            raise ValueError(\n                f\"`precision={precision!r})` is not supported in XLA.\"\n                f\" `precision` must be one of: {supported_precision}.\"\n            )\n        self.precision = precision\n\n        if precision == \"16-true\":\n            os.environ[\"XLA_USE_F16\"] = \"1\"\n            self._desired_dtype = torch.float16\n        elif precision == \"bf16-true\":\n            os.environ[\"XLA_USE_BF16\"] = \"1\"\n            self._desired_dtype = torch.bfloat16\n        else:\n            self._desired_dtype = torch.float32\n","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/plugins/precision/xla.py#L25-L61","documentation":"Raised by XLAPrecision.__init__ when the torch_xla package (and its dependencies) is not importable in the current environment. Lightning's XLA precision plugin delegates all mixed-precision handling to torch_xla, so the plugin cannot be constructed without it. The message string is the import-error text captured by Lightning's module availability check.","triggerScenarios":"Constructing XLAPrecision (or passing plugins=XLAPrecision(...) / strategy='xla' with a precision plugin) in an environment where `import torch_xla` fails, e.g. plain CPU/GPU machines or a PyTorch/XLA version mismatch.","commonSituations":"Running a training script written for TPU/TPOD on a local CUDA or CPU machine; installing pytorch-lightning but forgetting `torch-xla`; upgrading PyTorch to a version with no matching torch_xla wheel.","solutions":["Install torch_xla matching your PyTorch and Python version (e.g. pip install torch_xla --index-url https://download.pytorch.org/whl/cpu)","If you're not on TPU, switch to a different plugin/strategy (e.g. MixedPrecision for CUDA, no plugin for CPU)","Verify with `python -c \"import torch_xla\"` to see the underlying import error"],"exampleFix":"# before\nfrom lightning.pytorch.plugins import XLAPrecision\nplugin = XLAPrecision()  # ModuleNotFoundError on non-TPU machine\n\n# after (CUDA machine)\nfrom lightning.pytorch.plugins import MixedPrecision\nplugin = MixedPrecision(precision=\"16-mixed\")","handlingStrategy":"validation","validationCode":"from lightning.pytorch.utilities.imports import _XLA_AVAILABLE\nif not _XLA_AVAILABLE:\n    raise SystemExit(\"torch_xla not available; use a different precision plugin\")","typeGuard":null,"tryCatchPattern":"try:\n    plugin = XLAPrecision()\nexcept ModuleNotFoundError as e:\n    print(f\"XLA unavailable ({e}); falling back to MixedPrecision\")\n    plugin = MixedPrecision(precision=\"16-mixed\")","preventionTips":["Gate plugin selection on _XLA_AVAILABLE / torch.cuda.is_available()","Keep a separate requirements-tpu.txt with matching torch and torch_xla versions","Run `python -c \"import torch_xla\"` in CI for TPU jobs"],"tags":["xla","tpu","dependency-missing","pytorch-lightning"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}