class_docstr: Benchmarks is a simple but feature-complete benchmarking script
  to compare memory and time performance of models in Transformers.
code: def _inference_speed_memory(
      self, model_name: str, batch_size: int, sequence_length: int
  ) -> Tuple[float, Memory]:
      if self._use_fp16:
          os.environ["export MXNET_FC_TRUE_FP16"] = "1"
          dtype = "float16"
      else:
          dtype = "float32"
      if self._use_gpu:
          ctx = mxnet.gpu()
      else:
          ctx = mxnet.cpu()
      model_cls, cfg, tokenizer, backbone_param_path, _ = get_backbone(
          model_name
      )
      cfg.defrost()
      cfg.MODEL.layout = self._layout
      if model_cls.__name__ not in ["BartModel"]:
          cfg.MODEL.compute_layout = self._compute_layout
      cfg.freeze()
      if model_cls.__name__ in ["BartModel"]:
          model = model_cls.from_cfg(
              cfg, extract_feature=True, dtype=dtype
          )
      else:
          model = model_cls.from_cfg(cfg, dtype=dtype)
      model.load_parameters(
          backbone_param_path, ctx=ctx, cast_dtype=True
      )
      model.cast(dtype)
      model.hybridize(static_alloc=True, static_shape=True)
      vocab_size = cfg.MODEL.vocab_size
      if self._layout == "NT":
          input_ids = mxnet.np.random.randint(
              0,
              vocab_size,
              (batch_size, sequence_length),
              dtype=np.int32,
              ctx=ctx,
          )
          token_types = mxnet.np.zeros(
              (batch_size, sequence_length), dtype=np.int32, ctx=ctx
          )
          valid_length = mxnet.np.full(
              (batch_size,), sequence_length, dtype=np.int32, ctx=ctx
          )
      elif self._layout == "TN":
          input_ids = mxnet.np.random.randint(
              0,
              vocab_size,
              (sequence_length, batch_size),
              dtype=np.int32,
              ctx=ctx,
          )
          token_types = mxnet.np.zeros(
              (sequence_length, batch_size), dtype=np.int32, ctx=ctx
          )
          valid_length = mxnet.np.full(
              (batch_size,), sequence_length, dtype=np.int32, ctx=ctx
          )
      else:
          raise NotImplementedError
      mxnet.npx.waitall()
  
      def run_forward():
          if "roberta" in model_name or "xlmr" in model_name:
              out = model(input_ids, valid_length)
          elif "bart" in model_name:
              out = model(
                  input_ids, valid_length, input_ids, valid_length
              )
          else:
              out = model(input_ids, token_types, valid_length)
          if isinstance(out, list):
              for ele in out:
                  ele.wait_to_read()
          else:
              out.wait_to_read()
  
      if self._use_tvm:
          tvm = try_import_tvm()
          run_forward()
          if self._use_gpu:
              ctx = tvm.gpu()
          else:
              ctx = tvm.cpu()
          rt = compile_tvm_graph_executor(
              model=model,
              model_name=model_name,
              layout=self._layout,
              compute_layout=self._compute_layout,
              batch_size=batch_size,
              seq_length=sequence_length,
              instance_type=self._instance_type,
              dtype="float32" if not self._use_fp16 else "float16",
          )
          tvm_input_ids = tvm.nd.array(input_ids.asnumpy(), ctx=ctx)
          tvm_token_types = tvm.nd.array(token_types.asnumpy(), ctx=ctx)
          tvm_valid_length = tvm.nd.array(
              valid_length.asnumpy(), ctx=ctx
          )
  
          if "roberta" in model_name or "xlmr" in model_name:
              rt.set_input(data0=tvm_input_ids, data1=tvm_valid_length)
          elif "bart" in model_name:
              rt.set_input(data0=tvm_input_ids, data1=tvm_valid_length)
          else:
              rt.set_input(
                  data0=tvm_input_ids,
                  data1=tvm_token_types,
                  data2=tvm_valid_length,
              )
          # ftimer returns a ProfileResult
          ftimer = rt.module.time_evaluator(
              "run", ctx, number=3, repeat=self._repeat
          )
          runtimes = np.min(ftimer().results)
      else:
          timeit.repeat(run_forward, repeat=1, number=3)
          runtimes = float(
              np.min(
                  timeit.repeat(
                      run_forward, repeat=self._repeat, number=3
                  )
              )
              / 3.0
          )
          mxnet.npx.waitall()
      # Profile memory
      if self._use_gpu:
          nvml.nvmlInit()
          run_forward()
          mxnet.npx.waitall()
          handle = nvml.nvmlDeviceGetHandleByIndex(self._device_idx)
          meminfo = nvml.nvmlDeviceGetMemoryInfo(handle)
          max_bytes_in_use = meminfo.used
          memory = Memory(max_bytes_in_use)
          # shutdown nvml
          nvml.nvmlShutdown()
      else:
          # cpu
          memory_bytes = measure_peak_memory_cpu(run_forward)
          memory = (
              Memory(memory_bytes)
              if isinstance(memory_bytes, int)
              else memory_bytes
          )
      return runtimes, memory
  
imports: import copy
  import csv
  import linecache
  import logging
  import os
  import platform
  import sys
  import timeit
  import numpy
  import gluonnlp
  from gluonnlp.models import get_backbone
  from gluonnlp.utils.misc import logging_config
  from gluonnlp.utils.lazy_imports import try_import_tvm
  from collections import defaultdict, namedtuple
  from datetime import datetime
  import multiprocessing
  from multiprocessing import Pipe, Process, Queue
  from multiprocessing.connection import Connection
  from typing import Callable, Iterable, List, NamedTuple, Optional, Union, Tuple
label_desc: Use image node_id 4 for calling the GluonNLPBackboneBenchmark obj's underlying member method code with example usage: obj._inference_speed_memory(model_name, batch_size, sequence_length) and returns: runtimes,  memory