class_docstr: Construct a GPT-2 tokenizer. Based on byte-level Byte-Pair-Encoding.
  
  This tokenizer has been trained to treat spaces like parts of the tokens (a bit like sentencepiece) so a word will
  be encoded differently whether it is at the beginning of the sentence (without space) or not:
  
  ```python
  >>> from transformers import GPT2Tokenizer
  
  >>> tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
  >>> tokenizer("Hello world")["input_ids"]
  [15496, 995]
  
  >>> tokenizer(" Hello world")["input_ids"]
  [18435, 995]
  ```
  
  You can get around that behavior by passing `add_prefix_space=True` when instantiating this tokenizer or when you
  call it on some text, but since the model was not pretrained this way, it might yield a decrease in performance.
  
  <Tip>
  
  When used with `is_split_into_words=True`, this tokenizer will add a space before each word (even the first one).
  
  </Tip>
  
  This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
  this superclass for more information regarding those methods.
  
  Args:
      vocab_file (`str`):
          Path to the vocabulary file.
      merges_file (`str`):
          Path to the merges file.
      errors (`str`, *optional*, defaults to `"replace"`):
          Paradigm to follow when decoding bytes to UTF-8. See
          [bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information.
      unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
          The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
          token instead.
      bos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
          The beginning of sequence token.
      eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
          The end of sequence token.
      pad_token (`str`, *optional*):
          The token used for padding, for example when batching sequences of different lengths.
      add_prefix_space (`bool`, *optional*, defaults to `False`):
          Whether or not to add an initial space to the input. This allows to treat the leading word just as any
          other word. (GPT2 tokenizer detect beginning of words by the preceding space).
      add_bos_token (`bool`, *optional*, defaults to `False`):
          Whether or not to add an initial beginning of sentence token to the input. This allows to treat the leading
          word just as any other word.
class_docstr_tok: ["Construct","a","GPT-2","tokenizer",".","Based","on","byte-level","Byte-Pair-Encoding",".","This","tokenizer","has","been","trained","to","treat","spaces","like","parts","of","the","tokens","(","a","bit","like","sentencepiece",")","so","a","word","will","be","encoded","differently","whether","it","is","at","the","beginning","of","the","sentence","(","without","space",")","or","not",":","``","`","python",">",">",">","from","transformers","import","GPT2Tokenizer",">",">",">","tokenizer","=","GPT2Tokenizer.from_pretrained","(","``","gpt2","''",")",">",">",">","tokenizer","(","``","Hello","world","''",")","[","``","input_ids","''","]","[","15496",",","995","]",">",">",">","tokenizer","(","``","Hello","world","''",")","[","``","input_ids","''","]","[","18435",",","995","]","``","`","You","can","get","around","that","behavior","by","passing","`","add_prefix_space=True","`","when","instantiating","this","tokenizer","or","when","you","call","it","on","some","text",",","but","since","the","model","was","not","pretrained","this","way",",","it","might","yield","a","decrease","in","performance",".","<","Tip",">","When","used","with","`","is_split_into_words=True","`",",","this","tokenizer","will","add","a","space","before","each","word","(","even","the","first","one",")",".","<","\/Tip",">","This","tokenizer","inherits","from","[","`","PreTrainedTokenizer","`","]","which","contains","most","of","the","main","methods",".","Users","should","refer","to","this","superclass","for","more","information","regarding","those","methods",".","Args",":","vocab_file","(","`","str","`",")",":","Path","to","the","vocabulary","file",".","merges_file","(","`","str","`",")",":","Path","to","the","merges","file",".","errors","(","`","str","`",",","*","optional","*",",","defaults","to","`","``","replace","''","`",")",":","Paradigm","to","follow","when","decoding","bytes","to","UTF-8",".","See","[","bytes.decode","]","(","https",":","\/\/docs.python.org\/3\/library\/stdtypes.html","#","bytes.decode",")","for","more","information",".","unk_token","(","`","str","`",",","*","optional","*",",","defaults","to","`","``","<","|endoftext|",">","''","`",")",":","The","unknown","token",".","A","token","that","is","not","in","the","vocabulary","can","not","be","converted","to","an","ID","and","is","set","to","be","this","token","instead",".","bos_token","(","`","str","`",",","*","optional","*",",","defaults","to","`","``","<","|endoftext|",">","''","`",")",":","The","beginning","of","sequence","token",".","eos_token","(","`","str","`",",","*","optional","*",",","defaults","to","`","``","<","|endoftext|",">","''","`",")",":","The","end","of","sequence","token",".","pad_token","(","`","str","`",",","*","optional","*",")",":","The","token","used","for","padding",",","for","example","when","batching","sequences","of","different","lengths",".","add_prefix_space","(","`","bool","`",",","*","optional","*",",","defaults","to","`","False","`",")",":","Whether","or","not","to","add","an","initial","space","to","the","input",".","This","allows","to","treat","the","leading","word","just","as","any","other","word",".","(","GPT2","tokenizer","detect","beginning","of","words","by","the","preceding","space",")",".","add_bos_token","(","`","bool","`",",","*","optional","*",",","defaults","to","`","False","`",")",":","Whether","or","not","to","add","an","initial","beginning","of","sentence","token","to","the","input",".","This","allows","to","treat","the","leading","word","just","as","any","other","word","."]
code: def bpe(self, token):
      if token in self.cache:
          return self.cache[token]
      word = tuple(token)
      pairs = get_pairs(word)
  
      if not pairs:
          return token
  
      while True:
          bigram = min(
              pairs,
              key=lambda pair: self.bpe_ranks.get(pair, float("inf")),
          )
          if bigram not in self.bpe_ranks:
              break
          first, second = bigram
          new_word = []
          i = 0
          while i < len(word):
              try:
                  j = word.index(first, i)
              except ValueError:
                  new_word.extend(word[i:])
                  break
              else:
                  new_word.extend(word[i:j])
                  i = j
  
              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
          new_word = tuple(new_word)
          word = new_word
          if len(word) == 1:
              break
          else:
              pairs = get_pairs(word)
      word = " ".join(word)
      self.cache[token] = word
      return word
  
imports: import json
  import os
  from functools import lru_cache
  from typing import List, Optional, Tuple
  import regex
  from ...tokenization_utils import AddedToken, PreTrainedTokenizer
  from ...utils import logging
label_desc: Use image node_id 4 for calling the GPT2Tokenizer obj's underlying member method code with example usage: obj.bpe(token) and returns: word, self, token