{"record":{"id":"85e5971ef90283e9","repo":"Lightning-AI/pytorch-lightning","slug":"str-xla-available-85e597","errorCode":null,"errorMessage":"str(_XLA_AVAILABLE)","messagePattern":"str\\(_XLA_AVAILABLE\\)","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/plugins/precision/xla.py","lineNumber":41,"sourceCode":"\n_PRECISION_INPUT = Literal[\"32-true\", \"16-true\", \"bf16-true\"]\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) -> None:\n        if not _XLA_AVAILABLE:\n            raise ModuleNotFoundError(str(_XLA_AVAILABLE))\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\n    @override","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/plugins/precision/xla.py#L23-L59","documentation":"XLAPrecision requires torch_xla (PyTorch/XLA for TPU and other XLA devices). The module-level availability check failed and stored the underlying import error in _XLA_AVAILABLE; the constructor converts it into this ModuleNotFoundError when the plugin is created where torch_xla cannot be imported.","triggerScenarios":"Instantiating XLAPrecision(...) on a machine where 'import torch_xla' fails — no torch_xla wheel installed, or an xla build that does not match the installed torch version (which typically raises ImportError at import time).","commonSituations":"Running a TPU-targeted Fabric script locally on CPU/GPU; torch and torch_xia version mismatch after upgrading one but not the other.","solutions":["Install matching torch_xla: pip install torch_xla with the version corresponding to your torch build (see PyTorch/XLA release compatibility)","Confirm 'python -c \"import torch_xla\"' succeeds in the same environment/interpreter you run Fabric with","If you are not targeting TPU/XLA, drop the XLA plugin and use MixedPrecision or the strategy default"],"exampleFix":"# before\nprecision = XLAPrecision(\"bf16-mixed\")  # on a machine without torch_xla\n# after (shell)\npip install torch_xla --index-url https://download.pytorch.org/whl/cpu","handlingStrategy":"fallback","validationCode":"import importlib.util\nif importlib.util.find_spec(\"torch_xla\") is None:\n    raise RuntimeError(\"torch_xla not installed; cannot use XLAPrecision\")","typeGuard":"import importlib.util\n\ndef xla_available() -> bool:\n    return importlib.util.find_spec(\"torch_xla\") is not None","tryCatchPattern":"try:\n    precision = XLAPrecision(\"bf16-mixed\")\nexcept ModuleNotFoundError:\n    precision = MixedPrecision(\"bf16-mixed\")","preventionTips":["Install torch_xla matched to your torch version","Detect device (XLA vs CUDA vs CPU) before choosing precision plugins"],"tags":["xla","tpu","missing-dependency","pytorch-lightning"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}