{"record":{"id":"386a6d3446872d4a","repo":"Lightning-AI/pytorch-lightning","slug":"accessing-the-xla-device-before-processes-have-spa-386a6d","errorCode":null,"errorMessage":"Accessing the XLA device before processes have spawned is not allowed.","messagePattern":"Accessing the XLA device before processes have spawned is not allowed\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/strategies/xla.py","lineNumber":109,"sourceCode":"    def precision_plugin(self) -> XLAPrecision:\n        plugin = self._precision_plugin\n        if plugin is not None:\n            assert isinstance(plugin, XLAPrecision)\n            return plugin\n        return XLAPrecision()\n\n    @precision_plugin.setter\n    @override\n    def precision_plugin(self, precision_plugin: Optional[Precision]) -> None:\n        if precision_plugin is not None and not isinstance(precision_plugin, XLAPrecision):\n            raise TypeError(f\"The XLA strategy can only work with the `XLAPrecision` plugin, found {precision_plugin}\")\n        self._precision_plugin = precision_plugin\n\n    @property\n    @override\n    def root_device(self) -> torch.device:\n        if not self._launched:\n            raise RuntimeError(\"Accessing the XLA device before processes have spawned is not allowed.\")\n        import torch_xla.core.xla_model as xm\n\n        return xm.xla_device()\n\n    @property\n    @override\n    def global_rank(self) -> int:\n        return super().global_rank if self._launched else 0\n\n    @property\n    @override\n    def local_rank(self) -> int:\n        return super().local_rank if self._launched else 0\n\n    @property\n    @override\n    def node_rank(self) -> int:\n        return super().node_rank if self._launched else 0","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/strategies/xla.py#L91-L127","documentation":"XLAStrategy.root_device returns xm.xla_device(), but the XLA runtime is only initialized after the launcher has spawned processes. Accessing root_device before launch (self._launched is False) raises RuntimeError to prevent initializing XLA outside the multiprocess context.","triggerScenarios":"Reading strategy.root_device (directly or via trainer logic) before trainer fit/launch — e.g. in LightningModule.__init__, configure_model, or module-level code — while using XLAStrategy.","commonSituations":"Moving device setup out of hooks; logging devices pre-run; utility code that queries strategy.root_device at import time.","solutions":["Access root_device only after launch: inside setup(), on_fit_start, or training_step hooks","For device-needing setup in LightningModule, use configure_model()/setup() which run post-launch","Guard with strategy._launched if you must probe early"],"exampleFix":"# before\nclass Lit(L.LightningModule):\n    def __init__(self):\n        self.x = torch.zeros(3, device=self.trainer.strategy.root_device)  # RuntimeError\n\n# after\nclass Lit(L.LightningModule):\n    def setup(self, stage=None):\n        self.x = torch.zeros(3, device=self.trainer.strategy.root_device)  # after launch","handlingStrategy":"validation","validationCode":"if not getattr(strategy, \"_launched\", False):\n    # defer device access to post-launch hooks\n    ...","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Access root_device only from setup()/run-phase hooks","Initialize tensors on self.device inside LightningModule hooks, never in __init__"],"tags":["xla","root-device","lifecycle"],"backgroundTag":"access-before-initialization","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}