class_docstr: BPE Helper for GPT2 Models.
  
  Original source:
      https://github.com/openai/gpt-2/blob/main/src/encoder.py
  
  Original license: MIT
  
  This is a modified implementation from that of fairseq:
      https://github.com/pytorch/fairseq/blob/main/fairseq/data/encoders/gpt2_bpe_utils.py
  
  Fairseq license: MIT
class_docstr_tok: ["BPE","Helper","for","GPT2","Models",".","Original","source",":","https",":","\/\/github.com\/openai\/gpt-2\/blob\/main\/src\/encoder.py","Original","license",":","MIT","This","is","a","modified","implementation","from","that","of","fairseq",":","https",":","\/\/github.com\/pytorch\/fairseq\/blob\/main\/fairseq\/data\/encoders\/gpt2_bpe_utils.py","Fairseq","license",":","MIT"]
docstr: Convert token to BPE.
  
  :param token:
      token to convert
  
  :return bpe_encoding:
      string bpe encoding
docstr_tok: ["Convert","token","to","BPE",".",":","param","token",":","token","to","convert",":","return","bpe_encoding",":","string","bpe","encoding"]
code: def bpe(self, token: str) -> str:
      """
      Convert token to BPE.
  
      :param token:
          token to convert
  
      :return bpe_encoding:
          string bpe encoding
      """
      word = tuple(token)
      pairs = self.get_pairs(word)
  
      if not pairs:
          return token
  
      while True:
          dropped_pairs = self._dropout_pairs(pairs)
          bigram = min(
              dropped_pairs,
              key=lambda pair: self.bpe_ranks.get(pair, float("inf")),
          )
          if bigram not in self.bpe_ranks:
              break
          first, second = bigram
          new_word: List[str] = []
          i = 0
          while i < len(word):
              try:
                  j = word.index(first, i)
                  new_word.extend(word[i:j])
                  i = j
              except Exception:
                  new_word.extend(word[i:])
                  break
  
              if (
                  word[i] == first
                  and i < len(word) - 1
                  and word[i + 1] == second
              ):
                  new_word.append(first + second)
                  i += 2
              else:
                  new_word.append(word[i])
                  i += 1
          word = tuple(new_word)
          if len(word) == 1:
              break
          else:
              pairs = self.get_pairs(word)
      return " ".join(word)
  
imports: from parlai.core.params import ParlaiParser
  from abc import ABC, abstractmethod
  from functools import lru_cache
  import json
  import os
  import random
  import re
  from typing import Dict, List, Optional, Set, Tuple
  from typing_extensions import final
  from parlai.core.build_data import download, make_dir
  from parlai.core.opt import Opt
  from parlai.utils.misc import warn_once
  from parlai.utils.typing import TShared
  from parlai.utils.io import PathManager
  import parlai.utils.logging
label_desc: Use image node_id 7 for calling the Gpt2BpeHelper obj's underlying member method code with example usage: obj.bpe(token) and returns: str, token