You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
deftorch_lexsort(keys, dim=-1):
iflen(keys) <2:
raiseValueError(f"keys must be at least 2 sequences, but {len(keys)=}.")
idx=keys[0].argsort(dim=dim, stable=True)
forkinkeys[1:]:
idx=idx.gather(dim, k.gather(dim, idx).argsort(dim=dim, stable=True))
returnidx
I also ran some tests on the proposed implementation as follows:
In [1]: importtorch; importnumpyasnpIn [2]: N=1000000In [3]: a=np.random.rand(N); b=np.random.randint(N//4, size=N)
In [4]: a_t=torch.tensor(a); b_t=torch.tensor(b)
In [5]: a_t_cu, b_t_cu=a_t.to(torch.device("cuda")), b_t.to(torch.device("cuda"))
In [6]: deftorch_lexsort(keys, dim=-1):
...: # defined as aboveIn [7]: %timeit-n2-r20np.lexsort([a, b])
302ms ± 31.2msperloop (mean ± std. dev. of20runs, 2loopseach)
In [8]: %timeit-n2-r20torch_lexsort([a_t, b_t])
293ms ± 35.1msperloop (mean ± std. dev. of20runs, 2loopseach)
In [9]: %timeit-n20-r100torch_lexsort([a_t_cu, b_t_cu])
Theslowestruntook5.27timeslongerthanthefastest. Thiscouldmeanthatanintermediateresultisbeingcached.
3.97ms ± 334µsperloop (mean ± std. dev. of100runs, 20loopseach)
In [10]: idx_np=np.lexsort([a, b]); idx_pt=torch_lexsort([a_t, b_t])
In [11]: (idx_np==idx_pt.numpy()).all()
Out[11]: True
Seems it helps with GPU support as well.
Thought it could replace the current implementation which detaches and recasts the tensor.
Suggest a potential alternative/fix
Mentioned Above
The text was updated successfully, but these errors were encountered:
🛠 Proposed Refactor
Hi,
I came upon this lexsort implementation in the
sort_csc
function in the following line:pytorch_geometric/torch_geometric/sampler/utils.py
Line 27 in 9c79ce8
And looks like an unofficial PyTorch implementation is proposed here:
https://discuss.pytorch.org/t/numpy-lexsort-equivalent-in-pytorch/47850/5?u=ebrahim.pichka
As below (with minor changes):
I also ran some tests on the proposed implementation as follows:
Seems it helps with GPU support as well.
Thought it could replace the current implementation which detaches and recasts the tensor.
Suggest a potential alternative/fix
Mentioned Above
The text was updated successfully, but these errors were encountered: