class_docstr: Benchmarks is a simple but feature-complete benchmarking script
  to compare memory and time performance of models in Transformers.
code: def _train_speed_memory(
      self, model_name: str, batch_size: int, sequence_length: int
  ) -> Tuple[float, Memory]:
      if self._use_fp16:
          from mxnet import amp
  
          amp.init()
  
      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)
      else:
          model = model_cls.from_cfg(cfg)
      model.load_parameters(backbone_param_path, ctx=ctx)
      model.hybridize(static_alloc=True)
      vocab_size = cfg.MODEL.vocab_size
      if hasattr(cfg.MODEL, "units"):
          out_units = cfg.MODEL.units
      else:
          out_units = cfg.MODEL.DECODER.units
      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
          )
          contextual_embedding_ograd = mxnet.np.random.normal(
              0,
              1,
              (batch_size, sequence_length, out_units),
              dtype=np.float32,
              ctx=ctx,
          )
          pooled_out_ograd = mxnet.np.random.normal(
              0, 1, (batch_size, out_units), dtype=np.float32, 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
          )
          contextual_embedding_ograd = mxnet.np.random.normal(
              0,
              1,
              (sequence_length, batch_size, out_units),
              dtype=np.float32,
              ctx=ctx,
          )
          pooled_out_ograd = mxnet.np.random.normal(
              0, 1, (batch_size, out_units), dtype=np.float32, ctx=ctx
          )
      else:
          raise NotImplementedError
      if model_cls.__name__ in [
          "BertModel",
          "AlbertModel",
          "ElectraModel",
          "MobileBertModel",
      ]:
  
          def train_step():
              with mxnet.autograd.record():
                  contextual_embedding, pooled_out = model(
                      input_ids, token_types, valid_length
                  )
                  # We'd like to set the head gradient of
                  # contextual_embedding to contextual_embedding_ograd
                  # and the head gradient of pooled_out to pooled_out_ograd
                  # Thus, we simply doing two hadamard product and sum up the results.
                  fake_loss = mxnet.np.sum(
                      contextual_embedding * contextual_embedding_ograd
                  ) + mxnet.np.sum(pooled_out * pooled_out_ograd)
                  fake_loss.backward()
              mxnet.npx.waitall()
  
      elif model_cls.__name__ in ["BartModel"]:
  
          def train_step():
              with mxnet.autograd.record():
                  contextual_embedding, pooled_out = model(
                      input_ids, valid_length, input_ids, valid_length
                  )
                  fake_loss = (
                      contextual_embedding * contextual_embedding_ograd
                  ).sum() + (pooled_out * pooled_out_ograd).sum()
                  fake_loss.backward()
              mxnet.npx.waitall()
  
      else:
          raise NotImplementedError
      timeit.repeat(train_step, repeat=1, number=5)
      mxnet.npx.waitall()
      runtimes = timeit.repeat(
          train_step, repeat=self._repeat, number=3
      )
      mxnet.npx.waitall()
      ctx.empty_cache()
      mxnet.npx.waitall()
      # Profile memory
      if self._use_gpu:
          nvml.nvmlInit()
          train_step()
          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(train_step)
          memory = (
              Memory(memory_bytes)
              if isinstance(memory_bytes, int)
              else memory_bytes
          )
      return float(np.min(runtimes) / 3.0), 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 5 for calling the GluonNLPBackboneBenchmark obj's underlying member method code with example usage: obj._train_speed_memory(model_name, batch_size, sequence_length) and returns: float,  memory