class_docstr: Multi-headed attention layer.
  
  Arguments:
      units: Positive integer, output dim of hidden layer.
      num_heads: Positive integer, number of heads
          to repeat the same attention structure.
      attention_dropout_rate: Float, dropout rate inside attention for training.
      density: Approximate fraction of trainable weights (in
          `RandomlyConnectedDense` layers).
      unidirectional: Boolean, use a unidirectional or bidirectional encoder.
      use_key_relative_position: Boolean, if 'True' use key
          relative embeddings in attention.
      use_value_relative_position: Boolean, if 'True' use value
          relative embeddings in attention.
      max_relative_position: Positive integer, max position for relative embeddings.
      heads_share_relative_embedding: Boolean, if 'True'
          heads will share relative embeddings.
class_docstr_tok: ["Multi-headed","attention","layer",".","Arguments",":","units",":","Positive","integer",",","output","dim","of","hidden","layer",".","num_heads",":","Positive","integer",",","number","of","heads","to","repeat","the","same","attention","structure",".","attention_dropout_rate",":","Float",",","dropout","rate","inside","attention","for","training",".","density",":","Approximate","fraction","of","trainable","weights","(","in","`","RandomlyConnectedDense","`","layers",")",".","unidirectional",":","Boolean",",","use","a","unidirectional","or","bidirectional","encoder",".","use_key_relative_position",":","Boolean",",","if","'True","'","use","key","relative","embeddings","in","attention",".","use_value_relative_position",":","Boolean",",","if","'True","'","use","value","relative","embeddings","in","attention",".","max_relative_position",":","Positive","integer",",","max","position","for","relative","embeddings",".","heads_share_relative_embedding",":","Boolean",",","if","'True'","heads","will","share","relative","embeddings","."]
docstr: Universal method to convert tensor from relative to absolute indexing.
  
  "Slides" relative embeddings by 45 degree.
  
  Arguments:
  x: A tensor of shape (batch, num_heads, length, relative_length, depth)
      or (batch, num_heads, length, relative_length)
  
  Returns:
      A tensor of shape (batch, num_heads, length, length, depth)
      or (batch, num_heads, length, length)
docstr_tok: ["Universal","method","to","convert","tensor","from","relative","to","absolute","indexing",".","``","Slides","''","relative","embeddings","by","45","degree",".","Arguments",":","x",":","A","tensor","of","shape","(","batch",",","num_heads",",","length",",","relative_length",",","depth",")","or","(","batch",",","num_heads",",","length",",","relative_length",")","Returns",":","A","tensor","of","shape","(","batch",",","num_heads",",","length",",","length",",","depth",")","or","(","batch",",","num_heads",",","length",",","length",")"]
code: def _relative_to_absolute_position(self, x: tf.Tensor) -> tf.Tensor:
      """Universal method to convert tensor from relative to absolute indexing.
  
      "Slides" relative embeddings by 45 degree.
  
      Arguments:
      x: A tensor of shape (batch, num_heads, length, relative_length, depth)
          or (batch, num_heads, length, relative_length)
  
      Returns:
          A tensor of shape (batch, num_heads, length, length, depth)
          or (batch, num_heads, length, length)
      """
      x_dim = len(x.shape)
  
      if x_dim < 4 or x_dim > 5:
          raise ValueError(
              f"Relative tensor has a wrong shape {x.shape}, "
              f"it should have 4 or 5 dimensions."
          )
      if x_dim == 4:
          # add fake depth dimension
          x = tf.expand_dims(x, axis=-1)
  
      batch = tf.shape(x)[0]
      num_heads = tf.shape(x)[1]
      length = tf.shape(x)[2]
      depth = tf.shape(x)[-1]
  
      x = tf.cond(
          length > self.relative_length,
          lambda: self._pad_relative_embeddings(x, length),
          lambda: self._slice_relative_embeddings(x, length),
      )
  
      # add a column of zeros to "slide" columns to diagonals through reshape
      pad_shift = tf.zeros_like(x[:, :, :, -1:, :])
      x = tf.concat([x, pad_shift], axis=-2)
  
      # flatten length dimensions
      x = tf.reshape(x, (batch, num_heads, -1, depth))
      width = 2 * length
  
      # add zeros so that the result of back reshape is still a matrix
      pad_flat = tf.zeros_like(
          x[
              :,
              :,
              : ((width - 1) - width * length % (width - 1))
              % (width - 1),
              :,
          ]
      )
      x = tf.concat([x, pad_flat], axis=-2)
  
      # "slide" columns to diagonals through reshape
      x = tf.reshape(x, (batch, num_heads, -1, width - 1, depth))
  
      # slice needed "diagonal" matrix
      x = x[:, :, :-1, -length:, :]
  
      if x_dim == 4:
          # remove fake depth dimension
          x = tf.squeeze(x, axis=-1)
  
      return x
  
imports: from typing import Optional, Text, Tuple, Union
  import numpy
  import tensorflow
  from keras.utils.control_flow_util import smart_cond
  from tensorflow.keras import backend
  import rasa.shared.utils.cli
  from rasa.utils.tensorflow.layers import RandomlyConnectedDense
label_desc: Use image node_id 5 for calling the MultiHeadAttention obj's underlying member method code with example usage: obj._relative_to_absolute_position(x) and returns: x