Lightning-AI/pytorch-lightning · error · AttributeError
You marked '{name}' as a forward method, but `{type(self._or
Error message
You marked '{name}' as a forward method, but `{type(self._original_module).__name__}.{name}` does not exist or is not a method. What it means
mark_forward_method() verifies via getattr(module, name) that the attribute exists and is a bound method before registering it in _forward_methods. If the underlying nn.Module has no such method (or it is a property/attribute), AttributeError is raised.
Source
Thrown at src/lightning/fabric/wrappers.py:173
prefix=prefix,
keep_vars=keep_vars,
)
@override
def load_state_dict( # type: ignore[override]
self, state_dict: Mapping[str, Any], strict: bool = True, **kwargs: Any
) -> _IncompatibleKeys:
return self._original_module.load_state_dict(state_dict=state_dict, strict=strict, **kwargs)
def mark_forward_method(self, method: Union[MethodType, str]) -> None:
"""Mark a method as a 'forward' method to prevent it bypassing the strategy wrapper (e.g., DDP)."""
if not isinstance(method, (MethodType, str)):
raise TypeError(f"Expected a method or a string, but got: {type(method).__name__}")
name = method if isinstance(method, str) else method.__name__
if name == "forward":
raise ValueError("You cannot mark the forward method itself as a forward method.")
if not isinstance(getattr(self._original_module, name, None), MethodType):
raise AttributeError(
f"You marked '{name}' as a forward method, but `{type(self._original_module).__name__}.{name}` does not"
f" exist or is not a method."
)
self._forward_methods.add(name)
def _redirection_through_forward(self, method_name: str) -> Callable:
assert method_name != "forward"
original_forward = self._original_module.forward
def wrapped_forward(*args: Any, **kwargs: Any) -> Any:
# Unpatch ourselves immediately before calling the method `method_name`
# because itself may want to call the real `forward`
self._original_module.forward = original_forward
# Call the actual method e.g. `.training_step(...)`
method = getattr(self._original_module, method_name)
return method(*args, **kwargs)
# We make the caller "unknowingly" send their arguments through the forward_module's `__call__`.View on GitHub (pinned to 9fed5c27d2)
Solutions
- Verify with hasattr(model, name) and callable(getattr(model, name)) before marking
- Fix typos: the string must exactly match the method name on the underlying nn.Module
- If the method is added dynamically, mark it after it is attached to the module
Example fix
# before
fabric_module.mark_forward_method('genearte')
# after
assert callable(getattr(fabric_module._original_module, 'generate', None))
fabric_module.mark_forward_method('generate') Defensive patterns
Strategy: validation
Validate before calling
from types import MethodType
attr = getattr(model._original_module, name, None)
assert isinstance(attr, MethodType), f"{name!r} is not a method on the module" Type guard
from types import MethodType
def has_method(obj: object, name: str) -> bool:
return isinstance(getattr(obj, name, None), MethodType) Prevention
- Guard hasattr/callable checks before marking dynamically obtained names
- Lint method-name strings against dir(model)
When it happens
Trigger: Calling mark_forward_method('genearte') (typo), or marking a method defined on a different object, or marking something that is a plain attribute/tensor rather than a MethodType on _original_module.
Common situations: Typos in the method name; marking a method that exists on the unwrapped original model but the wrapped module's _original_module is a compiled/OptimizedModule whose attribute resolution differs; marking a staticmethod/classmethod or a property; marking before the attribute is attached dynamically.
Related errors
- `setup_optimizers` requires at least one optimizer as input.
- An optimizer should be passed only once to the `setup_optimi
- The optimizer has references to the model's meta-device para
- `setup_dataloaders` requires at least one dataloader as inpu
- A dataloader should be passed only once to the `setup_datalo
AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28).
Data as JSON: /api/errors/9c3846474d7757f7.
Report an issue: GitHub.