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","."]
docstr: Perform relative shift to form the relative attention score.
docstr_tok: ["Perform","relative","shift","to","form","the","relative","attention","score","."]
code: def _rel_shift(x, klen=-1):
      """Perform relative shift to form the relative attention score."""
      x_size = tf.shape(x)
  
      x = tf.reshape(x, [x_size[1], x_size[0], x_size[2], x_size[3]])
      x = tf.slice(x, [1, 0, 0, 0], [-1, -1, -1, -1])
      x = tf.reshape(
          x, [x_size[0], x_size[1] - 1, x_size[2], x_size[3]]
      )
      x = tf.slice(x, [0, 0, 0, 0], [-1, klen, -1, -1])
  
      return x
  
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 3 for calling the RelativeMutiheadAttention obj's underlying member method code with example usage: obj._rel_shift(x, klen) and returns: x