Is there any unique function which keeps the order of occurence?

navid_mahmoudian: <p>Hello,</p>
<p>I have two questions regarding the <code>unique</code> function:</p>
<p>1- I figured out that in <a href="https://pytorch.org/docs/master/generated/torch.unique.html#torch-unique" rel="noopener nofollow ugc">Pytorch documentation for unique function</a> when sorted is not provided according to the example given in the documentation the default of it is false:</p>
<blockquote>
<blockquote>
<blockquote>
<p>output = torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long))<br>
output<br>
tensor([ 2,  3,  1])</p>
</blockquote>
</blockquote>
</blockquote>
<p>However, in my computer I get:<br>
torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long))<br>
tensor([1, 2, 3])</p>
<p>, which means that the default value of sorted=True. Where does this inconsistency come from?</p>
<p>2- Moreover, I figured out that the output of</p>
<p><code>torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long), sorted=False)</code><br>
is<br>
<code>tensor([2, 3, 1])</code></p>
<p>, which means that it doesn’t even keep the order of occurrence. How can I have a unique function that keeps track of the first occurrence of elements? I mean, with the above example, the output should be:<br>
<code>tensor([1, 3, 2])</code></p>

Nikronic: <p>Hi,</p>
<p>I have tested your cases and it seems you are right.<br>
Clearly, docs say that if one specifies <code>dim</code> then whether <code>sorted=True</code> or <code>sorted=False</code> the tensor will be sorted even though default values is <code>None</code>. And I think because of that, even though the default value of <code>sorted=False</code> it stills sorts the tensor.<br>
Due to this forced sorting, I think it losses the original order of values.</p>
<p>This is another related post around this issue:</p><aside class="quote quote-modified" data-post="8" data-topic="33095">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/enemis/48/9277_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/is-it-a-bug-for-torch-unique-on-gpu/33095/8">Is it a bug for torch.unique on gpu?</a> 
  </div>
  <blockquote>
    Stumbled upon this now as well. If there is different behaviour, maybe include a warning or similar? I am often changing between gpu and cpu, and this error would not have been expected. 
Also, for my case (5x5 tensors), it was significantly faster to transform to cpu and do the sort there and  back again. 
def unique_via_cpu(x, device):
    return torch.unique(x.cpu(), sorted=False).to(device)

Compared to the function posted above by <a class="mention" href="/u/ptrblck">@ptrblck</a>: 
118 µs ± 1.05 µs per loop (mean ± std. dev. of 7 …
  </blockquote>
</aside>

<p>Also, in the referenced post, you can find an approach to get the correct order but it is not built-in.</p>
<p>Bests</p>

navid_mahmoudian: <p>Hello, Thank you for the response.</p>
<blockquote>
<p>Clearly, docs say that if one specifies <code>dim</code> then whether <code>sorted=True</code> or <code>sorted=False</code> the tensor will be sorted even though default values is <code>None</code> . And I think because of that, even though the default value of <code>sorted=False</code> it stills sorts the tensor.</p>
</blockquote>
<p>But I haven’t declared the dim and the default value of dim is None, and I am working on cpu. So, I don’t understand this behavior.</p>

Nikronic: <p>I think that is the bug, and in the other post I have referenced, this issue has been stated that it acts differently from what it supposed to do, literally skips the <code>sorted</code> argument in GPU implementation.<br>
In CPU mode, order is not correct.</p>
<p>Also, in the source code, there are two types of implementation for <code>unique</code> in both CPU and GPU which acts differently w.r.t. value of <code>ndim</code> but currently it has not been fixed and the issue on github page is still open.</p>