code: def benchmark(args):
      assert args.source_vocab_file and os.path.isfile(
          args.source_vocab_file
      ), "Please specify a valid file for --source-vocab-file"
      assert args.target_vocab_file and os.path.isfile(
          args.target_vocab_file
      ), "Please specify a valid file for --target-vocab_file"
      assert args.path is not None, "--path required for generation!"
  
      print(args)
  
      # Benchmarking should be language-agnostic
      args.source_lang = "src"
      args.target_lang = "tgt"
  
      (
          models,
          model_args,
          task,
      ) = pytorch_translate_utils.load_diverse_ensemble_for_inference(
          args.path.split(CHECKPOINT_PATHS_DELIMITER)
      )
  
      append_eos_to_source = model_args[0].append_eos_to_source
      reverse_source = model_args[0].reverse_source
      assert all(
          a.append_eos_to_source == append_eos_to_source
          and a.reverse_source == reverse_source
          for a in model_args
      )
  
      def benchmark_length(n):
          # Generate synthetic raw text files
          source_text_file = generate_synthetic_text(
              dialect=args.source_lang,
              dialect_symbols=task.source_dictionary.symbols,
              length=n,
              examples=args.examples_per_length,
          )
          target_text_file = generate_synthetic_text(
              dialect=args.target_lang,
              dialect_symbols=task.target_dictionary.symbols,
              length=n,
              examples=args.examples_per_length,
          )
  
          task.load_dataset_from_text(
              args.gen_subset,
              source_text_file=source_text_file,
              target_text_file=target_text_file,
              append_eos=append_eos_to_source,
              reverse_source=reverse_source,
          )
  
          # Remove temporary text files
          os.remove(source_text_file)
          os.remove(target_text_file)
  
          # priming
          (
              scorer,
              num_sentences,
              gen_timer,
              _,
          ) = pytorch_translate_generate.generate_score(
              models=models,
              args=args,
              task=task,
              dataset=task.dataset(args.gen_subset),
          )
  
          total_time = 0.0
          for _ in range(args.runs_per_length):
              (
                  scorer,
                  num_sentences,
                  gen_timer,
                  _,
              ) = pytorch_translate_generate.generate_score(
                  models=models,
                  args=args,
                  task=task,
                  dataset=task.dataset(args.gen_subset),
              )
              total_time += gen_timer.sum
              gen_timer.reset()
  
          sentences_per_run = args.examples_per_length
          runs = args.runs_per_length
          total_sentences = sentences_per_run * runs
          total_tokens = total_sentences * n
  
          print(f"--- {n} tokens ---")
          print(
              f"Generated {total_tokens} tokens ({runs} runs of {sentences_per_run})"
          )
          print(f"Total time: {total_time:.3f} seconds")
          time_per_sentence = total_time / total_sentences
          print(f"Time per sentence: {time_per_sentence:.3f} seconds\n")
  
      benchmark_length(6)
      benchmark_length(10)
      benchmark_length(20)
  
file_path: translate/pytorch_translate/benchmark.py
imports: import os
  import random
  import tempfile
  from fairseq import options, tasks
  from pytorch_translate import generate, options, rnn, utils
  from pytorch_translate.constants import CHECKPOINT_PATHS_DELIMITER
label_desc: Use image node_id 4 for calling a global function with example usage: benchmark(args) without return types