{"record":{"id":"af391673c4dd180e","repo":"Lightning-AI/pytorch-lightning","slug":"you-are-calling-the-method-type-self-original-m","errorCode":null,"errorMessage":"You are calling the method `{type(self._original_module).__name__}.{name}()` from outside the model. To avoid issues with the currently selected strategy, explicitly mark it as a forward method with `fabric_model.mark_forward_method({name!r})` after `fabric.setup()`.","messagePattern":"You are calling the method `(.+?)\\.(.+?)\\(\\)` from outside the model\\. To avoid issues with the currently selected strategy, explicitly mark it as a forward method with `fabric_model\\.mark_forward_method\\((.+?)\\)` after `fabric\\.setup\\(\\)`\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/lightning/fabric/wrappers.py","lineNumber":219,"sourceCode":"    def _wrap_method_with_module_call_tracker(self, method: Callable, name: str) -> Callable:\n        \"\"\"Tracks whether any submodule in ``self._original_module`` was called during the execution of ``method`` by\n        registering forward hooks on all submodules.\"\"\"\n        module_called = False\n\n        def hook(*_: Any, **__: Any) -> None:\n            nonlocal module_called\n            module_called = True\n\n        @wraps(method)\n        def _wrapped_method(*args: Any, **kwargs: Any) -> Any:\n            handles = []\n            for module in self._original_module.modules():\n                handles.append(module.register_forward_hook(hook))\n\n            output = method(*args, **kwargs)\n\n            if module_called:\n                raise RuntimeError(\n                    f\"You are calling the method `{type(self._original_module).__name__}.{name}()` from outside the\"\n                    \" model. To avoid issues with the currently selected strategy, explicitly mark it as a\"\n                    f\" forward method with `fabric_model.mark_forward_method({name!r})` after `fabric.setup()`.\"\n                )\n            for handle in handles:\n                handle.remove()\n            return output\n\n        return _wrapped_method\n\n    def _register_backward_hook(self, tensor: Tensor) -> Tensor:\n        if not tensor.requires_grad:\n            return tensor\n\n        strategy_requires = is_overridden(\"backward\", self._strategy, parent=Strategy)\n        precision_requires = any(\n            is_overridden(method, self._strategy.precision, parent=Precision)\n            for method in (\"pre_backward\", \"backward\", \"post_backward\")","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/fabric/wrappers.py#L201-L237","documentation":"When a non-forward method of a wrapped FabricModule is called from outside the module, forward hooks are temporarily registered to detect whether the call went through the model's forward (and hence the strategy, e.g. DDP gradient sync). If no forward was triggered, Lightning raises this RuntimeError because calling e.g. model.generate() directly would bypass the strategy and silently break gradient synchronization.","triggerScenarios":"Calling fabric_module.generate(...) (a method not in _forward_methods) directly in your training loop after fabric.setup(), under a strategy like DDP that relies on forward-based hooks/sync.","commonSituations":"Using HF transformers-style model.generate() or custom methods (encode, decode) on a Fabric-wrapped module under DDP/FSDP without marking them first; refactoring a single-GPU script to multi-GPU Fabric; new model methods added after setup.","solutions":["Mark the method right after setup: fabric_module.mark_forward_method('generate')","Or move the logic into the module's forward() so it is routed through the strategy","Or call the method on the unwrapped module only when no gradients/sync are needed (inference-only)"],"exampleFix":"# before\nmodel = fabric.setup(model)\nout = model.generate(input_ids, do_sample=True)  # RuntimeError\n\n# after\nmodel = fabric.setup(model)\nmodel.mark_forward_method('generate')\nout = model.generate(input_ids, do_sample=True)","handlingStrategy":"validation","validationCode":"name = 'generate'\nif name not in getattr(fabric_module, '_forward_methods', set()) and callable(getattr(fabric_module, name, None)):\n    fabric_module.mark_forward_method(name)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Mark every custom callable you invoke from the training loop immediately after fabric.setup()","Keep inference-only calls on the unwrapped module (fabric_module._original_module or fabric.unwrap) when no sync is needed"],"tags":["pytorch-lightning","fabric","ddp","gradient-sync","method-bypass"],"backgroundTag":"ddp-gradient-sync-bypass","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}