code: def test_gpt2(model_name, ctx):
      # test from pretrained
      assert len(list_pretrained_gpt2()) > 0
      with tempfile.TemporaryDirectory() as root, ctx:
          (
              cfg,
              tokenizer,
              params_path,
              lm_params_path,
          ) = get_pretrained_gpt2(
              model_name, load_backbone=True, load_lm=True, root=root
          )
          assert cfg.MODEL.vocab_size == len(tokenizer.vocab)
          # test backbone
          gpt2_model = GPT2Model.from_cfg(cfg)
          gpt2_model.load_parameters(params_path)
          # test lm model
          gpt2_lm_model = GPT2ForLM(cfg)
          gpt2_lm_model.load_parameters(lm_params_path)
  
          # test forward
          batch_size = 3
          seq_length = 32
          vocab_size = len(tokenizer.vocab)
          input_ids = mx.np.array(
              np.random.randint(
                  2, vocab_size, (batch_size, seq_length)
              ),
              dtype=np.int32,
              ctx=ctx,
          )
          logits, _ = gpt2_lm_model(
              input_ids, gpt2_lm_model.init_states(batch_size, ctx)
          )
          mx.npx.waitall()
          # test backward
          label_smooth_loss = LabelSmoothCrossEntropyLoss(
              num_labels=vocab_size
          )
          with mx.autograd.record():
              logits, _ = gpt2_lm_model(
                  input_ids, gpt2_lm_model.init_states(batch_size, ctx)
              )
              loss = label_smooth_loss(logits, input_ids)
              loss.backward()
          mx.npx.waitall()
  
file_path: gluon-nlp/tests/test_models_gpt2.py
imports: import pytest
  import numpy
  import mxnet
  import tempfile
  from numpy.testing import assert_allclose
  from gluonnlp.models.gpt2 import GPT2Model, GPT2ForLM, list_pretrained_gpt2, get_pretrained_gpt2
  from gluonnlp.loss import LabelSmoothCrossEntropyLoss
  from gluonnlp.utils.testing import verify_backbone_fp16
label_desc: Use image node_id 4 for calling a global function with example usage: test_gpt2(model_name, ctx) without return types