{"record":{"id":"95c67e0c758aef69","repo":"Lightning-AI/pytorch-lightning","slug":"str-xla-available-95c67e","errorCode":null,"errorMessage":"{str(_XLA_AVAILABLE)}","messagePattern":"\\{str\\(_XLA_AVAILABLE\\)\\}","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/strategies/launchers/xla.py","lineNumber":49,"sourceCode":"    r\"\"\"Launches processes that run a given function in parallel on XLA supported hardware, and joins them all at the\n    end.\n\n    The main process in which this launcher is invoked creates N so-called worker processes (using the\n    `torch_xla` :func:`xmp.spawn`) that run the given function.\n    Worker processes have a rank that ranges from 0 to N - 1.\n\n    Note:\n        - This launcher requires all objects to be pickleable.\n        - It is important that the entry point to the program/script is guarded by ``if __name__ == \"__main__\"``.\n\n    Args:\n        strategy: A reference to the strategy that is used together with this launcher\n\n    \"\"\"\n\n    def __init__(self, strategy: Union[\"XLAStrategy\", \"XLAFSDPStrategy\"]) -> None:\n        if not _XLA_AVAILABLE:\n            raise ModuleNotFoundError(str(_XLA_AVAILABLE))\n        self._strategy = strategy\n        self._start_method = \"fork\"\n\n    @property\n    @override\n    def is_interactive_compatible(self) -> bool:\n        return True\n\n    @override\n    def launch(self, function: Callable, *args: Any, **kwargs: Any) -> Any:\n        \"\"\"Launches processes that run the given function in parallel.\n\n        The function is allowed to have a return value. However, when all processes join, only the return value\n        of worker process 0 gets returned from this `launch` method in the main process.\n\n        Arguments:\n            function: The entry point for all launched processes.\n            *args: Optional positional arguments to be passed to the given function.","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/strategies/launchers/xla.py#L31-L67","documentation":"The XLA launcher requires torch_xla, and at import time Lightning captured the missing-dependency ModuleNotFound message in _XLA_AVAILABLE. Constructing the XLALauncher without torch_xla installed re-raises that stored import error, typically telling you the 'torch_xla' package is missing.","triggerScenarios":"Instantiating XLAStrategy/XLAFSDPStrategy (e.g. Fabric(accelerator='tpu') or strategy='xla...') without torch_xla installed, or with a torch_xla build mismatched to the installed torch version so the import failed.","commonSituations":"Running TPU code in a CPU/GPU environment; new environments where requirements omitted torch_xla; upgrading torch without rebuilding torch_xla, making the import fail and getting captured as _XLA_AVAILABLE error state.","solutions":["pip install torch_xla matching your torch version (see PyTorch/XLA release matrix)","If you didn't intend TPU, switch accelerator/strategy to 'cpu' or 'gpu' equivalents","Verify the install with 'python -c \"import torch_xla\"' to surface any version-mismatch import errors"],"exampleFix":"# before\nfabric = Fabric(accelerator=\"tpu\", devices=8)  # ModuleNotFoundError: No module named 'torch_xla'\n\n# after\npip install torch_xla==<version matching torch>\nfabric = Fabric(accelerator=\"tpu\", devices=8)","handlingStrategy":"validation","validationCode":"try:\n    import torch_xla  # noqa\n    xla_ok = True\nexcept ImportError:\n    xla_ok = False\nif not xla_ok:\n    accelerator = \"cpu\"  # or fail fast with a clear message","typeGuard":"def xla_available() -> bool:\n    try:\n        import torch_xla  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    launcher = XLALauncher(strategy)\nexcept ModuleNotFoundError as e:\n    if \"torch_xla\" in str(e):\n        subprocess.run([sys.executable, \"-m\", \"pip\", \"install\", \"torch_xla\"])\n    raise","preventionTips":["Gate TPU/XLA code paths behind an import check","Keep torch and torch_xla versions locked together in requirements"],"tags":["pytorch-lightning","xla","tpu","missing-dependency","torch-xla"],"backgroundTag":"missing-optional-accelerator-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}