class_docstr: Raw XLNet module for encoding sequences. Please see
  :class:`~texar.tf.modules.PretrainedXLNetMixin` for a brief description
  of XLNet.
  
  Args:
      pretrained_model_name (optional): a `str`, the name
          of pre-trained model (e.g., ``xlnet-based-cased``). Please refer to
          :class:`~texar.tf.modules.PretrainedXLNetMixin` 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.
  
  .. document private functions
  .. automethod:: _build
class_docstr_tok: ["Raw","XLNet","module","for","encoding","sequences",".","Please","see",":","class",":","`","~texar.tf.modules.PretrainedXLNetMixin","`","for","a","brief","description","of","XLNet",".","Args",":","pretrained_model_name","(","optional",")",":","a","`","str","`",",","the","name","of","pre-trained","model","(","e.g.",",","``","xlnet-based-cased","``",")",".","Please","refer","to",":","class",":","`","~texar.tf.modules.PretrainedXLNetMixin","`","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",".","..","document","private","functions","..","automethod",":",":","_build"]
code: def __init__(
      self, pretrained_model_name=None, cache_dir=None, hparams=None
  ):
      super(XLNetEncoder, self).__init__(hparams=hparams)
  
      self.load_pretrained_config(pretrained_model_name, cache_dir)
  
      num_layers = self._hparams.num_layers
      use_segments = self._hparams.use_segments
      untie_r = self._hparams.untie_r
  
      with tf.variable_scope(self.variable_scope):
          if untie_r:
              self.r_w_bias = tf.get_variable(
                  "r_w_bias",
                  [
                      num_layers,
                      self._hparams.num_heads,
                      self._hparams.head_dim,
                  ],
                  dtype=tf.float32,
              )
              self.r_r_bias = tf.get_variable(
                  "r_r_bias",
                  [
                      num_layers,
                      self._hparams.num_heads,
                      self._hparams.head_dim,
                  ],
                  dtype=tf.float32,
              )
          else:
              self.r_w_bias = tf.get_variable(
                  "r_w_bias",
                  [self._hparams.num_heads, self._hparams.head_dim],
                  dtype=tf.float32,
              )
              self.r_r_bias = tf.get_variable(
                  "r_r_bias",
                  [self._hparams.num_heads, self._hparams.head_dim],
                  dtype=tf.float32,
              )
  
          if use_segments:
              self.segment_embed = tf.get_variable(
                  "seg_embed",
                  [
                      num_layers,
                      2,
                      self._hparams.num_heads,
                      self._hparams.head_dim,
                  ],
                  dtype=tf.float32,
              )
              self.r_s_bias = (
                  tf.get_variable(
                      "r_s_bias",
                      [
                          num_layers,
                          self._hparams.num_heads,
                          self._hparams.head_dim,
                      ],
                      dtype=tf.float32,
                  )
                  if untie_r
                  else tf.get_variable(
                      "r_s_bias",
                      [self._hparams.num_heads, self._hparams.head_dim],
                      dtype=tf.float32,
                  )
              )
          else:
              self.segment_embed = None
              self.r_s_bias = None
  
          # Word embedding
          self.word_embedder = WordEmbedder(
              vocab_size=self._hparams.vocab_size,
              hparams={"dim": self._hparams.hidden_dim},
          )
  
          # Position embedding
          self.pos_embed = RelativePositionalEncoding(
              hparams={
                  "dim": self._hparams.hidden_dim,
                  "max_seq_len": self._hparams.max_seq_len,
              }
          )
  
          self.attn_layers = []
          self.ff_layers = []
          rel_attn_hparams = dict_fetch(
              self._hparams, RelativeMutiheadAttention.default_hparams()
          )
          rel_attn_hparams["name"] = "rel_attn"
  
          ff_hparams = dict_fetch(
              self._hparams, PositionWiseFF.default_hparams()
          )
          ff_hparams["name"] = "ff"
  
          for i in range(num_layers):
              with tf.variable_scope("layer_{}".format(i)):
                  if self._hparams.untie_r:
                      if use_segments:
                          self.attn_layers.append(
                              RelativeMutiheadAttention(
                                  self.r_r_bias[i],
                                  self.r_w_bias[i],
                                  self.r_s_bias[i],
                                  self.segment_embed[i],
                                  hparams=rel_attn_hparams,
                              )
                          )
                      else:
                          self.attn_layers.append(
                              RelativeMutiheadAttention(
                                  self.r_r_bias[i],
                                  self.r_w_bias[i],
                                  hparams=rel_attn_hparams,
                              )
                          )
                  else:
                      if use_segments:
                          self.attn_layers.append(
                              RelativeMutiheadAttention(
                                  self.r_r_bias,
                                  self.r_w_bias,
                                  self.r_s_bias,
                                  self.segment_embed[i],
                                  hparams=rel_attn_hparams,
                              )
                          )
                      else:
                          self.attn_layers.append(
                              RelativeMutiheadAttention(
                                  self.r_r_bias,
                                  self.r_w_bias,
                                  hparams=rel_attn_hparams,
                              )
                          )
                  self.ff_layers.append(
                      PositionWiseFF(hparams=ff_hparams)
                  )
  
          dropout_hparams = {
              "type": "Dropout",
              "kwargs": {"rate": self._hparams.dropout},
          }
          self.dropout = get_layer(hparams=dropout_hparams)
  
          self.mask_embed = tf.get_variable(
              "mask_emb",
              [1, 1, self.hparams.hidden_dim],
              dtype=tf.float32,
          )
  
imports: import tensorflow
  from texar.tf.utils.mode import is_train_mode
  from texar.tf.core.layers import get_initializer, get_layer
  from texar.tf.modules.embedders.embedders import WordEmbedder
  from texar.tf.modules.encoders.encoder_base import EncoderBase
  from texar.tf.modules.pretrained.xlnet import PretrainedXLNetMixin
  from texar.tf.modules.pretrained.xlnet_utils import PositionWiseFF, RelativePositionalEncoding, RelativeMutiheadAttention
  from texar.tf.utils.utils import dict_fetch
label_desc: Use image node_id 1 to create a new XLNetEncoder object from inherited base classes: EncoderBase, PretrainedXLNetMixin with example: obj = XLNetEncoder(pretrained_model_name, cache_dir, hparams)