I had this old code, based on rnn.utils:
def invert_permutation(permutation):
    output = torch.empty_like(permutation)
    output.scatter_(0, permutation,
                    torch.arange(0, len(permutation), dtype=torch.int64, device=permutation.device))
    return output