rusty1s / pytorch_sparse

PyTorch Extension Library of Optimized Autograd Sparse Matrix Operations
MIT License
1.01k stars 147 forks source link

fix(`SparseTensor.__getitem__`): support `np.ndarray` and fix `List[b… #194

Closed bwdeng20 closed 2 years ago

bwdeng20 commented 2 years ago

The following snippet can reproduce errors this pull request aims to eliminate.

import torch
from torch_sparse import SparseTensor
def index1(spm,key):
    npm=spm[:, key]
    print(get_dense(npm.to_dense()))
    return npm

n=5
a=torch.arange(n*n).reshape(n,n)
tsa=SparseTensor.from_dense(a)

# index-based
Slist=[0,2,1]
Snp=np.array(Slist,copy=True)

# bool-based
Dlist=[False]*n
for i in Slist:
    Dlist[i]=True
Dnp=np.array(Dlist,copy=True)

# uncomment to check error
# index1(tsa,Snp) # ambiguous error
# index1(tsa,Dlist) # wrong result
codecov-commenter commented 2 years ago

Codecov Report

Merging #194 (cd434f7) into master (efc9808) will increase coverage by 3.09%. The diff coverage is 100.00%.

:exclamation: Current head cd434f7 differs from pull request most recent head 9a56fb4. Consider uploading reports for the commit 9a56fb4 to get more accurate results Impacted file tree graph

@@            Coverage Diff             @@
##           master     #194      +/-   ##
==========================================
+ Coverage   69.20%   72.29%   +3.09%     
==========================================
  Files          28       28              
  Lines        1117     1119       +2     
==========================================
+ Hits          773      809      +36     
+ Misses        344      310      -34     
Impacted Files Coverage Δ
torch_sparse/tensor.py 48.71% <100.00%> (+2.63%) :arrow_up:
torch_sparse/masked_select.py 77.35% <0.00%> (+60.37%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update efc9808...9a56fb4. Read the comment docs.