'Comparing two sparse matrices fails with Not Implemented error
I have recently started programming in Pytorch for a university project. I wanted to compare if two sparse matrices are equal but it returned a not implemented error. I tried looking for the solution on the internet but didnt find any clues.
torch.equal(matrix_1, matrix_2)
{NotImplementedError}Could not run 'aten::equal' with arguments from the 'SparseCUDA' backend. This could be because the operator doesn't exist for this backend, or was omitted during the selective/custom build process (if using custom build). If you are a Facebook employee using PyTorch on mobile, please visit https://fburl.com/ptmfixes for possible resolutions. 'aten::equal' is only available for these backends: [CPU, CUDA, QuantizedCPU, BackendSelect, Python, Named, Conjugate, Negative, ZeroTensor, ADInplaceOrView, AutogradOther, AutogradCPU, AutogradCUDA, AutogradXLA, AutogradLazy, AutogradXPU, AutogradMLC, AutogradHPU, AutogradNestedTensor, AutogradPrivateUse1, AutogradPrivateUse2, AutogradPrivateUse3, Tracer, AutocastCPU, Autocast, Batched, VmapMode, Functionalize].
CPU: registered at C:\actions-runner_work\pytorch\pytorch\builder\windows\pytorch\build\aten\src\ATen\RegisterCPU.cpp:21063 [kernel] CUDA: registered at C:\actions-runner_work\pytorch\pytorch\builder\windows\pytorch\build\aten\src\ATen...
Solution 1:[1]
I can't address what torch is doing, or not doing, but comparison tests on sparse matrices can be tricky - at least computationally expensive.
For example using scipy.sparse:
In [96]: M = sparse.coo_matrix(np.eye(3))
In [97]: M
Out[97]:
<3x3 sparse matrix of type '<class 'numpy.float64'>'
with 3 stored elements in COOrdinate format>
In [98]: M.A
Out[98]:
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Attempting an element-wise comparison:
In [99]: M == M
<ipython-input-99-80b3ae713acc>:1: SparseEfficiencyWarning: Comparing sparse matrices using == is inefficient, try using != instead.
M == M
Out[99]:
<3x3 sparse matrix of type '<class 'numpy.bool_'>'
with 9 stored elements in Compressed Sparse Row format>
In [100]: _.A
Out[100]:
array([[ True, True, True],
[ True, True, True],
[ True, True, True]])
Note that the result if 'full' - all 9 values are set. All those implied 0's test as equal.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hpaulj |
