Hi,
I did this a few days ago,
# Helper functions

def argsort(seq):
    # http://stackoverflow.com/questions/3071415/efficient-method-to-calculate-the-rank-vector-of-a-list-in-python
    return sorted(range(len(seq)), key=seq.__getitem__)

# apply padding
def pad(tensor, length):
    return torch.cat([tensor, tensor.new(length - tensor.size(0), *tensor.size()[1:]).zero_()])

lengths=[]
for i in range(batch_size):
    length_i = input_variables[i].size()[0]
    lengths.append( length_i )

# sort in numpy - TODO do this in pure python or PyTorch
#lengths_np = np.array((lengths))
#indx = np.argsort(lengths_np, axis=0)[::-1]
#inverse_indx = np.argsort(indx, axis=0)

#sort - #see http://stackoverflow.com/questions/2483696/undo-or-reverse-argsort-python
indx = argsort( lengths ) # ascending
indx = indx[::-1]         # descending
inverse_indx = argsort(indx)

# check
#lengths_dec = [lengths[i] for i in indx] # sorted lengths
#remap_lengths = [lengths_dec[i] for i in inverse_indx] # map 
#lengths == remap_lengths

padded_vec_lst = []
for i in range(batch_size):
    padded_vec = pad(vec_lst[i], max_length)
    padded_vec_lst.append( padded_vec )


It works 
HTHs