code: def test_save_and_load_state_dict(self):
      with mock_env_with_temp() as d:
          checkpoint = Checkpoint(self.trainer)
          self._init_early_stopping(checkpoint)
          self._do_a_pass()
          # Test normal case
          checkpoint.save(1500)
  
          self.assertTrue(
              PathManager.exists(
                  os.path.join(d, "models", "model_1500.ckpt")
              )
          )
          self.assertTrue(
              PathManager.exists(os.path.join(d, "current.ckpt"))
          )
          self.assertFalse(
              PathManager.exists(os.path.join(d, "best.ckpt"))
          )
          os.remove(os.path.join(d, "models", "model_1500.ckpt"))
          os.remove(os.path.join(d, "current.ckpt"))
  
          best_model = deepcopy(self.trainer.model)
          best_optimizer = deepcopy(self.trainer.optimizer)
          # Test with update_best
          checkpoint.save(2000, update_best=True)
  
          self.assertTrue(
              PathManager.exists(
                  os.path.join(d, "models", "model_2000.ckpt")
              )
          )
          self.assertTrue(
              PathManager.exists(os.path.join(d, "best.ckpt"))
          )
          self.assertTrue(
              PathManager.exists(os.path.join(d, "current.ckpt"))
          )
  
          self._do_a_pass()
          checkpoint.save(2500)
  
          # Test resume
          self.trainer.config.checkpoint.resume = True
  
          current_model = deepcopy(self.trainer.model)
          current_optimizer = deepcopy(self.trainer.optimizer)
          checkpoint.load_state_dict()
  
          self.assertFalse(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  best_model.state_dict(),
              )
          )
          self.assertTrue(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  current_model.state_dict(),
              )
          )
          self.assertFalse(
              self._compare_optimizers(
                  self.trainer.optimizer, best_optimizer
              )
          )
          self.assertTrue(
              self._compare_optimizers(
                  self.trainer.optimizer, current_optimizer
              )
          )
  
          base_0_weight_current = self.trainer.model.base[
              0
          ].weight.data.clone()
  
          # Test resume_best
          self.trainer.config.checkpoint.resume = True
          self.trainer.config.checkpoint.resume_best = True
  
          checkpoint.load_state_dict()
  
          self.assertTrue(
              compare_state_dicts(
                  self.trainer.model.state_dict(),
                  best_model.state_dict(),
              )
          )
          self.assertTrue(
              self._compare_optimizers(
                  self.trainer.optimizer, best_optimizer
              )
          )
          self.assertFalse(
              self._compare_optimizers(
                  self.trainer.optimizer, current_optimizer
              )
          )
          base_0_weight_best = self.trainer.model.base[
              0
          ].weight.data.clone()
  
          self.trainer.config.checkpoint.resume_best = False
          # Test distributed settings
          self.trainer.model = torch.nn.DataParallel(self.trainer.model)
          checkpoint.load_state_dict()
  
          weight_to_be_tested = self.trainer.model.module.base[0].weight
          weight_device = weight_to_be_tested.device
  
          self.assertTrue(
              torch.equal(
                  weight_to_be_tested,
                  base_0_weight_current.to(weight_device),
              )
          )
          self.assertFalse(
              torch.equal(
                  weight_to_be_tested,
                  base_0_weight_best.to(weight_device),
              )
          )
  
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 3 for calling the TestUtilsCheckpoint obj's underlying member method code with example usage: obj.test_save_and_load_state_dict() without return types