class_docstr: Wrapper for a Huggingface Transformers GPT-2 model.
  
  This class loads a tokenizer and model using the Huggingface library and
  provides the LIT-required functions plus additional helper functions to
  convert and clean tokens and to compute the top_k predictions from logits.
class_docstr_tok: ["Wrapper","for","a","Huggingface","Transformers","GPT-2","model",".","This","class","loads","a","tokenizer","and","model","using","the","Huggingface","library","and","provides","the","LIT-required","functions","plus","additional","helper","functions","to","convert","and","clean","tokens","and","to","compute","the","top_k","predictions","from","logits","."]
docstr: Post-process single-example preds. Operates on numpy arrays.
docstr_tok: ["Post-process","single-example","preds",".","Operates","on","numpy","arrays","."]
code: def _postprocess(self, preds):
      """Post-process single-example preds. Operates on numpy arrays."""
      ntok = preds.pop("ntok")
      ids = preds.pop("input_ids")[:ntok]
      preds["tokens"] = self._detokenize(ids)
  
      # Decode predicted top-k tokens.
      # token_topk_preds will be a list[list[(word, prob)]]
      # Initialize prediction for 0th token as N/A.
      token_topk_preds = [[("N/A", 1.0)]]
      pred_ids = preds.pop("top_k_indices")[
          :ntok
      ]  # <int>[num_tokens, k]
      pred_probs = preds.pop("top_k_probs")[
          :ntok
      ]  # <float32>[num_tokens, k]
      for token_pred_ids, token_pred_probs in zip(pred_ids, pred_probs):
          token_pred_words = self._detokenize(token_pred_ids)
          token_topk_preds.append(
              list(zip(token_pred_words, token_pred_probs))
          )
      preds["pred_tokens"] = token_topk_preds
  
      # Process attention.
      for key in preds:
          if not re.match(r"layer_(\d+)/attention", key):
              continue
          # Select only real tokens, since most of this matrix is padding.
          # <float32>[num_heads, max_seq_length, max_seq_length]
          # -> <float32>[num_heads, num_tokens, num_tokens]
          preds[key] = preds[key][:, :ntok, :ntok].transpose((0, 2, 1))
          # Make a copy of this array to avoid memory leaks, since NumPy otherwise
          # keeps a pointer around that prevents the source array from being GCed.
          preds[key] = preds[key].copy()
  
      return preds
  
imports: import re
  from lit_nlp.api import model
  from lit_nlp.api import types
  from lit_nlp.examples.models import model_utils
  from lit_nlp.lib import file_cache
  from lit_nlp.lib import utils
  import numpy
  import tensorflow
  import transformers
label_desc: Use image node_id 7 for calling the GPT2LanguageModel obj's underlying member method code with example usage: obj._postprocess(preds) and returns: preds