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"]
docstr: Compute XLNet representations for the input. This layer exists
  because :class:`XLNetDecoder` compute embeddings in the decoder helper.
  `word_embed` has shape `[batch_size, max_time, word_embed_dim]`.
  Please refer to :meth:`_build` for the detailed information of other
  arguments.
docstr_tok: ["Compute","XLNet","representations","for","the","input",".","This","layer","exists","because",":","class",":","`","XLNetDecoder","`","compute","embeddings","in","the","decoder","helper",".","`","word_embed","`","has","shape","`","[","batch_size",",","max_time",",","word_embed_dim","]","`",".","Please","refer","to",":","meth",":","`","_build","`","for","the","detailed","information","of","other","arguments","."]
code: def _execute(
      self,
      word_embed,
      segment_ids=None,  # noqa: C901
      input_mask=None,
      memory=None,
      permute_mask=None,
      target_mapping=None,
      bi_data=False,
      clamp_len=None,
      cache_len=0,
      same_length=False,
      attn_type="bi",
      two_stream=False,
      mode=None,
  ):
      r"""Compute XLNet representations for the input. This layer exists
      because :class:`XLNetDecoder` compute embeddings in the decoder helper.
      `word_embed` has shape `[batch_size, max_time, word_embed_dim]`.
      Please refer to :meth:`_build` for the detailed information of other
      arguments.
      """
      # word_embed: [max_time, batch_size, word_embed_dim]
      word_embed = tf.transpose(word_embed, perm=[1, 0, 2])
      # segment_ids: [max_time, batch_size]
      if segment_ids is not None:
          segment_ids = tf.transpose(segment_ids, perm=[1, 0])
      # input_mask: [max_time, batch_size]
      if input_mask is not None:
          input_mask = tf.transpose(input_mask, perm=[1, 0])
      # memory: A list of length num_layers
      # each tensor of shape [mem_len, batch_size, hidden_dim]
      if memory is not None:
          memory = [tf.transpose(m, perm=[1, 0, 2]) for m in memory]
      # permute_mask: [max_time, max_time, batch_size]
      if permute_mask is not None:
          permute_mask = tf.transpose(permute_mask, perm=[1, 2, 0])
      # target_mapping: [num_targets, max_time, batch_size]
      if target_mapping is not None:
          target_mapping = tf.transpose(target_mapping, perm=[1, 2, 0])
  
      max_time = tf.shape(word_embed)[0]
      batch_size = tf.shape(word_embed)[1]
      mem_len = tf.shape(memory[0])[0] if memory is not None else 0
      tot_len = max_time + mem_len
      reuse_len = self._hparams.reuse_len
      is_training = is_train_mode(mode)
  
      # Attention mask
      # causal attention mask
      if attn_type == "uni":
          attn_mask = self._create_mask(
              max_time, mem_len, tf.float32, same_length
          )
          attn_mask = attn_mask[:, :, None, None]
      elif attn_type == "bi":
          attn_mask = None
      else:
          raise ValueError(
              "Unsupported attention type: {}".format(attn_type)
          )
  
      # data mask: input mask & perm mask
      if input_mask is not None and permute_mask is not None:
          data_mask = input_mask[None] + permute_mask
      elif input_mask is not None and permute_mask is None:
          data_mask = input_mask[None]
      elif input_mask is None and permute_mask is not None:
          data_mask = permute_mask
      else:
          data_mask = None
  
      if data_mask is not None:
          # all mems can be attended to
          mems_mask = tf.zeros(
              [tf.shape(data_mask)[0], mem_len, batch_size],
              dtype=tf.float32,
          )
          data_mask = tf.concat([mems_mask, data_mask], 1)
          if attn_mask is None:
              attn_mask = data_mask[:, :, :, None]
          else:
              attn_mask += data_mask[:, :, :, None]
  
      if attn_mask is not None:
          attn_mask = tf.cast(attn_mask > 0, dtype=tf.float32)
  
      if attn_mask is not None:
          non_tgt_mask = -tf.eye(max_time, dtype=tf.float32)
          non_tgt_mask = tf.concat(
              [
                  tf.zeros([max_time, mem_len], dtype=tf.float32),
                  non_tgt_mask,
              ],
              axis=-1,
          )
          non_tgt_mask = tf.cast(
              (attn_mask + non_tgt_mask[:, :, None, None]) > 0,
              dtype=tf.float32,
          )
      else:
          non_tgt_mask = None
  
      # Segment embedding
      if segment_ids is not None:
          mem_pad = tf.zeros([mem_len, batch_size], dtype=tf.int32)
          cat_ids = tf.concat([mem_pad, segment_ids], 0)
          segment_matrix = tf.cast(
              tf.logical_not(
                  tf.equal(segment_ids[:, None], cat_ids[None, :])
              ),
              tf.int32,
          )
          segment_matrix = tf.one_hot(
              segment_matrix, 2, dtype=tf.float32
          )
      else:
          segment_matrix = None
  
      # Position embedding
      pos_embed = self.pos_embed(
          batch_size, max_time, tot_len, clamp_len, attn_type, bi_data
      )
      pos_embed = self.dropout(pos_embed, training=is_training)
  
      states_h = self.dropout(word_embed, training=is_training)
  
      if two_stream:
          if target_mapping is not None:
              word_embed_q = tf.tile(
                  self.mask_embed,
                  [tf.shape(target_mapping)[0], batch_size, 1],
              )
          else:
              word_embed_q = word_embed
          states_g = self.dropout(word_embed_q)
      else:
          states_g = None
  
      new_memory = []
      num_layers = self._hparams.num_layers
      for i in range(num_layers):
          cur_memory = memory[i] if memory is not None else None
          if cache_len > 0:
              new_memory.append(
                  self._cache_mem(
                      states_h, cur_memory, cache_len, reuse_len
                  )
              )
          states_h, states_g = self.attn_layers[i](
              states_h=states_h,
              pos_embed=pos_embed,
              states_g=states_g,
              segment_mat=segment_matrix,
              attn_mask_h=non_tgt_mask,
              attn_mask_g=attn_mask,
              target_mapping=None,
              memory=cur_memory,
              mode=mode,
          )
          ff_layer = self.ff_layers[i]
          states_h = ff_layer(states_h, mode=mode)
  
          if states_g is not None:
              states_g = ff_layer(states_g, mode=mode)
  
      output = self.dropout(
          states_h if states_g is None else states_g,
          training=is_training,
      )
  
      # Now output: [max_time, batch_size, hidden_dim]
      # new_memory: None or A list of length num_layers,
      # each tensor of shape [cache_len, batch_size, hidden_dim]
      output = tf.transpose(output, perm=[1, 0, 2])
      if new_memory is not None:
          new_memory = [
              tf.transpose(m, perm=[1, 0, 2]) for m in new_memory
          ]
  
      if not self._built:
          self._add_internal_trainable_variables()
          self._built = True
  
          if self.pretrained_model_dir:
              self.init_pretrained_weights(self.variable_scope.name)
  
      if cache_len == 0:
          return output, None
  
      return output, new_memory
  
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 9 for calling the XLNetEncoder obj's underlying member method code with example usage: obj._execute(word_embed, segment_ids, input_mask, memory, permute_mask, target_mapping, bi_data, clamp_len, cache_len, same_length, attn_type, two_stream, mode) and returns: output,  new_memory, output,  None