Lightning-AI/pytorch-lightning · error · AttributeError
Your LightningModule code tried to access `self.trainer.{ite
Error message
Your LightningModule code tried to access `self.trainer.{item}` but this attribute is not available when using Fabric with a LightningModule. What it means
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.
Source
Thrown at src/lightning/pytorch/core/module.py:1846
LightningModule._jit_is_scripting = True
try:
yield
finally:
LightningModule._jit_is_scripting = False
class _TrainerFabricShim:
"""Intercepts attribute access on LightningModule's trainer reference and redirects it to the Fabric object."""
def __init__(self, fabric: lf.Fabric) -> None:
super().__init__()
self._fabric = fabric
def __getattr__(self, item: Any) -> Any:
try:
return getattr(self._fabric, item)
except AttributeError:
raise AttributeError(
f"Your LightningModule code tried to access `self.trainer.{item}` but this attribute is not available"
f" when using Fabric with a LightningModule."
)
View on GitHub (pinned to 9fed5c27d2)
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)
Example fix
# before ws = self.trainer.world_size # under Fabric -> AttributeError # after fabric = self._fabric ws = fabric.world_size if fabric is not None else self.trainer.world_size
Defensive patterns
Strategy: type-guard
Validate before calling
fabric = getattr(model, "_fabric", None) val = getattr(fabric, item, None) if fabric is not None else getattr(model.trainer, item, None)
Type guard
def has_fabric(model) -> bool:
return getattr(model, "_fabric", None) is not None Try / catch
try:
v = model.trainer.world_size
except AttributeError:
v = model._fabric.world_size Prevention
- Never access self.trainer in modules shared with Fabric
- Centralize world-size/rank access in one helper that handles both modes
When it happens
Trigger: 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.
Common situations: Migrating a Trainer-based module to Fabric while keeping trainer-specific logic, or accessing training-state attributes that only exist in the Trainer.
Related errors
- Received multiple values for {', '.join(duplicated_plugin_ke
- Received both `precision={precision_input}` and `plugins={se
- accelerator set through both strategy class and accelerator
- precision set through both strategy class and plugins, choos
- checkpoint_io set through both strategy class and plugins, c
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/3ee6861ee67dd202.
Report an issue: GitHub.