class_docstr: Compute relative multi-head attention for XLNet encoder.
  
  This module computes relative multi-head attention as explained in
  `Transformer-XL, (Zihang et. al)` and in `XLNet (Zhiling et. al)`.
  
  Args:
      r_r_bias: A tensor of shape `(num_heads, head_dim)`.
          The bias value added to query head while computing position based
          attention score.
  
      r_w_bias: A tensor of shape `(num_heads, head_dim)`.
          The bias value added to query head while computing content based
          attention score.
  
      r_s_bias (optional): A tensor of shape `(num_heads, head_dim)`.
          The bias value added to query head while computing segment based
          attention score.
  
      segment_embed (optional): A tensor of shape `(2, num_heads, head_dim)`
          if use_segments is True. Otherwise, this is set to None.
  
      hparams (dict or HParams, optional): Hyperparameters. Missing
          hyperparameter will be set to default values. See
          :meth:`default_hparams` for the hyperparameter sturcture
          and default values.
class_docstr_tok: ["Compute","relative","multi-head","attention","for","XLNet","encoder",".","This","module","computes","relative","multi-head","attention","as","explained","in","`","Transformer-XL",",","(","Zihang","et",".","al",")","`","and","in","`","XLNet","(","Zhiling","et",".","al",")","`",".","Args",":","r_r_bias",":","A","tensor","of","shape","`","(","num_heads",",","head_dim",")","`",".","The","bias","value","added","to","query","head","while","computing","position","based","attention","score",".","r_w_bias",":","A","tensor","of","shape","`","(","num_heads",",","head_dim",")","`",".","The","bias","value","added","to","query","head","while","computing","content","based","attention","score",".","r_s_bias","(","optional",")",":","A","tensor","of","shape","`","(","num_heads",",","head_dim",")","`",".","The","bias","value","added","to","query","head","while","computing","segment","based","attention","score",".","segment_embed","(","optional",")",":","A","tensor","of","shape","`","(","2",",","num_heads",",","head_dim",")","`","if","use_segments","is","True",".","Otherwise",",","this","is","set","to","None",".","hparams","(","dict","or","HParams",",","optional",")",":","Hyperparameters",".","Missing","hyperparameter","will","be","set","to","default","values",".","See",":","meth",":","`","default_hparams","`","for","the","hyperparameter","sturcture","and","default","values","."]
code: def _compute_attention_score(
      self,
      q_head,
      k_head_h,
      v_head_h,
      k_head_r,
      segment_mat,
      attn_mask=None,
      mode=None,
  ):
      is_training = is_train_mode(mode)
  
      # Content based attention score.
      q_head_rw = q_head + self.r_w_bias
      # attn_ac: (max_time, tot_len, batch_size, n_head)
      attn_ac = tf.einsum("ibnd,jbnd->ijbn", q_head_rw, k_head_h)
  
      # Position based attention score.
      q_head_rr = q_head + self.r_r_bias
      # attn_bd: (max_time, tot_len, batch_size, n_head)
      attn_bd = tf.einsum("ibnd,jbnd->ijbn", q_head_rr, k_head_r)
      attn_bd = self._rel_shift(attn_bd, klen=tf.shape(attn_ac)[1])
  
      # Segment based attention score.
      if segment_mat is None:
          attn_ef = 0
      else:
          q_head_rs = q_head + self.r_s_bias
          attn_ef = tf.einsum(
              "ibnd,snd->ibns", q_head_rs, self.segment_embed
          )
          attn_ef = tf.einsum("ijbs,ibns->ijbn", segment_mat, attn_ef)
  
      # Merge attention scores and perform masking.
      # attn_score: (max_time, tot_len, batch_size, n_head)
      attn_score = (attn_ac + attn_bd + attn_ef) * self.scale
      if attn_mask is not None:
          # attn_score = attn_score * (1 - attn_mask) - 1e30 * attn_mask
          attn_score = attn_score - 1e30 * attn_mask
  
      # attention probability
      attn_prob = tf.nn.softmax(attn_score, 1)
      attn_prob = self.dropout_attn(attn_prob, training=is_training)
  
      # attention output
      attn_vec = tf.einsum("ijbn,jbnd->ibnd", attn_prob, v_head_h)
  
      return attn_vec
  
imports: import tensorflow
  from texar.tf.core import layers
  from texar.tf.module_base import ModuleBase
  from texar.tf.utils.mode import is_train_mode
label_desc: Use image node_id 4 for calling the RelativeMutiheadAttention obj's underlying member method code with example usage: obj._compute_attention_score(q_head, k_head_h, v_head_h, k_head_r, segment_mat, attn_mask, mode) and returns: attn_vec