code: def main():
      parser = HfArgumentParser(PyTorchBenchmarkArguments)
      # Benchmark Training
      for use_fp16 in [False, True]:
          df = pd.DataFrame(
              columns=[
                  "model",
                  "batch_size",
                  "sequence_length",
                  "latency",
                  "memory",
              ]
          )
          for model in HF_MODELS:
              for batch_size, seq_length in train_workloads:
                  prefix = "{}_{}_{}".format(
                      model, batch_size, seq_length
                  ).replace("/", "_")
                  args = [
                      "--models",
                      model,
                      "--batch_sizes",
                      "{}".format(batch_size),
                      "--sequence_lengths",
                      "{}".format(seq_length),
                      "--train_time_csv_file",
                      "{}.train_time.csv".format(prefix),
                      "--train_memory_csv_file",
                      "{}.train_memory.csv".format(prefix),
                      "--repeat",
                      "3",
                      "--save_to_csv",
                      "--training",
                      "--no_inference",
                      "--no_multi_process",
                  ]
                  if use_fp16:
                      args.append("--fp16")
                  benchmark_args = parser.parse_args_into_dataclasses(
                      args
                  )[0]
                  benchmark = CustomizedPyTorchBenchmark(
                      args=benchmark_args
                  )
                  p = Process(target=benchmark.run)
                  p.start()
                  p.join()
  
                  try:
                      train_time_df = pd.read_csv(
                          "{}.train_time.csv".format(prefix)
                      )
                      train_memory_df = pd.read_csv(
                          "{}.train_memory.csv".format(prefix)
                      )
                      latency = train_time_df["result"][0]
                      memory = train_memory_df["result"][0]
                      os.remove("{}.train_time.csv".format(prefix))
                      os.remove("{}.train_memory.csv".format(prefix))
                  except Exception:
                      latency = math.nan
                      memory = math.nan
                  new_df = pd.DataFrame(
                      {
                          "model": [model],
                          "batch_size": [batch_size],
                          "sequence_length": [seq_length],
                          "latency": [latency],
                          "memory": [memory],
                      }
                  )
                  print(new_df)
                  df = df.append(new_df, ignore_index=True)
                  if use_fp16:
                      df.to_csv("pytorch_train_fp16.csv")
                  else:
                      df.to_csv("pytorch_train_fp32.csv")
  
      # Benchmark Inference
      for torch_script in [False, True]:
          for use_fp16 in [False, True]:
              if torch_script and use_fp16:
                  # Cannot support both torch_script and use_fp16.
                  continue
              df = pd.DataFrame(
                  columns=[
                      "model",
                      "batch_size",
                      "sequence_length",
                      "latency",
                      "memory",
                  ]
              )
              for model in HF_MODELS:
                  for batch_size, seq_length in inference_workloads:
                      prefix = "{}_{}_{}".format(
                          model, batch_size, seq_length
                      ).replace("/", "_")
                      args = [
                          "--models",
                          model,
                          "--batch_sizes",
                          "{}".format(batch_size),
                          "--sequence_lengths",
                          "{}".format(seq_length),
                          "--inference_time_csv_file",
                          "{}.inference_time.csv".format(prefix),
                          "--inference_memory_csv_file",
                          "{}.inference_memory.csv".format(prefix),
                          "--repeat",
                          "3",
                          "--save_to_csv",
                          "--no_multi_process",
                      ]
                      if use_fp16:
                          args.append("--fp16")
                      if torch_script:
                          args.append("--torchscript")
                      benchmark_args = (
                          parser.parse_args_into_dataclasses(args)[0]
                      )
                      benchmark = CustomizedPyTorchBenchmark(
                          args=benchmark_args
                      )
                      p = Process(target=benchmark.run)
                      p.start()
                      p.join()
  
                      try:
                          inference_time_df = pd.read_csv(
                              "{}.inference_time.csv".format(prefix)
                          )
                          inference_memory_df = pd.read_csv(
                              "{}.inference_memory.csv".format(prefix)
                          )
                          latency = inference_time_df["result"][0]
                          memory = inference_memory_df["result"][0]
                          os.remove(
                              "{}.inference_time.csv".format(prefix)
                          )
                          os.remove(
                              "{}.inference_memory.csv".format(prefix)
                          )
                      except Exception:
                          latency = math.nan
                          memory = math.nan
                      new_df = pd.DataFrame(
                          {
                              "model": [model],
                              "batch_size": [batch_size],
                              "sequence_length": [seq_length],
                              "latency": [latency],
                              "memory": [memory],
                          }
                      )
                      print(new_df)
                      df = df.append(new_df, ignore_index=True)
                      if use_fp16 and torch_script:
                          df.to_csv("pytorch_infer_fp16_ts.csv")
                      elif use_fp16 and not torch_script:
                          df.to_csv("pytorch_infer_fp16.csv")
                      elif not use_fp16 and torch_script:
                          df.to_csv("pytorch_infer_fp32_ts.csv")
                      else:
                          df.to_csv("pytorch_infer_fp32.csv")
  
file_path: gluon-nlp/docs/model_zoo/benchmarks/benchmark_hf.py
imports: import argparse
  import pandas
  import math
  import os
  import multiprocessing
  from multiprocessing import Process
  import torch
  from typing import Callable
  from transformers import HfArgumentParser
  from transformers.benchmark.benchmark import PyTorchBenchmark, PyTorchBenchmarkArguments
  import logging
  import timeit
label_desc: Use image node_id 1 for calling a global function with example usage: main() without return types