def relative_positions_adjacency_matrix(length, batch_size,
                                        max_relative_position):
  """Generates batch of relative position matrices.

  Args:
    length: Integer specifying maximum sequence length in batch.
    batch_size: Integer specifying batch size.
    max_relative_position: Integer specifying the distance at which the relative
      position betwen targets is clipped.

  Returns:
    <int>[batch_size, length, length]
  """
  range_vec = tf.range(length)
  range_mat = tf.reshape(tf.tile(range_vec, [length]), [length, length])
  distance_mat = range_mat - tf.transpose(range_mat)
  distance_mat_clipped = tf.clip_by_value(distance_mat, -max_relative_position,
                                          max_relative_position)
  # Shift values to be >= 0. Each integer still uniquely identifies a relative
  # position difference.
  shifted_mat = distance_mat_clipped + max_relative_position

  # Tile to batch_size, duplicating the same adjacency matrix for each target.
  shifted_mat = tf.expand_dims(shifted_mat, 0)
  shifted_mat = tf.tile(shifted_mat, [batch_size, 1, 1])

  return shifted_mat
Generates batch of relative position matrices.

  Args:
    length: Integer specifying maximum sequence length in batch.
    batch_size: Integer specifying batch size.
    max_relative_position: Integer specifying the distance at which the relative
      position betwen targets is clipped.

  Returns:
    <int>[batch_size, length, length]
  
language/xsp/model/common_layers.py