docstr: Tests the resume capabilities of exp_manager
docstr_tok: ["Tests","the","resume","capabilities","of","exp_manager"]
code: def test_resume(self, tmp_path):
      """Tests the resume capabilities of exp_manager"""
      test_trainer = pl.Trainer(
          accelerator="cpu", enable_checkpointing=False, logger=False
      )
  
      # Error because explicit_log_dir does not exist
      with pytest.raises(NotFoundError):
          exp_manager(
              test_trainer,
              {
                  "exp_dir": str(tmp_path / "test_resume"),
                  "resume_if_exists": True,
                  "explicit_log_dir": "Does_not_exist",
              },
          )
  
      # Error because checkpoints folder does not exist
      with pytest.raises(NotFoundError):
          exp_manager(
              test_trainer,
              {
                  "resume_if_exists": True,
                  "exp_dir": str(tmp_path / "test_resume"),
              },
          )
  
      # No error because we tell exp_manager to ignore notfounderror
      exp_manager(
          test_trainer,
          {
              "resume_if_exists": True,
              "exp_dir": str(tmp_path / "test_resume_2"),
              "resume_ignore_no_checkpoint": True,
          },
      )
  
      test_trainer = pl.Trainer(
          accelerator="cpu", enable_checkpointing=False, logger=False
      )
      Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
      ).mkdir(parents=True)
      # Error because checkpoints do not exist in folder
      with pytest.raises(NotFoundError):
          exp_manager(
              test_trainer,
              {
                  "resume_if_exists": True,
                  "explicit_log_dir": str(
                      tmp_path / "test_resume" / "default" / "version_0"
                  ),
              },
          )
  
      Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
          / "mymodel--end.ckpt"
      ).touch()
      # Error because *end.ckpt is in folder indicating that training has already finished
      with pytest.raises(ValueError):
          exp_manager(
              test_trainer,
              {
                  "resume_if_exists": True,
                  "explicit_log_dir": str(
                      tmp_path / "test_resume" / "default" / "version_0"
                  ),
              },
          )
  
      Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
          / "mymodel--end.ckpt"
      ).unlink()
      Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
          / "mymodel--last.ckpt"
      ).touch()
      Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
          / "mymodel2--last.ckpt"
      ).touch()
      # Error because multiple *last.ckpt is in folder. If more than one, don't know which to restore
      with pytest.raises(ValueError):
          exp_manager(
              test_trainer,
              {
                  "resume_if_exists": True,
                  "explicit_log_dir": str(
                      tmp_path / "test_resume" / "default" / "version_0"
                  ),
              },
          )
  
      # Finally succeed
      Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
          / "mymodel2--last.ckpt"
      ).unlink()
      log_dir = exp_manager(
          test_trainer,
          {
              "resume_if_exists": True,
              "explicit_log_dir": str(
                  tmp_path / "test_resume" / "default" / "version_0"
              ),
          },
      )
      checkpoint = Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
          / "mymodel--last.ckpt"
      )
      assert (
          Path(test_trainer.ckpt_path).resolve() == checkpoint.resolve()
      )
  
      # Succeed again and make sure that run_0 exists and previous log files were moved
      test_trainer = pl.Trainer(
          accelerator="cpu", enable_checkpointing=False, logger=False
      )
      exp_manager(
          test_trainer,
          {"resume_if_exists": True, "explicit_log_dir": str(log_dir)},
      )
      checkpoint = Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "checkpoints"
          / "mymodel--last.ckpt"
      )
      assert (
          Path(test_trainer.ckpt_path).resolve() == checkpoint.resolve()
      )
      prev_run_dir = Path(
          tmp_path / "test_resume" / "default" / "version_0" / "run_0"
      )
      assert prev_run_dir.exists()
      prev_log = Path(
          tmp_path
          / "test_resume"
          / "default"
          / "version_0"
          / "run_0"
          / "lightning_logs.txt"
      )
      assert prev_log.exists()
  
      # Error becasue `dirpath` specified and has no checkpoint
      test_trainer = pl.Trainer(
          accelerator="cpu", enable_checkpointing=False, logger=False
      )
      dirpath_checkpoint_dir = Path(
          tmp_path / "test_resume" / "dirpath_test" / "ckpts"
      )
      dirpath_checkpoint_dir.mkdir(parents=True)
      with pytest.raises(NotFoundError):
          exp_manager(
              test_trainer,
              {
                  "resume_if_exists": True,
                  "checkpoint_callback_params": {
                      "dirpath": str(dirpath_checkpoint_dir)
                  },
                  "explicit_log_dir": str(log_dir),
              },
          )
  
      # Check that model loads from `dirpath` and not <log_dir>/checkpoints
      dirpath_log_dir = Path(
          tmp_path / "test_resume" / "dirpath_test" / "logs"
      )
      dirpath_log_dir.mkdir(parents=True)
      dirpath_checkpoint = Path(
          dirpath_checkpoint_dir / "mymodel--last.ckpt"
      )
      dirpath_checkpoint.touch()
      exp_manager(
          test_trainer,
          {
              "resume_if_exists": True,
              "checkpoint_callback_params": {
                  "dirpath": str(dirpath_checkpoint_dir)
              },
              "explicit_log_dir": str(dirpath_log_dir),
          },
      )
      assert (
          Path(test_trainer.ckpt_path).resolve()
          == dirpath_checkpoint.resolve()
      )
  
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 6 for calling the TestExpManager obj's underlying member method code with example usage: obj.test_resume(tmp_path) without return types