def pad_tensor(t, length):
  """Pads the input tensor with 0s along the first dimension up to the length.

  Args:
    t: the input tensor, assuming the rank is at least 1.
    length: a tensor of shape [1]  or an integer, indicating the first dimension
      of the input tensor t after padding, assuming length <= t.shape[0].

  Returns:
    padded_t: the padded tensor, whose first dimension is length. If the length
      is an integer, the first dimension of padded_t is set to length
      statically.
  """

  # Computing the padding statically makes the operation work with XLA.
  rank = len(t.get_shape())
  paddings = [[0 for _ in range(2)] for _ in range(rank)]
  t_d0 = tf.shape(t)[0]

  if isinstance(length, int) or len(length.get_shape()) == 0:  # pylint:disable=g-explicit-length-test
    paddings[0][1] = length - t_d0
  else:
    paddings[0][1] = length[0] - t_d0

  return tf.pad(t, paddings)
Pads the input tensor with 0s along the first dimension up to the length.

  Args:
    t: the input tensor, assuming the rank is at least 1.
    length: a tensor of shape [1]  or an integer, indicating the first dimension
      of the input tensor t after padding, assuming length <= t.shape[0].

  Returns:
    padded_t: the padded tensor, whose first dimension is length. If the length
      is an integer, the first dimension of padded_t is set to length
      statically.
  
inference_api/src/main/object_detection/utils/shape_utils.py