code: def convert_fairseq_checkpoint(
      checkpoint: Dict[str, Any], key_map: Mapping[str, str]
  ) -> Dict[str, Any]:
      """Convert a fairseq checkpoint to fairseq2.
  
      :param checkpoint:
          The original fairseq checkpoint.
      :param key_map:
          A map of regex patterns to fairseq2 model keys.
  
      :returns:
          A converted checkpoint that is compatible with fairseq2.
      """
      old_state_dict = checkpoint["model"]
  
      new_state_dict = convert_model_state_dict(old_state_dict, key_map)
  
      # We use the built-in version attribute of `torch.nn.Module`.
      try:
          del new_state_dict["encoder.version"]
      except KeyError:
          pass
      try:
          del new_state_dict["decoder.version"]
      except KeyError:
          pass
  
      try:
          del new_state_dict["encoder.embed_positions._float_tensor"]
      except KeyError:
          pass
      try:
          del new_state_dict["decoder.embed_positions._float_tensor"]
      except KeyError:
          pass
  
      return {"model": new_state_dict}
  
file_path: fairseq2/src/fairseq2/models/utils/checkpoint.py
imports: import re
  import warnings
  from typing import Any, Callable, Dict, Mapping, Optional, Protocol, Union
  import torch
  from torch import Tensor
  from typing_extensions import TypeAlias
  from fairseq2.data.typing import PathLike
  from fairseq2.typing import Device
  from fairseq2.utils.version import _is_pt21_or_greater
label_desc: Use image node_id 3 for calling a global function with example usage: convert_fairseq_checkpoint(checkpoint, key_map) and returns: dict