class_docstr: Pre-trained GPT2 Tokenizer.
  
  Args:
      pretrained_model_name (optional): a `str`, the name of
          pre-trained model (e.g., `117M`). Please refer to
          :class:`~texar.torch.modules.PretrainedGPT2Mixin` for
          all supported models.
          If None, the model name in :attr:`hparams` is used.
      cache_dir (optional): the path to a folder in which the
          pre-trained models will be cached. If `None` (default),
          a default directory (``texar_data`` folder under user's home
          directory) will be used.
      hparams (dict or HParams, optional): Hyperparameters. Missing
          hyperparameter will be set to default values. See
          :meth:`default_hparams` for the hyperparameter structure
          and default values.
class_docstr_tok: ["Pre-trained","GPT2","Tokenizer",".","Args",":","pretrained_model_name","(","optional",")",":","a","`","str","`",",","the","name","of","pre-trained","model","(","e.g.",",","`","117M","`",")",".","Please","refer","to",":","class",":","`","~texar.torch.modules.PretrainedGPT2Mixin","`","for","all","supported","models",".","If","None",",","the","model","name","in",":","attr",":","`","hparams","`","is","used",".","cache_dir","(","optional",")",":","the","path","to","a","folder","in","which","the","pre-trained","models","will","be","cached",".","If","`","None","`","(","default",")",",","a","default","directory","(","``","texar_data","``","folder","under","user","'s","home","directory",")","will","be","used",".","hparams","(","dict","or","HParams",",","optional",")",":","Hyperparameters",".","Missing","hyperparameter","will","be","set","to","default","values",".","See",":","meth",":","`","default_hparams","`","for","the","hyperparameter","structure","and","default","values","."]
code: def __init__(
      self,
      pretrained_model_name: Optional[str] = None,
      cache_dir: Optional[str] = None,
      hparams=None,
  ):
      self.load_pretrained_config(
          pretrained_model_name, cache_dir, hparams
      )
  
      super().__init__(hparams=None)
  
      self.config = {"errors": self.hparams["errors"]}
  
      if self.pretrained_model_dir is not None:
          vocab_file = os.path.join(
              self.pretrained_model_dir,
              self._VOCAB_FILE_NAMES["vocab_file"],
          )
          merges_file = os.path.join(
              self.pretrained_model_dir,
              self._VOCAB_FILE_NAMES["merges_file"],
          )
          assert pretrained_model_name is not None
          if self._MAX_INPUT_SIZE.get(pretrained_model_name):
              self.max_len = self._MAX_INPUT_SIZE[pretrained_model_name]
      else:
          vocab_file = self.hparams["vocab_file"]
          merges_file = self.hparams["merges_file"]
          if self.hparams.get("max_len"):
              self.max_len = self.hparams["max_len"]
  
      if not os.path.isfile(vocab_file):
          raise ValueError(
              "Can't find a vocabulary file at path "
              "'{}".format(vocab_file)
          )
  
      if not os.path.isfile(merges_file):
          raise ValueError(
              "Can't find a merges file at path "
              "'{}".format(merges_file)
          )
  
      with open(vocab_file) as fp:
          self.encoder = json.load(fp)
      self.decoder = {v: k for k, v in self.encoder.items()}
      self.errors = self.hparams[
          "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 fp:
          bpe_data = fp.read().split("\n")[1:-1]
      bpe_merges = [tuple(merge.split()) for merge in bpe_data]
      self.bpe_ranks = dict(zip(bpe_merges, range(len(bpe_merges))))
      self.cache: Dict[str, str] = {}
  
      # Should haved 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: from typing import Any, Dict, List, Optional, Tuple
  import os
  import json
  import regex
  from texar.tf.modules.pretrained.gpt2 import PretrainedGPT2Mixin
  from texar.tf.data.tokenizers.tokenizer_base import TokenizerBase
  from texar.tf.data.tokenizers.gpt2_tokenizer_utils import bytes_to_unicode, get_pairs
label_desc: Use image node_id 1 to create a new GPT2Tokenizer object from inherited base classes: TokenizerBase, PretrainedGPT2Mixin with example: obj = GPT2Tokenizer(pretrained_model_name, cache_dir, hparams)