code: def sample(
      model: GPT2LMHeadModel,
      tokenizer: GPT2Tokenizer,
      sampler: Sampler,
      n_samples: int,
      n_tokens: int,
      seq_len: int,
      prompt: str,
  ):
      """
      ## Sample from model
  
      :param model: is the model to sample from
      :param tokenizer: is the tokenizer to use
      :param sampler: is the sampler to use
      :param n_samples: is the number of samples to generate
      :param n_tokens: is the number of tokens to generate
      :param seq_len: is the maximum sequence length for the model
      :param prompt: is the starting prompt
      """
      # Tokenize the `prompt` and make `n_samples` copies of it
      data = torch.tile(
          torch.tensor(tokenizer.encode(prompt))[None, :],
          (n_samples, 1),
      )
  
      # Collect output for printing
      logs = [[(prompt, Text.meta)] for _ in range(n_samples)]
      # Sample `n_tokens`
      for i in monit.iterate("Sample", n_tokens):
          # Truncate the data to the maximum sequence length
          data = data[-seq_len:]
          # Get the model output. The 'logits' has shape `[batch_size, seq_len, n_tokens]`
          logits = model(data)[0]
          # Get the `logits` of the last token
          logits = logits[:, -1]
          # Sample from the `logits`
          res = sampler(logits)
          # Add the sampled token to the data
          data = torch.cat([data, res[:, None]], dim=1)
          # Decode and add the sampled token for logging
          for j in range(n_samples):
              logs[j] += [("" + tokenizer.decode(res[j]), Text.value)]
  
      # Print the sampled outputs
      for j in range(n_samples):
          logger.log(logs[j])
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/sampling/experiment.py
imports: import torch
  from labml import monit, logger, lab
  from labml.logger import Text
  from labml_nn.sampling import Sampler
  from labml_nn.sampling.greedy import GreedySampler
  from labml_nn.sampling.nucleus import NucleusSampler
  from labml_nn.sampling.temperature import TemperatureSampler
  from labml_nn.sampling.top_k import TopKSampler
  from transformers import GPT2Tokenizer, GPT2LMHeadModel
label_desc: Use image node_id 1 for calling a global function with example usage: sample(model, tokenizer, sampler, n_samples, n_tokens, seq_len, prompt) without return types