{"record":{"id":"3ee6861ee67dd202","repo":"Lightning-AI/pytorch-lightning","slug":"your-lightningmodule-code-tried-to-access-self-tr","errorCode":null,"errorMessage":"Your LightningModule code tried to access `self.trainer.{item}` but this attribute is not available when using Fabric with a LightningModule.","messagePattern":"Your LightningModule code tried to access `self\\.trainer\\.(.+?)` but this attribute is not available when using Fabric with a LightningModule\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":1846,"sourceCode":"    LightningModule._jit_is_scripting = True\n    try:\n        yield\n    finally:\n        LightningModule._jit_is_scripting = False\n\n\nclass _TrainerFabricShim:\n    \"\"\"Intercepts attribute access on LightningModule's trainer reference and redirects it to the Fabric object.\"\"\"\n\n    def __init__(self, fabric: lf.Fabric) -> None:\n        super().__init__()\n        self._fabric = fabric\n\n    def __getattr__(self, item: Any) -> Any:\n        try:\n            return getattr(self._fabric, item)\n        except AttributeError:\n            raise AttributeError(\n                f\"Your LightningModule code tried to access `self.trainer.{item}` but this attribute is not available\"\n                f\" when using Fabric with a LightningModule.\"\n            )\n","sourceCodeStart":1828,"sourceCodeEnd":1850,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L1828-L1850","documentation":"When a LightningModule is used standalone under Fabric, `self.trainer` does not exist; attribute lookups are forwarded to the Fabric object. If Fabric also lacks the requested attribute, Lightning raises this AttributeError clarifying that `self.trainer.<item>` is not available in Fabric mode.","triggerScenarios":"Sharing a LightningModule between Trainer and Fabric workflows where the code accesses self.trainer.strategy, self.trainer.world_size, etc., and running it under Fabric.","commonSituations":"Migrating a Trainer-based module to Fabric while keeping trainer-specific logic, or accessing training-state attributes that only exist in the Trainer.","solutions":["Branch on availability: use `getattr`/`isinstance(self._fabric, ...)` or check `self._fabric is not None` before touching trainer attributes","Replace trainer lookups with Fabric equivalents (fabric.world_size, fabric.global_rank) or your own state","Guard shared code with `if hasattr(self, \"trainer\") and self.trainer is not None` style checks (careful: __getattr__ is only called when normal lookup fails)"],"exampleFix":"# before\nws = self.trainer.world_size  # under Fabric -> AttributeError\n# after\nfabric = self._fabric\nws = fabric.world_size if fabric is not None else self.trainer.world_size","handlingStrategy":"type-guard","validationCode":"fabric = getattr(model, \"_fabric\", None)\nval = getattr(fabric, item, None) if fabric is not None else getattr(model.trainer, item, None)","typeGuard":"def has_fabric(model) -> bool:\n    return getattr(model, \"_fabric\", None) is not None","tryCatchPattern":"try:\n    v = model.trainer.world_size\nexcept AttributeError:\n    v = model._fabric.world_size","preventionTips":["Never access self.trainer in modules shared with Fabric","Centralize world-size/rank access in one helper that handles both modes"],"tags":["fabric","trainer","attribute-error","lightning"],"backgroundTag":"attribute-not-available-in-mode","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}