code: def sample_gpt2(args):
      ctx = mx.gpu(args.gpu) if args.gpu is not None else mx.cpu()
  
      cfg, tokenizer, _, lm_params_path = get_pretrained_gpt2(
          model_name=args.model_name, load_backbone=False, load_lm=True
      )
      cfg.defrost()
      cfg.MODEL.layout = args.layout
      cfg.freeze()
  
      if args.length is None:
          args.length = cfg.MODEL.max_length
      assert (
          args.length <= cfg.MODEL.max_length
      ), "Can't get samples longer than window size: {}".format(
          cfg.MODEL.max_length
      )
  
      model = GPT2ForLM(cfg)
      model.hybridize()
      model.load_parameters(lm_params_path, ctx=ctx)
      gpt2decoder = GPT2Decoder(model)
  
      sampler = BeamSearchSampler(
          beam_size=1,
          decoder=gpt2decoder,
          eos_id=None,
          vocab_size=cfg.MODEL.vocab_size,
          max_length_a=0,
          max_length_b=args.length,
          min_length=1,
          temperature=args.temperature,
          sampling=True,
          sampling_topp=args.top_p,
          sampling_topk=args.top_k,
          early_return=False,
      )
  
      start_input = mx.np.full(
          (args.batch_size, 1)
          if args.layout == "NT"
          else (1, args.batch_size),
          tokenizer.vocab.eos_id,
          ctx=ctx,
      )
      start_states = gpt2decoder.init_states(args.batch_size, ctx)
  
      generated = 0
      while args.nsamples <= 0 or generated < args.nsamples:
          samples, _, _ = sampler(start_input, start_states)
          for i in range(args.batch_size):
              generated += 1
              ids = samples[i][0].asnumpy().tolist()
              ids = ids[1 : ids.index(-1)] if -1 in ids else ids[1:]
              text = tokenizer.decode(ids)
              print(
                  "=" * 40
                  + " SAMPLE "
                  + str(generated)
                  + " "
                  + "=" * 40
              )
              print(text)
  
file_path: gluon-nlp/scripts/generation/generate_unconditional_gpt2_samples.py
imports: import os
  import mxnet
  import argparse
  from gluonnlp.utils import set_seed
  from gluonnlp.sequence_sampler import BeamSearchSampler, BaseStepDecoder
  from gluonnlp.models.gpt2 import GPT2ForLM, list_pretrained_gpt2, get_pretrained_gpt2
label_desc: Use image node_id 2 for calling a global function with example usage: sample_gpt2(args) without return types