Solved by ptrblck in post #2 
              
              
                unique with return_inverse=True would yield the desired result: 
x = torch.tensor([12, 6, 4, 5, 3, 12, 4])
x.unique(return_inverse=True)
# > (tensor([ 3,  4,  5,  6, 12]), tensor([4, 3, 1, 2, 0, 4, 1]))