If your original tensor is sorted (or if you could sort it), the reverse indices of unique would give you the desired tensor:
x = torch.tensor([ 0, 0, 1, 1, 3, 3, 7])
_, idx = x.unique(sorted=True, return_inverse=True)
print(idx)
> tensor([0, 0, 1, 1, 2, 2, 3])