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_states = gpt2decoder.init_states(args.batch_size, ctx)
  
      while True:
          raw_text = input("Model prompt >>> ")
          while not raw_text:
              print("Prompt should not be empty!")
              raw_text = input("Model prompt >>> ")
          context_tokens = tokenizer.encode(raw_text, output_type=int)
          batch_axis = 0 if args.layout == "NT" else 1
          new_shape = (
              (args.batch_size, len(context_tokens))
              if args.layout == "NT"
              else (len(context_tokens), args.batch_size)
          )
          start_input = mx.np.broadcast_to(
              mx.np.expand_dims(
                  mx.np.array(context_tokens, ctx=ctx), batch_axis
              ),
              new_shape,
          )
          generated = 0
          while 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)
          print("=" * 80)
  
file_path: gluon-nlp/scripts/generation/interactive_conditional_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