If I understand properly, this could help your
>>> import torch
>>> value = torch.tensor([2,9,7,8,4])
>>> row_of_value = torch.tensor([0,0,1,2,3])
>>> vec_row = [torch.zeros((row_of_value==i).sum())+value[row_of_value==i] for i in range(torch.max(row_of_value)+1)]
>>> vec_row
[tensor([2., 9.]), tensor([7.]), tensor([8.]), tensor([4.])]
>>> torch.nn.utils.rnn.pad_sequence(vec_row,batch_first=True)
tensor([[2., 9.],
        [7., 0.],
        [8., 0.],
        [4., 0.]])