Lightning-AI/pytorch-lightning · error · ValueError

You cannot mark the forward method itself as a forward metho

Error message

You cannot mark the forward method itself as a forward method.

What it means

mark_forward_method() exists to redirect non-standard methods (like generate) through the strategy's forward path. Marking 'forward' itself is rejected with ValueError because forward is already routed through the wrapper and self-redirection would recurse infinitely.

Source

Thrown at src/lightning/fabric/wrappers.py:171

        return self._original_module.state_dict(
            destination=destination,  # type: ignore[type-var]
            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)

View on GitHub (pinned to 9fed5c27d2)

Solutions

  1. Remove 'forward' from the list of methods you mark; it is handled automatically
  2. Only mark auxiliary callable methods such as 'generate', 'encode', 'decode' that you invoke directly
  3. If you meant a differently named method, fix the string typo

Example fix

# before
for name in ['forward', 'generate']:
    fabric_module.mark_forward_method(name)

# after
for name in ['generate']:
    fabric_module.mark_forward_method(name)
Defensive patterns

Strategy: validation

Validate before calling

assert name != 'forward', "forward must not be marked; it is already routed through the strategy"

Prevention

When it happens

Trigger: Calling fabric_module.mark_forward_method('forward') or fabric_module.mark_forward_method(fabric_module.forward) after fabric.setup().

Common situations: Generic code that loops over dir(model) or a list of method names including 'forward' and marks them all; copy-pasted snippet adapted from a generate() example without changing the name; misunderstanding that forward needs no marking.

Related errors


AI-assisted analysis of Lightning-AI/pytorch-lightning@9fed5c27d2 (2026-08-28). Data as JSON: /api/errors/2876d4c47b28c3a5. Report an issue: GitHub.