docstr: Test to ensure that when we resume from a checkpoint, we do not re-run validation unnecessarily.
docstr_tok: ["Test","to","ensure","that","when","we","resume","from","a","checkpoint",",","we","do","not","re-run","validation","unnecessarily","."]
code: def test_resume_checkpoint_skip_validation(self, tmp_path):
      """Test to ensure that when we resume from a checkpoint, we do not re-run validation unnecessarily."""
      tmp_path = tmp_path / "test_2"
  
      def run_training(resume_path=None):
          class TestModel(ExampleModel):
              def train_dataloader(self):
                  dataset = OnesDataset(10)
                  return torch.utils.data.DataLoader(
                      dataset, batch_size=1
                  )
  
          class AssertCallback(Callback):
              recorded_validations = 0
              recorded_train_steps = 0
  
              def on_validation_start(
                  self,
                  trainer: "pl.Trainer",
                  pl_module: "pl.LightningModule",
              ) -> None:
                  self.recorded_validations += 1
  
              def on_train_batch_end(
                  self,
                  trainer: "pl.Trainer",
                  pl_module: "pl.LightningModule",
                  outputs,
                  batch: Any,
                  batch_idx: int,
              ) -> None:
                  self.recorded_train_steps += 1
  
              def on_train_end(
                  self,
                  trainer: "pl.Trainer",
                  pl_module: "pl.LightningModule",
              ) -> None:
                  if resume_path is not None:
                      # we should only run validation at the end of training.
                      assert self.recorded_validations == 1
                      # we continue from half way
                      assert (
                          self.recorded_train_steps
                          == len(pl_module.train_dataloader()) // 2
                      )
                  else:
                      # we've run validation within the middle of training and at the end of training.
                      assert self.recorded_validations == 2
                      assert self.recorded_train_steps == len(
                          pl_module.train_dataloader()
                      )
  
          model = TestModel()
          trainer = pl.Trainer(
              accelerator="cpu",
              enable_checkpointing=False,
              logger=False,
              callbacks=[AssertCallback()],
              val_check_interval=0.5,
              num_sanity_val_steps=0,
              max_epochs=1,
          )
          exp_manager(
              trainer,
              {
                  "explicit_log_dir": str(tmp_path),
                  "checkpoint_callback_params": {
                      "filename": f"{{epoch}}-{{step}}"
                  },
              },
          )
          trainer.fit(model, ckpt_path=resume_path)
  
      run_training()
      resume_path = tmp_path / "checkpoints/epoch=0-step=5.ckpt"
      run_training(resume_path)
  
imports: import math
  import os
  import re
  from pathlib import Path
  from typing import Any
  import pytest
  import pytorch_lightning
  import torch
  from omegaconf import OmegaConf
  from omegaconf.errors import OmegaConfBaseException
  from pytorch_lightning import Callback
  from pytorch_lightning.loops import _TrainingEpochLoop
  from nemo.constants import NEMO_ENV_VARNAME_VERSION
  from nemo.core.classes import ModelPT
  from nemo.utils.exp_manager import CheckpointMisconfigurationError, LoggerMisconfigurationError, NotFoundError, exp_manager
label_desc: Use image node_id 13 for calling the TestExpManager obj's underlying member method code with example usage: obj.test_resume_checkpoint_skip_validation(tmp_path) and returns: torch