code: def test_fit_loop_reset(tmpdir):
      """Test that the reset logic in fit- and epoch loop is aware of whether the loop is restarting from a completed
      loop or from a mid-epoch checkpoint."""
  
      # generate checkpoints at end of epoch and mid-epoch
      model = BoringModel()
      checkpoint_callback = ModelCheckpoint(
          dirpath=tmpdir,
          every_n_train_steps=2,
          save_top_k=-1,
      )
      trainer = Trainer(
          default_root_dir=tmpdir,
          limit_train_batches=4,
          max_epochs=2,
          callbacks=[checkpoint_callback],
          logger=False,
          enable_model_summary=False,
      )
      trainer.fit(model)
  
      # reset state loaded from a checkpoint from mid-epoch
      mid_epoch_ckpt = torch.load(str(tmpdir / "epoch=0-step=2.ckpt"))
      fit_loop = trainer.fit_loop
      epoch_loop = fit_loop.epoch_loop
      optimizer_loop = epoch_loop.automatic_optimization
      assert not fit_loop.restarting
      assert not epoch_loop.restarting
      assert not optimizer_loop.restarting
  
      # we load exactly what was saved - no reset yet
      fit_loop.load_state_dict(mid_epoch_ckpt["loops"]["fit_loop"])
      # resetting from a mid-of-epoch checkpoint SHOULD NOT reset the current counters to 0
      fit_loop.reset()
      epoch_loop.reset()
  
      assert fit_loop.restarting
      assert fit_loop.epoch_progress.total.ready == 1
      assert (
          fit_loop.epoch_progress.total.completed == 0
      )  # the checkpoint was saved mid epoch
      assert fit_loop.epoch_progress.current.ready == 0
      assert fit_loop.epoch_progress.current.completed == 0
  
      assert epoch_loop.restarting
      assert epoch_loop.batch_progress.total.ready == 2
      assert epoch_loop.batch_progress.total.processed == 2
      assert (
          epoch_loop.batch_progress.total.completed == 1
      )  # the checkpoint was saved on train_batch_end
      assert (
          epoch_loop.batch_progress.current.ready == 1
      )  # currents get set to the completed value
      assert epoch_loop.batch_progress.current.processed == 1
      assert epoch_loop.batch_progress.current.completed == 1
  
      assert optimizer_loop.restarting
  
      # reset state loaded from a checkpoint from the end of an epoch
      end_of_epoch_ckpt = torch.load(
          str(tmpdir / "epoch=0-step=4.ckpt")
      )
      fit_loop = trainer.fit_loop
      epoch_loop = fit_loop.epoch_loop
      fit_loop.restarting = False
      epoch_loop.restarting = False
      optimizer_loop.restarting = False
  
      # we load exactly what was saved - no reset yet
      fit_loop.load_state_dict(end_of_epoch_ckpt["loops"]["fit_loop"])
      # resetting from a end-of-epoch checkpoint SHOULD reset the current counters to 0
      fit_loop.reset()
      epoch_loop.reset()
  
      assert fit_loop.restarting
      assert fit_loop.epoch_progress.total.ready == 1
      assert (
          fit_loop.epoch_progress.total.completed == 0
      )  # the checkpoint saves before the epoch completes
      assert fit_loop.epoch_progress.current.ready == 0
      assert fit_loop.epoch_progress.current.completed == 0
  
      assert epoch_loop.restarting
      assert epoch_loop.batch_progress.total.ready == 4
      assert epoch_loop.batch_progress.total.processed == 4
      assert (
          epoch_loop.batch_progress.total.completed == 3
      )  # the checkpoint was saved on train_batch_end
      assert (
          epoch_loop.batch_progress.current.ready == 3
      )  # currents get set to the completed value
      assert epoch_loop.batch_progress.current.processed == 3
      assert epoch_loop.batch_progress.current.completed == 3
  
imports: import os
  from copy import deepcopy
  from dataclasses import dataclass
  from typing import Dict, Iterator
  from unittest.mock import ANY, Mock
  import pytest
  import torch
  from lightning.pytorch import LightningModule, Trainer
  from lightning.pytorch.callbacks import Callback, ModelCheckpoint, OnExceptionCheckpoint
  from lightning.pytorch.demos.boring_classes import BoringModel, RandomDataset
  from lightning.pytorch.loops import _Loop
  from lightning.pytorch.loops.progress import _BaseProgress
  from torch.utils.data.dataloader import DataLoader, _MultiProcessingDataLoaderIter
  from tests_pytorch.helpers.runif import RunIf
label_desc: Use image node_id 7 for calling a global function with example usage: test_fit_loop_reset(tmpdir) without return types