Try
x = tensor([[3, 3, 5, 5, 5],
            [3, 3, 2, 2, 3]])
unique_x, indices = torch.unique_consecutive(x, return_inverse=True)
indices -= indices.min(dim=1, keepdims=True)[0]
result = -torch.ones_like(x)
result = result.scatter_(1, indices, x)