def sparse_tile_batch(tensor, multiplier):
  """Tile SparseTensor along batch dimension.

  This is a sparse equivalent for tf.contrib.seq2seq.tile_batch.

  NOTE: Only rank 3 tensors are supported currently.

  Args:
    tensor: A rank 3 SparseTensor.
    multiplier: Number of times to tile the batch dimension.

  Returns:
    tiled_tensor: A rank 3 SparseTensor representing the tiled dense tensor.
  """
  # Collect indices.
  indices = tensor.indices
  values = tensor.values
  batch_indices = indices[:, 0]
  nonbatch_indices = indices[:, 1:]
  batch_size = tensor.dense_shape[0]
  num_row, num_col = tensor.dense_shape[1], tensor.dense_shape[2]
  num_nonzero = tf.shape(indices)[0]

  # Tile non-batch dimensions. Every row is repeated `multiplier` times.
  # So given nonbatch_indices = [[0, 1], [2, 3]] and multiplier = 2,
  # new_nonbatch_indices = [[0, 1], [0, 1], [2, 3], [2, 3]].
  new_nonbatch_i = tf.reshape(
      tf.tile(tf.expand_dims(nonbatch_indices, 1), (1, multiplier, 1)),
      (num_nonzero * multiplier, -1))

  # Tile batch dimension. Every batch index `bi` is expanded into a range from
  # `bi * multiplier : (bi + 1) * multiplier`.
  new_batch_i = tf.reshape(
      multiplier * tf.expand_dims(batch_indices, 1) +
      tf.expand_dims(tf.range(multiplier, dtype=tf.int64), 0),
      (num_nonzero * multiplier, 1))

  # Concatenate to get new indices.
  new_indices = tf.concat([new_batch_i, new_nonbatch_i], 1)

  # Get new values. Tile these `multiplier` times.
  new_values = tf.reshape(
      tf.tile(tf.expand_dims(values, 1), (1, multiplier)),
      (num_nonzero * multiplier,))

  # Create new sparse tensor.
  tiled_tensor = tf.SparseTensor(new_indices,
                                 new_values,
                                 [batch_size * multiplier, num_row, num_col])

  return tiled_tensor
Tile SparseTensor along batch dimension.

  This is a sparse equivalent for tf.contrib.seq2seq.tile_batch.

  NOTE: Only rank 3 tensors are supported currently.

  Args:
    tensor: A rank 3 SparseTensor.
    multiplier: Number of times to tile the batch dimension.

  Returns:
    tiled_tensor: A rank 3 SparseTensor representing the tiled dense tensor.
  
language/labs/exemplar_decoding/models/common.py