{"record":{"id":"9c3846474d7757f7","repo":"Lightning-AI/pytorch-lightning","slug":"you-marked-name-as-a-forward-method-but-typ","errorCode":null,"errorMessage":"You marked '{name}' as a forward method, but `{type(self._original_module).__name__}.{name}` does not exist or is not a method.","messagePattern":"You marked '(.+?)' as a forward method, but `(.+?)\\.(.+?)` does not exist or is not a method\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/wrappers.py","lineNumber":173,"sourceCode":"            prefix=prefix,\n            keep_vars=keep_vars,\n        )\n\n    @override\n    def load_state_dict(  # type: ignore[override]\n        self, state_dict: Mapping[str, Any], strict: bool = True, **kwargs: Any\n    ) -> _IncompatibleKeys:\n        return self._original_module.load_state_dict(state_dict=state_dict, strict=strict, **kwargs)\n\n    def mark_forward_method(self, method: Union[MethodType, str]) -> None:\n        \"\"\"Mark a method as a 'forward' method to prevent it bypassing the strategy wrapper (e.g., DDP).\"\"\"\n        if not isinstance(method, (MethodType, str)):\n            raise TypeError(f\"Expected a method or a string, but got: {type(method).__name__}\")\n        name = method if isinstance(method, str) else method.__name__\n        if name == \"forward\":\n            raise ValueError(\"You cannot mark the forward method itself as a forward method.\")\n        if not isinstance(getattr(self._original_module, name, None), MethodType):\n            raise AttributeError(\n                f\"You marked '{name}' as a forward method, but `{type(self._original_module).__name__}.{name}` does not\"\n                f\" exist or is not a method.\"\n            )\n        self._forward_methods.add(name)\n\n    def _redirection_through_forward(self, method_name: str) -> Callable:\n        assert method_name != \"forward\"\n        original_forward = self._original_module.forward\n\n        def wrapped_forward(*args: Any, **kwargs: Any) -> Any:\n            # Unpatch ourselves immediately before calling the method `method_name`\n            # because itself may want to call the real `forward`\n            self._original_module.forward = original_forward\n            # Call the actual method e.g. `.training_step(...)`\n            method = getattr(self._original_module, method_name)\n            return method(*args, **kwargs)\n\n        # We make the caller \"unknowingly\" send their arguments through the forward_module's `__call__`.","sourceCodeStart":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/wrappers.py#L155-L191","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"# before\nfabric_module.mark_forward_method('genearte')\n\n# after\nassert callable(getattr(fabric_module._original_module, 'generate', None))\nfabric_module.mark_forward_method('generate')","handlingStrategy":"validation","validationCode":"from types import MethodType\nattr = getattr(model._original_module, name, None)\nassert isinstance(attr, MethodType), f\"{name!r} is not a method on the module\"","typeGuard":"from types import MethodType\n\ndef has_method(obj: object, name: str) -> bool:\n    return isinstance(getattr(obj, name, None), MethodType)","tryCatchPattern":null,"preventionTips":["Guard hasattr/callable checks before marking dynamically obtained names","Lint method-name strings against dir(model)"],"tags":["pytorch-lightning","fabric","attribute-error","method-lookup"],"backgroundTag":"attribute-not-found","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}