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:
  
  ::
  
      >>> 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.
  
  .. note::
  
      When used with ``is_split_into_words=True``, this tokenizer will add a space before each word (even the first
      one).
  
  This tokenizer inherits from :class:`~transformers.PreTrainedTokenizer` which contains most of the main methods.
  Users should refer to this superclass for more information regarding those methods.
  
  Args:
      vocab_file (:obj:`str`):
          Path to the vocabulary file.
      merges_file (:obj:`str`):
          Path to the merges file.
      errors (:obj:`str`, `optional`, defaults to :obj:`"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 (:obj:`str`, `optional`, defaults to :obj:`<|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 (:obj:`str`, `optional`, defaults to :obj:`<|endoftext|>`):
          The beginning of sequence token.
      eos_token (:obj:`str`, `optional`, defaults to :obj:`<|endoftext|>`):
          The end of sequence token.
      add_prefix_space (:obj:`bool`, `optional`, defaults to :obj:`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).
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",":",":",":",">",">",">","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",".","..","note",":",":","When","used","with","``","is_split_into_words=True","``",",","this","tokenizer","will","add","a","space","before","each","word","(","even","the","first","one",")",".","This","tokenizer","inherits","from",":","class",":","`","~transformers.PreTrainedTokenizer","`","which","contains","most","of","the","main","methods",".","Users","should","refer","to","this","superclass","for","more","information","regarding","those","methods",".","Args",":","vocab_file","(",":","obj",":","`","str","`",")",":","Path","to","the","vocabulary","file",".","merges_file","(",":","obj",":","`","str","`",")",":","Path","to","the","merges","file",".","errors","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","``","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","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","<","|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","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","<","|endoftext|",">","`",")",":","The","beginning","of","sequence","token",".","eos_token","(",":","obj",":","`","str","`",",","`","optional","`",",","defaults","to",":","obj",":","`","<","|endoftext|",">","`",")",":","The","end","of","sequence","token",".","add_prefix_space","(",":","obj",":","`","bool","`",",","`","optional","`",",","defaults","to",":","obj",":","`","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",")","."]
code: def __init__(
      self,
      vocab_file,
      merges_file,
      errors="replace",
      unk_token="<|endoftext|>",
      bos_token="<|endoftext|>",
      eos_token="<|endoftext|>",
      add_prefix_space=False,
      **kwargs,
  ):
      bos_token = (
          AddedToken(bos_token, lstrip=False, rstrip=False)
          if isinstance(bos_token, str)
          else bos_token
      )
      eos_token = (
          AddedToken(eos_token, lstrip=False, rstrip=False)
          if isinstance(eos_token, str)
          else eos_token
      )
      unk_token = (
          AddedToken(unk_token, lstrip=False, rstrip=False)
          if isinstance(unk_token, str)
          else unk_token
      )
      super().__init__(
          errors=errors,
          unk_token=unk_token,
          bos_token=bos_token,
          eos_token=eos_token,
          add_prefix_space=add_prefix_space,
          **kwargs,
      )
  
      with open(vocab_file, encoding="utf-8") as vocab_handle:
          self.encoder = json.load(vocab_handle)
      self.decoder = {v: k for k, v in self.encoder.items()}
      self.errors = errors  # how to handle errors in decoding
      self.byte_encoder = bytes_to_unicode()
      self.byte_decoder = {v: k for k, v in self.byte_encoder.items()}
      with open(merges_file, encoding="utf-8") as merges_handle:
          bpe_merges = merges_handle.read().split("\n")[1:-1]
      bpe_merges = [tuple(merge.split()) for merge in bpe_merges]
      self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
      self.cache = {}
      self.add_prefix_space = add_prefix_space
  
      # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
      self.pat = re.compile(
          r"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""
      )
  
imports: import json
  import os
  from functools import lru_cache
  from typing import TYPE_CHECKING, List, Optional, Tuple
  import regex
  from fastNLP.transformers.torch.tokenization_utils import AddedToken, PreTrainedTokenizer
  from fastNLP.core.log import logger
label_desc: Use image node_id 1 to create a new GPT2Tokenizer object from inherited base classes: PreTrainedTokenizer with example: obj = GPT2Tokenizer(vocab_file, merges_file, errors, unk_token, bos_token, eos_token, add_prefix_space)