code: def test_finalize_and_restore_from_it(self):
      with mock_env_with_temp():
          checkpoint = Checkpoint(self.trainer)
          self._init_early_stopping(checkpoint)
          original_model = deepcopy(self.trainer.model)
          self._do_a_pass()
          model_1500 = deepcopy(self.trainer.model)
          checkpoint.save(1500)
  
          swap = self.trainer.model
          self.trainer.model = original_model
          checkpoint.restore()
          # First test without best.ckpt
          self.assertTrue(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  original_model.state_dict(),
              )
          )
          self.assertFalse(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  model_1500.state_dict(),
              )
          )
  
          self.trainer.model = swap
  
          self._do_a_pass()
          model_2000 = deepcopy(self.trainer.model)
          checkpoint.save(2000, update_best=True)
  
          self._do_a_pass()
          model_2500 = deepcopy(self.trainer.model)
          checkpoint.save(2500)
  
          checkpoint.restore()
  
          self.assertFalse(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  original_model.state_dict(),
              )
          )
          self.assertFalse(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  model_1500.state_dict(),
              )
          )
          self.assertTrue(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  model_2000.state_dict(),
              )
          )
          self.assertFalse(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  model_2500.state_dict(),
              )
          )
  
imports: import contextlib
  import os
  import tempfile
  import unittest
  from copy import deepcopy
  from io import StringIO
  from unittest.mock import Mock, patch
  import torch
  from mmf.common.registry import registry
  from mmf.models.base_model import BaseModel
  from mmf.trainers.callbacks.checkpoint import CheckpointCallback
  from mmf.trainers.callbacks.early_stopping import EarlyStoppingCallback
  from mmf.trainers.callbacks.lr_scheduler import LRSchedulerCallback
  from mmf.utils.checkpoint import Checkpoint
  from mmf.utils.configuration import load_yaml
  from mmf.utils.file_io import PathManager
  from omegaconf import OmegaConf
  from tests.test_utils import compare_state_dicts, skip_if_no_cuda
label_desc: Use image node_id 4 for calling the TestUtilsCheckpoint obj's underlying member method code with example usage: obj.test_finalize_and_restore_from_it() without return types