<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def pad_tensor(tensor, length, padding_index=DEFAULT_PADDING_INDEX):
 """ Pad a ``tensor`` to ``length`` with ``padding_index``. Args: length (int):
 Pad the ``tensor`` up to ``length``. padding_index (int, optional):
 Index to pad tensor with. Returns """

    n_padding = length - tensor.shape[0]
    assert n_padding >= 0
    if n_padding == 0:
        return tensor
    padding = tensor.new(n_padding, *tensor.shape[1:]).fill_(padding_index)
    return torch.cat((tensor, padding), dim=0)