@@ -57,13 +57,10 @@ def on_post_move_to_device(self) -> None:
5757
5858
5959class _LightningModuleWrapperBase (DeviceDtypeModuleMixin , torch .nn .Module ):
60- def __init__ (self , pl_module : Union ["pl.LightningModule" , _LightningPrecisionModuleWrapperBase ]):
61- """
62- Wraps the user's LightningModule and redirects the forward call to the appropriate
63- method, either ``training_step``, ``validation_step`` or ``test_step``.
64- If the LightningModule is in none of the states `training`, `testing` or `validation`,
65- the inputs will be redirected to the
66- :meth:`~pytorch_lightning.core.lightning.LightningModule.predict` method.
60+ def __init__ (self , pl_module : Union ["pl.LightningModule" , _LightningPrecisionModuleWrapperBase ]) -> None :
61+ """Wraps the user's LightningModule and redirects the forward call to the appropriate method, either
62+ ``training_step``, ``validation_step``, ``test_step``, or ``predict_step``.
63+
6764 Inheriting classes may also modify the inputs or outputs of forward.
6865
6966 Args:
@@ -77,28 +74,26 @@ def __init__(self, pl_module: Union["pl.LightningModule", _LightningPrecisionMod
7774 self ._ddp_params_and_buffers_to_ignore = [f"module.{ p } " for p in _ddp_params_and_buffers_to_ignore ]
7875
7976 def forward (self , * inputs : Any , ** kwargs : Any ) -> Any :
80- lightning_module = unwrap_lightning_module (self .module )
81- trainer = lightning_module .trainer
82-
83- if trainer and trainer .training :
84- output = self .module .training_step (* inputs , ** kwargs )
85-
86- # In manual_optimization, we need to prevent DDP reducer as
87- # it is done manually in `LightningModule.manual_backward`
88- # `require_backward_grad_sync` will be reset in the
89- # ddp_strategy `post_training_step` hook
90- if not lightning_module .automatic_optimization :
91- trainer .model .require_backward_grad_sync = False
92- elif trainer and trainer .testing :
93- output = self .module .test_step (* inputs , ** kwargs )
94- elif trainer and (trainer .sanity_checking or trainer .validating ):
95- output = self .module .validation_step (* inputs , ** kwargs )
96- elif trainer and trainer .predicting :
97- output = self .module .predict_step (* inputs , ** kwargs )
98- else :
99- output = self .module (* inputs , ** kwargs )
100-
101- return output
77+ pl_module = unwrap_lightning_module (self .module )
78+ trainer = pl_module .trainer
79+
80+ if trainer is not None :
81+ if trainer .training :
82+ output = self .module .training_step (* inputs , ** kwargs )
83+ # In manual_optimization, we need to prevent DDP reducer as
84+ # it is done manually in `LightningModule.manual_backward`
85+ # `require_backward_grad_sync` will be reset in the
86+ # ddp_strategy `post_training_step` hook
87+ if not pl_module .automatic_optimization :
88+ trainer .model .require_backward_grad_sync = False # type: ignore[assignment]
89+ return output
90+ if trainer .testing :
91+ return self .module .test_step (* inputs , ** kwargs )
92+ if trainer .sanity_checking or trainer .validating :
93+ return self .module .validation_step (* inputs , ** kwargs )
94+ if trainer .predicting :
95+ return self .module .predict_step (* inputs , ** kwargs )
96+ return self .module (* inputs , ** kwargs )
10297
10398 def on_post_move_to_device (self ) -> None :
10499 pass
0 commit comments