code: def main():
      parser = argparse.ArgumentParser(
          description="Tool to average the params of input checkpoints to "
          "produce a new checkpoint",
      )
      # fmt: off
      parser.add_argument('--inputs', required=True, nargs='+',
                          help='Input checkpoint file paths.')
      parser.add_argument('--output', required=True, metavar='FILE',
                          help='Write the new checkpoint containing the averaged weights to this path.')
      num_group = parser.add_mutually_exclusive_group()
      num_group.add_argument('--num-epoch-checkpoints', type=int,
                             help='if set, will try to find checkpoints with names checkpoint_xx.pt in the '
                             'path specified by input, and average last this many of them.')
      num_group.add_argument('--num-update-checkpoints', type=int,
                             help='if set, will try to find checkpoints with names checkpoint_ee_xx.pt in the path specified by'
                             ' input, and average last this many of them.')
      num_group.add_argument('--num-best-checkpoints', type=int, default=0,
                             help='if set, will try to find checkpoints with names checkpoint_best_ee_xx.pt in the path specified by'
                             ' input, and average last this many of them.')
      parser.add_argument('--checkpoint-upper-bound', type=int,
                          help='when using --num-epoch-checkpoints, this will set an upper bound on which epoch to use, '
                          'when using --num-update-checkpoints, this will set an upper bound on which update to use'
                          'e.g., with --num-epoch-checkpoints=10 --checkpoint-upper-bound=50, checkpoints 41-50 would be'
                          ' averaged.'
                          'e.g., with --num-update-checkpoints=10 --checkpoint-upper-bound=50000, checkpoints 40500-50000 would'
                          ' be averaged assuming --save-interval-updates 500'
                          )
      # fmt: on
      args = parser.parse_args()
      print(args)
  
      num = None
      is_update_based = False
      if args.num_update_checkpoints is not None:
          num = args.num_update_checkpoints
          is_update_based = True
      elif args.num_epoch_checkpoints is not None:
          num = args.num_epoch_checkpoints
  
      assert args.checkpoint_upper_bound is None or (
          args.num_epoch_checkpoints is not None
          or args.num_update_checkpoints is not None
      ), "--checkpoint-upper-bound requires --num-epoch-checkpoints or --num-update-checkpoints"
      assert (
          args.num_epoch_checkpoints is None
          or args.num_update_checkpoints is None
      ), "Cannot combine --num-epoch-checkpoints and --num-update-checkpoints"
  
      if num is not None:
          args.inputs = last_n_checkpoints(
              args.inputs,
              num,
              is_update_based,
              upper_bound=args.checkpoint_upper_bound,
          )
          print("averaging checkpoints: ", args.inputs)
  
      if args.num_best_checkpoints > 0:
          args.inputs = list(
              sorted(
                  args.inputs,
                  key=lambda x: float(
                      os.path.basename(x)
                      .split("_")[-1]
                      .replace(".pt", "")
                  ),
              )
          )
          args.inputs = args.inputs[: args.num_best_checkpoints]
          for path in args.inputs:
              print(os.path.basename(path))
      new_state = average_checkpoints(args.inputs)
      with PathManager.open(args.output, "wb") as f:
          torch.save(new_state, f)
      print(
          "Finished writing averaged checkpoint to {}".format(
              args.output
          )
      )
  
file_path: fairseq/scripts/average_checkpoints.py
imports: import argparse
  import collections
  import os
  import re
  import torch
  from fairseq.file_io import PathManager
label_desc: Use image node_id 3 for calling a global function with example usage: main() without return types