code: def load_existing_checkpoint(
      checkpoint_path, trainer, restore_state=True
  ) -> Tuple[bool, Optional[Dict]]:
      loaded = False
      extra_state = None
  
      if not PathManager.isfile(checkpoint_path):
          print(
              f"| No existing checkpoint at {checkpoint_path}. "
              f"Starting training from scratch."
          )
          return loaded, extra_state
  
      if restore_state:
          extra_state = trainer.load_checkpoint(checkpoint_path)
          if extra_state is None:
              loaded = False
              print(
                  f"| Failed to load checkpoint and state from {checkpoint_path}."
              )
          else:
              loaded = True
              print(
                  f"| Loaded checkpoint {checkpoint_path} "
                  f"(epoch {extra_state['epoch']}) with restored extra state."
              )
              # batch_offset being None denotes this was a checkpoint saved at
              # the end of an epoch (after the last batch).
              if extra_state["batch_offset"] is None:
                  trainer.lr_step(extra_state["epoch"])
                  extra_state["epoch"] += 1
                  extra_state["batch_offset"] = 0
  
      else:
          dummy_state = trainer.load_checkpoint(
              checkpoint_path, reset_optimizer=True
          )
          if dummy_state is None:
              loaded = False
              print(
                  f"| Failed to load checkpoint weights from {checkpoint_path}."
              )
          else:
              loaded = True
              print(
                  f"| Loaded checkpoint weights from {checkpoint_path}."
              )
  
      return loaded, extra_state
  
imports: import os
  from collections import deque, OrderedDict
  from typing import Any, Deque, Dict, List, Optional, Tuple
  import torch
  from fairseq import checkpoint_utils, utils
  from pytorch_translate import constants
  from pytorch_translate.file_io import PathManager
label_desc: Use image node_id 2 for calling a global function with example usage: load_existing_checkpoint(checkpoint_path, trainer, restore_state) and returns: loaded,  extra_state, loaded,  extra_state