Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated Fabric trainer example to not call self.trainer.model during validation #19993

Merged
merged 6 commits into from
Jun 21, 2024
16 changes: 15 additions & 1 deletion examples/fabric/build_your_own_trainer/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ def fit(
assert optimizer is not None
model, optimizer = self.fabric.setup(model, optimizer)

if not is_overridden("on_validation_model_eval", _unwrap_objects(model)):
awaelchli marked this conversation as resolved.
Show resolved Hide resolved

def ovme(s) -> None:
s.eval()

model.on_validation_model_eval = MethodType(ovme, model)

if not is_overridden("on_validation_model_train", _unwrap_objects(model)):

def ovmt(s) -> None:
s.train()

model.on_validation_model_train = MethodType(ovmt, model)

# assemble state (current epoch and global step will be added in save)
state = {"model": model, "optim": optimizer, "scheduler": scheduler_cfg}

Expand Down Expand Up @@ -264,7 +278,7 @@ def val_loop(
val_loader: Optional[torch.utils.data.DataLoader],
limit_batches: Union[int, float] = float("inf"),
):
"""The validation loop ruunning a single validation epoch.
"""The validation loop running a single validation epoch.

Args:
model: the LightningModule to evaluate
Expand Down
Loading