Indices returned by torch.topk is of wrong order

username: ShengweiAn
cooked: <p>The indices return by torch.topk is strange.<br>
In my option, if <code>sorted=False</code>, then the returned indices should be sorted, that is the elements in the indices are ascending.<br>
However, in the following example, <code>t2</code> has something wrong:</p>
<pre><code class="lang-auto">2144,  20,  104,  118,  123,  136,  137,  144,  171
</code></pre>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; t0 = torch.load('./t0.pt')
&gt;&gt;&gt; t0.shape
torch.Size([2146])
&gt;&gt;&gt; t0
tensor([4.4721, 4.5826, 4.3589,  ..., 4.5826, 3.7417, 4.0000], device='cuda:0')
&gt;&gt;&gt; t1, t2 = torch.topk(t0, 200, dim=0, largest=False, sorted=False)
&gt;&gt;&gt; t1
tensor([3.1623, 3.7417, 3.6056, 3.7417, 3.6056, 3.7417, 3.7417, 3.6056, 3.7417,
        3.7417, 3.6056, 3.6056, 3.3166, 3.7417, 3.6056, 3.7417, 3.7417, 3.6056,
        3.7417, 3.7417, 3.7417, 3.7417, 3.7417, 3.7417, 3.7417, 3.4641, 3.7417,
        3.7417, 3.7417, 3.6056, 3.6056, 3.6056, 3.7417, 3.4641, 3.7417, 3.6056,
        3.7417, 3.7417, 3.6056, 3.1623, 3.7417, 3.4641, 3.7417, 3.7417, 3.7417,
        3.7417, 3.7417, 3.7417, 3.6056, 3.7417, 3.7417, 3.7417, 3.7417, 3.4641,
        3.4641, 3.7417, 3.1623, 3.4641, 3.3166, 3.4641, 3.7417, 3.3166, 3.4641,
        3.6056, 3.3166, 3.3166, 3.3166, 3.7417, 3.3166, 3.7417, 3.3166, 3.7417,
        3.4641, 3.7417, 3.3166, 3.7417, 3.7417, 3.6056, 3.7417, 3.7417, 3.4641,
        3.7417, 3.3166, 3.7417, 3.4641, 3.7417, 3.7417, 3.7417, 3.7417, 3.7417,
        3.4641, 3.7417, 3.1623, 3.4641, 3.7417, 3.3166, 3.7417, 3.7417, 3.3166,
        3.4641, 3.4641, 3.3166, 3.4641, 3.7417, 3.7417, 3.4641, 3.7417, 3.7417,
        3.3166, 3.3166, 3.7417, 3.3166, 3.3166, 3.7417, 3.7417, 3.4641, 3.6056,
        3.7417, 3.7417, 3.4641, 3.7417, 3.7417, 3.4641, 3.3166, 3.7417, 3.7417,
        3.7417, 3.4641, 3.7417, 3.3166, 3.7417, 3.3166, 3.4641, 3.7417, 3.3166,
        3.3166, 3.4641, 3.3166, 3.3166, 3.7417, 3.7417, 3.1623, 3.3166, 3.4641,
        3.7417, 3.7417, 3.3166, 3.7417, 3.4641, 3.3166, 3.4641, 3.6056, 3.7417,
        3.7417, 3.6056, 3.7417, 3.0000, 3.6056, 3.7417, 3.4641, 3.7417, 3.3166,
        3.7417, 3.7417, 3.3166, 3.3166, 3.3166, 3.3166, 2.8284, 3.4641, 3.3166,
        3.7417, 3.7417, 3.6056, 3.6056, 3.6056, 3.7417, 3.7417, 3.6056, 3.3166,
        3.7417, 3.7417, 3.7417, 3.4641, 3.4641, 2.8284, 3.4641, 3.4641, 3.7417,
        3.1623, 3.7417, 3.7417, 3.8730, 3.8730, 3.8730, 3.8730, 3.8730, 3.8730,
        3.8730, 3.8730], device='cuda:0')
&gt;&gt;&gt; t2
tensor([   6,   18,   22,   30,  119,  127,  148,  202,  205,  211,  218,  221,
         241,  249,  259,  281,  282,  288,  296,  312,  339,  366,  439,  503,
         523,  526,  533,  534,  541,  665,  676,  677,  716,  720,  724,  734,
         787,  797,  798,  823,  850,  879,  887,  913,  931,  933,  954,  955,
         969,  978,  997, 1009, 1016, 1038, 1040, 1044, 1046, 1047, 1082, 1083,
        1095, 1131, 1143, 1145, 1163, 1166, 1241, 1254, 1258, 1262, 1272, 1291,
        1295, 1299, 1305, 1306, 1309, 1310, 1313, 1320, 1328, 1331, 1334, 1339,
        1343, 1347, 1357, 1365, 1390, 1400, 1402, 1416, 1417, 1419, 1439, 1456,
        1466, 1474, 1501, 1519, 1530, 1535, 1539, 1608, 1619, 1622, 1626, 1630,
        1636, 1651, 1657, 1665, 1667, 1668, 1671, 1672, 1685, 1686, 1694, 1696,
        1701, 1707, 1713, 1716, 1717, 1732, 1761, 1781, 1785, 1788, 1810, 1821,
        1822, 1832, 1863, 1868, 1880, 1887, 1899, 1907, 1917, 1971, 1973, 1985,
        1996, 1997, 1998, 2000, 2005, 2009, 2016, 2017, 2020, 2021, 2027, 2029,
        2034, 2041, 2042, 2043, 2051, 2056, 2059, 2060, 2062, 2064, 2067, 2068,
        2070, 2072, 2073, 2074, 2075, 2076, 2078, 2080, 2082, 2084, 2087, 2092,
        2099, 2107, 2113, 2119, 2120, 2124, 2130, 2131, 2134, 2138, 2140, 2144,
          20,  104,  118,  123,  136,  137,  144,  171], device='cuda:0')
&gt;&gt;&gt; 
</code></pre>

username: ShengweiAn
cooked: <p>I could not upload the t0.py file, so I print it out:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; print(t0)
tensor([4.4721, 4.5826, 4.3589, 4.7958, 4.5826, 4.2426, 3.1623, 4.8990, 4.5826,
        4.8990, 4.7958, 4.8990, 4.5826, 4.8990, 4.7958, 4.5826, 4.4721, 4.8990,
        3.7417, 4.7958, 3.8730, 4.6904, 3.6056, 4.8990, 4.7958, 4.6904, 4.2426,
        4.3589, 4.8990, 4.5826, 3.7417, 4.7958, 4.5826, 4.7958, 4.7958, 4.8990,
        4.5826, 4.5826, 4.5826, 4.8990, 4.2426, 4.5826, 4.4721, 4.1231, 4.8990,
        4.5826, 4.8990, 4.7958, 4.5826, 4.8990, 4.1231, 4.7958, 4.4721, 4.8990,
        4.5826, 4.8990, 4.8990, 4.5826, 4.5826, 4.8990, 4.0000, 4.1231, 4.8990,
        4.5826, 4.8990, 4.3589, 4.2426, 4.8990, 4.2426, 4.8990, 4.2426, 4.8990,
        4.2426, 4.1231, 4.5826, 4.2426, 4.1231, 4.5826, 4.5826, 4.5826, 4.5826,
        4.4721, 4.5826, 4.7958, 4.5826, 4.1231, 4.4721, 4.8990, 4.2426, 4.5826,
        4.5826, 4.1231, 4.3589, 4.5826, 4.4721, 4.1231, 4.4721, 4.4721, 4.5826,
        4.8990, 4.7958, 4.5826, 4.7958, 4.4721, 3.8730, 4.4721, 4.4721, 4.8990,
        5.2915, 4.8990, 4.5826, 4.7958, 4.4721, 4.4721, 4.8990, 4.7958, 4.5826,
        4.8990, 3.8730, 3.6056, 4.7958, 4.5826, 4.5826, 3.8730, 4.0000, 4.6904,
        4.0000, 3.7417, 4.4721, 4.1231, 4.8990, 4.1231, 4.4721, 4.4721, 4.7958,
        4.5826, 3.8730, 3.8730, 4.2426, 4.1231, 4.2426, 4.2426, 4.8990, 4.2426,
        3.8730, 4.1231, 4.4721, 4.4721, 3.7417, 4.3589, 4.3589, 4.1231, 4.1231,
        4.0000, 4.4721, 4.4721, 4.1231, 4.4721, 4.5826, 4.5826, 4.5826, 4.3589,
        4.4721, 4.3589, 4.2426, 4.5826, 4.1231, 4.2426, 4.7958, 4.2426, 4.7958,
        3.8730, 4.7958, 4.8990, 4.2426, 4.4721, 4.0000, 4.8990, 4.6904, 4.6904,
        4.8990, 4.1231, 4.5826, 4.8990, 4.5826, 4.4721, 4.8990, 4.1231, 4.8990,
        4.8990, 4.2426, 4.4721, 4.4721, 4.3589, 4.5826, 5.0990, 4.5826, 4.7958,
        4.2426, 4.2426, 4.1231, 4.1231, 3.6056, 4.5826, 4.1231, 3.7417, 4.4721,
        4.8990, 4.4721, 4.8990, 4.8990, 3.7417, 4.1231, 4.4721, 4.5826, 4.4721,
        4.0000, 4.7958, 3.6056, 4.4721, 4.8990, 3.6056, 4.7958, 4.4721, 3.8730,
        4.8990, 4.7958, 4.2426, 4.4721, 4.8990, 4.1231, 4.7958, 4.8990, 4.8990,
        4.2426, 4.8990, 4.2426, 4.5826, 4.2426, 4.8990, 4.7958, 3.3166, 4.7958,
        4.8990, 4.1231, 3.8730, 4.2426, 4.2426, 4.4721, 3.7417, 4.8990, 4.8990,
        4.2426, 4.4721, 4.8990, 4.8990, 4.4721, 4.5826, 4.8990, 3.6056, 4.5826,
        4.7958, 4.4721, 4.6904, 4.5826, 4.2426, 4.5826, 4.8990, 4.8990, 4.8990,
        4.2426, 4.5826, 4.8990, 4.5826, 4.2426, 4.4721, 4.7958, 4.7958, 4.8990,
        4.1231, 4.2426, 3.7417, 3.7417, 4.3589, 4.8990, 4.7958, 4.2426, 4.6904,
        3.6056, 4.0000, 4.4721, 4.4721, 4.4721, 4.4721, 4.8990, 4.8990, 3.7417,
        4.1231, 4.5826, 4.5826, 4.7958, 4.1231, 4.4721, 4.5826, 4.4721, 4.2426,
        4.4721, 4.0000, 4.5826, 4.5826, 4.8990, 4.4721, 3.7417, 4.5826, 4.0000,
        4.0000, 4.5826, 4.8990, 4.2426, 4.2426, 4.4721, 4.4721, 4.5826, 4.4721,
        4.7958, 4.8990, 4.8990, 4.5826, 3.8730, 4.5826, 4.1231, 3.8730, 4.5826,
        4.5826, 4.1231, 4.8990, 4.1231, 4.5826, 3.8730, 3.7417, 4.8990, 4.2426,
        4.5826, 4.4721, 4.5826, 4.8990, 4.5826, 4.7958, 4.5826, 4.4721, 4.1231,
        4.8990, 5.0000, 4.2426, 4.8990, 4.7958, 4.8990, 4.5826, 4.5826, 4.2426,
        4.8990, 4.6904, 4.7958, 4.4721, 4.4721, 4.4721, 3.7417, 4.8990, 4.5826,
        4.5826, 3.8730, 4.1231, 4.8990, 4.8990, 4.1231, 4.8990, 4.1231, 4.5826,
        4.7958, 4.4721, 4.8990, 4.8990, 4.8990, 4.8990, 4.2426, 4.5826, 4.8990,
        4.2426, 4.8990, 4.5826, 4.1231, 4.7958, 4.2426, 4.2426, 4.5826, 4.7958,
        4.8990, 4.2426, 4.8990, 4.5826, 4.7958, 4.1231, 4.8990, 4.1231, 4.8990,
        4.7958, 4.4721, 4.7958, 4.1231, 4.1231, 4.1231, 4.7958, 4.5826, 4.5826,
        4.5826, 4.7958, 4.2426, 4.4721, 4.8990, 4.7958, 4.7958, 4.4721, 4.5826,
        4.3589, 3.8730, 4.8990, 4.7958, 4.1231, 4.7958, 4.1231, 4.5826, 4.1231,
        4.4721, 4.4721, 4.2426, 4.2426, 4.5826, 4.8990, 4.4721, 3.7417, 4.1231,
        4.8990, 4.5826, 4.2426, 4.5826, 4.8990, 4.5826, 4.2426, 4.8990, 4.8990,
        4.5826, 4.4721, 4.8990, 4.8990, 3.8730, 4.5826, 4.8990, 4.8990, 4.2426,
        4.2426, 4.5826, 4.2426, 4.3589, 4.7958, 4.4721, 4.3589, 4.8990, 4.7958,
        4.5826, 4.4721, 4.1231, 4.1231, 4.5826, 4.8990, 4.5826, 4.1231, 3.8730,
        4.5826, 4.5826, 4.8990, 4.1231, 4.1231, 4.4721, 4.8990, 4.5826, 4.5826,
        4.5826, 4.4721, 4.2426, 4.4721, 4.7958, 4.8990, 4.1231, 4.4721, 3.8730,
        4.5826, 4.7958, 4.2426, 3.8730, 4.4721, 4.4721, 4.2426, 4.1231, 3.7417,
        4.8990, 4.7958, 4.4721, 4.2426, 4.5826, 4.4721, 4.5826, 4.4721, 4.8990,
        4.1231, 4.7958, 4.2426, 4.4721, 4.8990, 4.5826, 4.2426, 4.5826, 4.4721,
        4.4721, 3.7417, 4.1231, 4.4721, 3.4641, 4.4721, 4.8990, 4.7958, 4.0000,
        4.8990, 4.1231, 3.7417, 3.7417, 3.8730, 4.4721, 4.8990, 4.8990, 4.5826,
        4.5826, 3.7417, 4.5826, 4.1231, 4.0000, 4.2426, 4.5826, 4.8990, 3.8730,
        4.1231, 4.1231, 4.2426, 4.4721, 4.3589, 4.7958, 4.1231, 4.1231, 4.2426,
        4.4721, 4.2426, 4.1231, 4.0000, 4.1231, 4.5826, 4.1231, 4.1231, 4.5826,
        4.2426, 4.8990, 4.5826, 4.2426, 4.1231, 4.3589, 4.1231, 4.4721, 4.4721,
        4.0000, 4.7958, 3.8730, 4.7958, 3.8730, 4.1231, 4.5826, 4.3589, 4.5826,
        4.3589, 4.5826, 4.8990, 4.5826, 4.2426, 4.5826, 4.5826, 4.2426, 4.0000,
        4.7958, 4.3589, 4.5826, 4.7958, 4.2426, 4.3589, 4.8990, 4.7958, 4.7958,
        4.8990, 4.1231, 4.4721, 4.8990, 4.8990, 4.2426, 4.8990, 4.5826, 4.7958,
        4.2426, 4.5826, 4.7958, 4.2426, 4.7958, 4.5826, 4.5826, 4.4721, 4.4721,
        4.1231, 4.2426, 4.3589, 4.5826, 4.4721, 4.6904, 4.6904, 4.3589, 4.5826,
        4.4721, 4.8990, 4.8990, 4.4721, 4.5826, 4.1231, 4.5826, 4.8990, 4.4721,
        4.5826, 4.3589, 4.3589, 4.3589, 3.8730, 4.1231, 4.6904, 4.7958, 4.2426,
        4.4721, 4.5826, 4.8990, 4.7958, 4.3589, 4.5826, 4.8990, 4.1231, 4.3589,
        4.7958, 4.2426, 4.3589, 4.4721, 4.4721, 4.7958, 4.4721, 3.8730, 3.6056,
        4.5826, 4.8990, 4.4721, 4.7958, 4.7958, 4.5826, 4.2426, 4.7958, 4.5826,
        4.7958, 3.6056, 3.6056, 4.1231, 4.5826, 4.4721, 4.5826, 4.7958, 4.1231,
        4.6904, 4.3589, 4.8990, 4.3589, 4.2426, 4.7958, 4.0000, 4.7958, 4.3589,
        4.4721, 4.7958, 4.7958, 3.8730, 4.8990, 4.7958, 4.5826, 3.8730, 4.5826,
        4.7958, 4.7958, 4.8990, 4.1231, 3.8730, 4.5826, 4.1231, 4.2426, 4.1231,
        4.4721, 4.5826, 4.5826, 4.5826, 4.5826, 3.7417, 3.8730, 4.8990, 4.2426,
        3.4641, 4.8990, 4.8990, 4.5826, 3.7417, 4.8990, 4.5826, 4.7958, 4.8990,
        4.0000, 4.2426, 4.2426, 4.8990, 4.6904, 3.6056, 4.3589, 4.7958, 4.2426,
        4.1231, 4.3589, 4.5826, 4.5826, 4.7958, 4.4721, 4.8990, 4.8990, 4.5826,
        4.5826, 4.8990, 4.7958, 4.5826, 4.5826, 4.4721, 4.5826, 4.7958, 4.8990,
        4.8990, 4.8990, 4.8990, 4.5826, 3.8730, 4.7958, 4.4721, 4.8990, 4.8990,
        4.7958, 4.3589, 4.8990, 4.4721, 4.8990, 4.4721, 4.3589, 4.4721, 4.4721,
        4.8990, 4.7958, 4.8990, 4.4721, 4.7958, 4.5826, 4.4721, 4.7958, 4.5826,
        4.4721, 4.5826, 4.1231, 4.7958, 3.7417, 4.2426, 4.1231, 4.2426, 4.2426,
        3.8730, 4.2426, 4.1231, 4.2426, 4.2426, 3.7417, 3.6056, 4.4721, 4.2426,
        4.2426, 4.5826, 4.2426, 4.1231, 4.1231, 3.8730, 4.5826, 4.0000, 4.5826,
        4.7958, 4.2426, 4.5826, 4.8990, 4.6904, 4.8990, 4.4721, 4.8990, 4.5826,
        4.1231, 4.4721, 4.2426, 4.8990, 3.1623, 4.5826, 4.8990, 4.4721, 4.5826,
        4.4721, 4.4721, 4.0000, 4.8990, 4.7958, 4.7958, 4.8990, 4.7958, 4.4721,
        4.2426, 4.2426, 4.2426, 4.4721, 4.1231, 4.5826, 4.2426, 4.4721, 4.4721,
        4.2426, 4.7958, 4.1231, 4.7958, 3.7417, 4.2426, 4.8990, 4.7958, 4.8990,
        4.5826, 4.4721, 4.5826, 4.1231, 4.7958, 4.4721, 4.7958, 4.2426, 3.8730,
        4.8990, 4.0000, 4.1231, 4.2426, 4.1231, 4.1231, 4.2426, 4.5826, 4.1231,
        4.5826, 4.1231, 3.8730, 4.2426, 4.4721, 4.0000, 3.4641, 4.5826, 4.1231,
        4.5826, 3.8730, 4.8990, 4.3589, 4.8990, 3.7417, 4.5826, 4.8990, 4.4721,
        4.8990, 4.1231, 4.7958, 4.5826, 4.0000, 4.8990, 4.4721, 4.5826, 4.5826,
        4.1231, 4.4721, 4.5826, 4.7958, 4.1231, 4.7958, 4.0000, 4.8990, 4.4721,
        4.5826, 4.4721, 4.2426, 4.5826, 3.7417, 5.0000, 4.7958, 4.8990, 4.4721,
        4.5826, 4.1231, 4.4721, 4.8990, 4.5826, 4.8990, 4.1231, 4.2426, 4.8990,
        4.8990, 4.0000, 4.5826, 4.2426, 3.7417, 4.2426, 3.7417, 4.1231, 4.2426,
        4.2426, 4.4721, 4.1231, 4.8990, 4.7958, 4.0000, 4.2426, 4.1231, 4.5826,
        4.5826, 4.8990, 4.4721, 4.5826, 4.8990, 4.5826, 4.3589, 4.7958, 4.4721,
        3.7417, 3.7417, 4.4721, 4.8990, 4.1231, 4.4721, 4.8990, 4.2426, 4.5826,
        4.8990, 4.4721, 4.4721, 4.5826, 4.5826, 4.4721, 3.6056, 4.3589, 4.3589,
        4.5826, 4.2426, 4.1231, 4.5826, 4.3589, 4.4721, 3.7417, 4.5826, 4.7958,
        3.8730, 4.8990, 4.8990, 4.7958, 4.7958, 4.2426, 4.8990, 4.4721, 4.8990,
        4.4721, 4.3589, 4.5826, 4.5826, 4.7958, 4.5826, 4.1231, 3.7417, 4.4721,
        4.1231, 4.5826, 4.8990, 4.5826, 4.1231, 3.8730, 4.5826, 4.0000, 4.1231,
        4.2426, 3.7417, 5.1962, 4.1231, 4.2426, 4.7958, 4.6904, 4.7958, 3.7417,
        4.1231, 5.0000, 3.8730, 4.2426, 4.5826, 4.8990, 3.8730, 4.7958, 3.8730,
        3.8730, 3.8730, 4.0000, 4.4721, 4.4721, 4.1231, 4.2426, 4.7958, 4.7958,
        4.5826, 4.5826, 3.8730, 3.4641, 4.5826, 3.4641, 4.5826, 4.3589, 4.8990,
        3.7417, 4.2426, 3.1623, 3.4641, 4.1231, 4.8990, 5.0000, 4.4721, 4.2426,
        4.4721, 4.2426, 4.4721, 4.4721, 4.8990, 4.8990, 4.8990, 4.5826, 4.2426,
        4.7958, 4.5826, 4.4721, 3.8730, 3.8730, 4.1231, 4.7958, 4.7958, 4.4721,
        3.8730, 4.0000, 4.1231, 4.5826, 4.1231, 4.2426, 4.7958, 4.8990, 4.1231,
        4.2426, 4.4721, 3.3166, 3.4641, 4.5826, 4.2426, 4.5826, 4.8990, 4.2426,
        4.4721, 4.5826, 4.7958, 4.8990, 4.4721, 4.6904, 3.7417, 4.7958, 4.8990,
        4.2426, 3.8730, 4.8990, 4.8990, 4.5826, 4.7958, 4.1231, 4.4721, 4.4721,
        4.7958, 4.1231, 4.2426, 4.2426, 4.2426, 4.4721, 4.1231, 4.4721, 3.8730,
        4.2426, 4.8990, 4.4721, 5.1962, 4.2426, 4.2426, 4.2426, 4.4721, 4.5826,
        4.5826, 4.0000, 4.2426, 4.1231, 4.2426, 4.8990, 3.3166, 4.4721, 4.7958,
        4.8990, 4.5826, 4.0000, 4.8990, 4.2426, 4.2426, 4.1231, 4.7958, 4.3589,
        3.4641, 4.5826, 3.6056, 4.0000, 4.2426, 4.5826, 4.8990, 4.8990, 4.2426,
        3.8730, 4.1231, 4.5826, 4.7958, 4.5826, 3.8730, 3.8730, 4.1231, 4.1231,
        4.1231, 5.1962, 3.3166, 4.2426, 4.5826, 3.3166, 4.2426, 4.2426, 3.8730,
        4.5826, 4.5826, 4.5826, 4.2426, 4.7958, 4.5826, 5.1962, 4.2426, 4.4721,
        4.8990, 4.8990, 4.5826, 4.2426, 4.1231, 4.4721, 4.5826, 4.1231, 4.5826,
        4.1231, 4.4721, 4.7958, 4.4721, 4.8990, 4.8990, 4.7958, 4.0000, 4.1231,
        4.7958, 4.5826, 4.5826, 4.2426, 4.6904, 4.1231, 4.3589, 4.5826, 4.5826,
        4.4721, 4.5826, 3.8730, 4.2426, 4.5826, 4.1231, 3.8730, 4.2426, 4.6904,
        4.7958, 3.8730, 5.2915, 4.7958, 4.4721, 4.7958, 4.4721, 4.5826, 4.8990,
        4.5826, 5.0000, 4.1231, 4.2426, 4.7958, 4.4721, 4.2426, 4.1231, 4.8990,
        3.8730, 4.1231, 4.8990, 4.4721, 4.3589, 4.5826, 4.3589, 4.3589, 3.3166,
        4.2426, 4.8990, 4.5826, 4.1231, 4.1231, 4.0000, 4.1231, 4.2426, 3.8730,
        3.8730, 4.7958, 4.1231, 3.7417, 4.5826, 4.1231, 4.4721, 3.3166, 4.5826,
        4.0000, 4.7958, 3.7417, 5.1962, 4.4721, 4.8990, 4.5826, 4.5826, 4.4721,
        4.2426, 3.8730, 4.1231, 3.3166, 5.0990, 4.2426, 4.6904, 4.4721, 4.4721,
        4.2426, 4.5826, 4.2426, 4.5826, 3.8730, 4.8990, 3.8730, 4.5826, 4.2426,
        4.8990, 4.5826, 4.1231, 4.6904, 3.7417, 4.2426, 4.7958, 4.8990, 3.4641,
        4.4721, 4.4721, 4.2426, 3.7417, 4.4721, 4.1231, 4.8990, 4.8990, 4.4721,
        3.3166, 3.7417, 4.5826, 4.8990, 3.7417, 3.6056, 4.3589, 4.2426, 3.7417,
        4.1231, 4.4721, 4.2426, 4.7958, 4.4721, 4.4721, 3.7417, 3.8730, 4.1231,
        4.5826, 4.1231, 4.8990, 4.2426, 4.4721, 3.4641, 3.8730, 4.3589, 3.7417,
        4.1231, 3.8730, 3.3166, 4.4721, 4.4721, 4.2426, 4.7958, 3.7417, 4.7958,
        3.8730, 4.4721, 3.4641, 4.2426, 4.1231, 4.5826, 3.7417, 4.4721, 4.4721,
        4.3589, 4.8990, 4.4721, 4.2426, 4.1231, 4.3589, 4.2426, 3.7417, 4.8990,
        4.1231, 4.5826, 3.8730, 4.1231, 4.1231, 4.1231, 3.7417, 4.1231, 4.5826,
        4.4721, 4.4721, 4.4721, 3.8730, 4.1231, 4.8990, 4.5826, 4.5826, 4.4721,
        3.8730, 4.5826, 4.1231, 4.0000, 4.1231, 3.8730, 4.1231, 5.0990, 4.4721,
        4.1231, 4.7958, 4.6904, 4.7958, 3.7417, 4.1231, 4.1231, 5.0000, 3.8730,
        4.2426, 4.5826, 4.8990, 4.6904, 3.8730, 3.7417, 4.0000, 3.4641, 4.2426,
        4.2426, 4.8990, 4.7958, 4.5826, 4.5826, 3.8730, 3.8730, 4.8990, 3.8730,
        4.5826, 4.3589, 4.8990, 3.7417, 3.1623, 3.8730, 3.4641, 4.1231, 4.8990,
        5.0000, 4.4721, 4.2426, 4.4721, 4.2426, 4.1231, 4.5826, 5.0000, 4.8990,
        4.8990, 4.5826, 4.5826, 4.7958, 4.5826, 4.4721, 4.2426, 4.5826, 3.7417,
        4.7958, 4.6904, 3.8730, 4.0000, 4.1231, 4.1231, 4.7958, 4.5826, 4.1231,
        4.2426, 4.4721, 4.6904, 4.8990, 4.1231, 4.5826, 4.4721, 3.3166, 4.2426,
        4.5826, 4.8990, 4.8990, 4.2426, 4.4721, 4.5826, 4.8990, 4.6904, 3.7417,
        4.7958, 4.8990, 4.2426, 3.8730, 4.8990, 4.5826, 4.7958, 3.7417, 4.1231,
        4.4721, 4.7958, 4.1231, 4.2426, 4.2426, 4.5826, 4.1231, 4.2426, 4.1231,
        4.2426, 4.2426, 4.5826, 4.4721, 5.1962, 4.2426, 4.2426, 4.2426, 4.2426,
        4.4721, 4.5826, 4.1231, 4.2426, 4.7958, 4.1231, 4.2426, 3.3166, 4.4721,
        4.4721, 4.7958, 4.8990, 5.0990, 4.5826, 4.1231, 4.8990, 4.8990, 4.5826,
        4.2426, 4.8990, 4.5826, 4.4721, 4.4721, 3.8730, 4.5826, 3.4641, 4.7958,
        4.1231, 4.2426, 4.5826, 4.8990, 4.8990, 4.2426, 4.1231, 4.5826, 4.5826,
        3.4641, 3.8730, 4.1231, 4.1231, 5.1962, 3.3166, 4.1231, 4.2426, 4.5826,
        3.4641, 4.5826, 3.8730, 3.8730, 4.5826, 4.4721, 4.5826, 4.2426, 4.7958,
        4.5826, 5.1962, 4.1231, 4.4721, 4.8990, 4.5826, 4.2426, 4.1231, 4.1231,
        4.4721, 4.4721, 4.8990, 4.1231, 4.3589, 4.2426, 4.7958, 4.4721, 4.7958,
        4.1231, 4.4721, 4.7958, 4.4721, 4.5826, 4.5826, 4.4721, 4.0000, 4.2426,
        4.3589, 4.5826, 4.2426, 4.4721, 4.5826, 3.8730, 3.8730, 4.5826, 4.8990,
        4.1231, 3.8730, 4.5826, 4.6904, 3.8730, 5.2915, 4.7958, 4.1231, 4.7958,
        4.4721, 4.2426, 4.8990, 5.0000, 4.1231, 4.2426, 4.4721, 3.8730, 4.8990,
        4.0000, 4.4721, 4.4721, 4.6904, 4.3589, 4.3589, 3.7417, 4.2426, 4.2426,
        4.1231, 4.0000, 4.1231, 4.2426, 3.8730, 3.8730, 4.7958, 4.1231, 3.7417,
        4.1231, 4.1231, 3.4641, 4.5826, 4.3589, 4.2426, 3.7417, 5.1962, 4.4721,
        4.5826, 3.7417, 4.2426, 4.4721, 4.2426, 4.2426, 4.1231, 3.3166, 5.1962,
        4.2426, 4.6904, 4.4721, 4.2426, 4.5826, 4.2426, 4.5826, 3.8730, 4.8990,
        3.8730, 4.2426, 4.8990, 4.2426, 3.3166, 4.6904, 4.1231, 4.1231, 4.7958,
        5.1962, 3.7417, 4.1231, 4.0000, 4.4721, 3.8730, 4.2426, 4.1231, 4.1231,
        3.3166, 4.8990, 3.3166, 3.7417, 4.5826, 4.8990, 3.7417, 3.4641, 4.2426,
        4.1231, 4.4721, 4.1231, 4.1231, 4.3589, 4.2426, 4.4721, 4.7958, 4.2426,
        4.4721, 4.4721, 3.6056, 3.7417, 3.8730, 4.1231, 4.5826, 4.1231, 4.8990,
        4.2426, 4.4721, 3.7417, 4.4721, 3.4641, 4.2426, 4.5826, 3.8730, 4.4721,
        3.7417, 4.1231, 4.4721, 4.4721, 4.2426, 4.7958, 3.7417, 4.4721, 3.8730,
        4.4721, 4.1231, 4.4721, 3.4641, 4.8990, 4.6904, 3.3166, 3.7417, 4.7958,
        4.4721, 4.8990, 4.2426, 4.5826, 4.5826, 4.3589, 4.2426, 4.1231, 4.0000,
        4.2426, 4.8990, 4.1231, 4.5826, 3.7417, 4.0000, 4.1231, 4.2426, 4.1231,
        4.1231, 4.5826, 4.4721, 4.4721, 4.4721, 4.1231, 4.8990, 4.5826, 4.8990,
        4.0000, 4.5826, 4.4721, 4.5826, 4.1231, 4.0000, 4.1231, 4.2426, 4.1231,
        5.0990, 4.1231, 4.2426, 4.7958, 4.6904, 4.3589, 3.7417, 4.1231, 5.0000,
        3.8730, 4.2426, 4.5826, 4.8990, 4.7958, 3.8730, 3.8730, 3.8730, 4.2426,
        4.1231, 4.2426, 5.0000, 4.7958, 4.5826, 3.8730, 3.8730, 4.8990, 3.4641,
        4.5826, 4.3589, 4.8990, 3.7417, 4.4721, 3.8730, 3.3166, 4.0000, 4.2426,
        4.1231, 4.8990, 5.0000, 4.4721, 4.2426, 4.1231, 4.2426, 4.1231, 4.4721,
        5.0000, 4.8990, 4.8990, 4.5826, 4.5826, 4.7958, 4.4721, 4.4721, 3.8730,
        4.5826, 3.7417, 4.7958, 4.6904, 4.2426, 4.0000, 4.5826, 4.1231, 4.2426,
        4.7958, 4.8990, 4.1231, 3.3166, 3.4641, 4.5826, 4.2426, 4.5826, 4.8990,
        4.2426, 4.4721, 4.2426, 4.8990, 4.6904, 3.7417, 4.4721, 4.4721, 4.8990,
        4.2426, 3.8730, 4.8990, 4.7958, 4.5826, 4.7958, 4.1231, 4.4721, 4.4721,
        4.6904, 4.1231, 4.2426, 4.2426, 4.5826, 4.1231, 4.4721, 4.2426, 4.2426,
        4.8990, 4.4721, 5.1962, 4.2426, 3.8730, 4.2426, 4.4721, 4.5826, 4.5826,
        3.3166, 4.0000, 4.2426, 4.1231, 4.2426, 3.3166, 4.7958, 4.6904, 5.0990,
        4.5826, 4.7958, 4.8990, 4.5826, 4.2426, 4.1231, 4.4721, 4.3589, 3.4641,
        4.8990, 4.1231, 4.2426, 4.5826, 4.8990, 4.8990, 3.3166, 4.8990, 4.2426,
        4.2426, 4.5826, 4.5826, 4.2426, 3.8730, 4.1231, 4.1231, 4.6904, 5.1962,
        3.3166, 4.2426, 4.5826, 3.8730, 4.2426, 3.8730, 3.8730, 4.5826, 3.7417,
        4.5826, 4.5826, 4.2426, 4.5826, 5.1962, 4.1231, 4.4721, 4.8990, 4.5826,
        3.7417, 4.1231, 4.4721, 4.5826, 4.5826, 4.1231, 4.3589, 4.7958, 4.4721,
        4.8990, 4.7958, 4.4721, 4.4721, 4.7958, 4.7958, 4.5826, 4.5826, 4.2426,
        4.8990, 4.1231, 4.3589, 4.5826, 4.5826, 4.4721, 4.5826, 3.8730, 4.2426,
        4.5826, 4.2426, 4.1231, 3.8730, 4.5826, 4.6904, 5.2915, 4.7958, 4.1231,
        4.7958, 4.4721, 4.5826, 4.8990, 5.0000, 4.1231, 3.8730, 4.4721, 4.8990,
        4.2426, 4.1231, 4.8990, 4.1231, 4.5826, 4.4721, 4.4721, 3.8730, 4.3589,
        3.1623, 4.3589, 3.3166, 4.5826, 4.1231, 4.1231, 4.1231, 4.2426, 3.8730,
        3.8730, 4.7958, 4.1231, 4.1231, 4.5826, 3.4641, 4.5826, 4.0000, 3.8730,
        4.7958, 4.4721, 4.1231, 5.1962, 4.4721, 4.5826, 4.5826, 3.7417, 3.7417,
        3.3166, 4.0000, 3.7417, 4.2426, 3.8730, 4.2426, 3.8730, 3.4641, 4.2426,
        4.4721, 3.8730, 3.3166, 4.2426, 4.0000, 4.5826, 4.4721, 4.8990, 4.2426,
        3.4641, 3.6056, 4.0000, 4.5826, 3.7417, 3.7417, 4.1231, 3.8730, 4.5826,
        4.1231, 4.4721, 3.6056, 4.8990, 3.7417, 4.2426, 4.1231, 4.2426, 4.7958,
        3.0000, 4.2426, 4.1231, 4.1231, 4.0000, 3.8730, 4.4721, 3.6056, 3.7417,
        3.4641, 3.8730, 4.2426, 4.5826, 4.3589, 4.4721, 4.2426, 4.4721, 3.7417,
        4.1231, 4.2426, 3.8730, 4.2426, 3.3166, 4.1231, 4.0000, 3.7417, 3.7417,
        4.4721, 3.3166, 4.2426, 3.3166, 4.5826, 4.1231, 3.3166, 3.3166, 4.5826,
        2.8284, 4.4721, 3.4641, 3.3166, 3.7417, 3.7417, 3.6056, 4.4721, 3.6056,
        4.1231, 3.6056, 4.5826, 3.7417, 3.8730, 3.7417, 4.1231, 4.5826, 3.6056,
        4.1231, 4.0000, 4.4721, 4.1231, 3.3166, 4.0000, 4.2426, 4.1231, 4.0000,
        4.7958, 4.8990, 3.7417, 4.2426, 4.2426, 4.4721, 4.2426, 4.2426, 3.8730,
        3.8730, 3.7417, 4.1231, 4.4721, 4.5826, 4.2426, 4.8990, 3.7417, 4.1231,
        4.8990, 4.4721, 3.8730, 4.5826, 3.4641, 3.4641, 4.2426, 4.0000, 3.8730,
        2.8284, 3.8730, 4.0000, 4.2426, 4.1231, 4.2426, 3.4641, 3.4641, 4.1231,
        4.2426, 3.7417, 4.2426, 4.2426, 4.5826, 3.1623, 4.4721, 3.7417, 4.1231,
        4.0000, 4.5826, 3.7417, 4.0000], device='cuda:0')
</code></pre>

username: ptrblck
cooked: <aside class="quote no-group" data-username="ShengweiAn" data-post="1" data-topic="74305">
<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/shengweian/48/23548_2.png" class="avatar"> ShengweiAn:</div>
<blockquote>
<p>In my option, if <code>sorted=False</code> , then the returned indices should be sorted, that is the elements in the indices are ascending.</p>
</blockquote>
</aside>
<p>I’m not sure I can follow your assumption.<br>
Why would you expect the output to be sorted, when you specify <code>sorted=False</code>?<br>
Are you seeing the expected result using <code>sorted=True</code>?</p>

username: ShengweiAn
cooked: <p>Thank you for your reply.<br>
I mean if <code>sorted=False</code>, then the returned elements should keep their original order in the tensor.<br>
Take the following codes as an example:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; a = torch.tensor([5., 4, 3, 2, 1, 0])
&gt;&gt;&gt; a
tensor([5., 4., 3., 2., 1., 0.])
&gt;&gt;&gt; torch.topk(a, 3, largest=False, sorted=False)
torch.return_types.topk(
values=tensor([0., 1., 2.]), 
indices=tensor([5, 4, 3]))  # since sorted=False, I think the indices should be [3, 4, 5]
&gt;&gt;&gt; torch.topk(a, 3, largest=False, sorted=True)
torch.return_types.topk(
values=tensor([0., 1., 2.]),
indices=tensor([5, 4, 3]))
&gt;&gt;&gt;
</code></pre>
<p>When I use <code>sorted=True</code>, the result is correct as expected.</p>

username: ShengweiAn
cooked: <p>I think I didn’t explain the problem clearly.<br>
When I use <code>sorted=False</code>, I would like to see the returned indices are sorted, not the returned value.<br>
I hope my previous reply is a bit more clearer.</p>

username: ptrblck
cooked: <p><code>sorted</code> defines, if the values should be returned in a sorted way or not.<br>
It does not determine the order of the indices.</p>
<p>If you want to sort the indices, you could apply <code>torch.sort</code> on them after the <code>topk</code> call.</p>

username: ShengweiAn
cooked: <p>Thank you for your reply.<br>
I understood the definition of  <code>sorted</code> .</p>
<p>What confuses me, is why the indices are not in the original order by default (when not sorting the values).<br>
Are there any design reasons? Such as the non-determinism incurred by the parallelism.<br>
But rerunning  <code>topk</code>  for servel times gave same results.</p>

username: ptrblck
cooked: <p>If you don’t specify <code>sorted=True</code>, there is no guarantee of any order (sorted or original order).</p>
<aside class="quote no-group" data-username="ShengweiAn" data-post="8" data-topic="74305">
<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/shengweian/48/23548_2.png" class="avatar"> ShengweiAn:</div>
<blockquote>
<p>Such as the non-determinism incurred by the parallelism.<br>
But rerunning <code>topk</code> for servel times gave same results.</p>
</blockquote>
</aside>
<p>Yes, that’s most likely the reason at least for GPU runs. I’m not sure, how this method is implemented on the CPU.</p>

username: ShengweiAn
cooked: <p>I see. Thank you very much for your patience and help.</p>

username: KFrank
cooked: <p>Hello Shengwei!</p>
<aside class="quote no-group" data-username="ShengweiAn" data-post="8" data-topic="74305" data-full="true">
<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/shengweian/48/23548_2.png" class="avatar"> ShengweiAn:</div>
<blockquote>
<p>What confuses me, is why the indices are not in the original order by default (when not sorting the values).</p>
</blockquote>
</aside>
<p>I wouldn’t consider this a bug, per se, because <code>torch.topk()</code><br>
doesn’t make any promises about the order it returns (when<br>
<code>sorted = False</code>).</p>
<p>I do think this could be a legitimate feature request.  There is<br>
value in having a known return order, and the original order<br>
(which corresponds to sorted indices) would seem to be the<br>
natural choice.</p>
<p>I don’t know what the <code>topk()</code> code looks like, so I don’t know<br>
how hard it would be to do, but if it weren’t too hard, <em>and didn’t<br>
significantly degrade the performance,</em> I would think that having<br>
<code>topk()</code> return the original (sorted-indices) order would be a<br>
worthwhile improvement.  (I also think that for <code>sorted = True</code><br>
sets of indices <em>for which the values are equal</em> should have<br>
the order of the indices within the set retained.)</p>
<p>(On the other hand, there are undoubtedly more important<br>
bug fixes / feature requests out there in the queue.)</p>
<p>(By analogy, one might compare <em>merge sort</em> and <em>heapsort.</em><br>
Marge sort is a stable sort, that is, when values test equal, they<br>
are returned in their original order, while heapsort is not stable,<br>
but can have performance advantages.  If there were no loss<br>
in performance, it would make sense to always use the stable<br>
sort.)</p>
<p>Best.</p>
<p>K. Frank</p>

username: ionut
cooked: <p>Hi guys! I am also interested in this topic. I would have also expected that the indices are sorted when we set <code>sorted=True</code>.</p>
<p>I also experimented with this flag and I got the following behavior. For the vector <code>v</code> that I create, the maximum values are 7, 9, 6 which appear at indices 3, 5 and 7. When I set the flag sorted to True and False I get the same result on CPU, but when I create <code>v</code> on the GPU, I get a different result. Can you guys help me figure out what happens here?</p>
<p>CPU version:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; v = torch.tensor([5, 2, 3,  7, 1,  9, 0,  6, 4], dtype=torch.float, device='cpu')

&gt;&gt;&gt; torch.topk(v, k=3, sorted=False)
torch.return_types.topk(
values=tensor([9., 7., 6.]),
indices=tensor([5, 3, 7]))

&gt;&gt;&gt; torch.topk(v, k=3, sorted=True)
torch.return_types.topk(
values=tensor([9., 7., 6.]),
indices=tensor([5, 3, 7]))
</code></pre>
<p>GPU version:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; v = torch.tensor([5, 2, 3,  7, 1,  9, 0,  6, 4], dtype=torch.float, device='cuda:0')

&gt;&gt;&gt; torch.topk(v, k=3, sorted=False)
torch.return_types.topk(
values=tensor([7., 9., 6.], device='cuda:0'),
indices=tensor([3, 5, 7], device='cuda:0'))

&gt;&gt;&gt; torch.topk(v, k=3, sorted=True)
torch.return_types.topk(
values=tensor([9., 7., 6.], device='cuda:0'),
indices=tensor([5, 3, 7], device='cuda:0'))
</code></pre>