Add sparse tensor support to cp.ElementwiseKernel
See original GitHub issueDescription
Hello, I would appreciate if one could use the cp.ElementwiseKernel
(and all the other custom kernels) also for sparse tensors.
Additional Information
Example:
import cupy
import cupyx
# Coo Matrix
data = cupy.array([1, 1, 1, 1, 1, 1], 'f')
row = cupy.array([0, 1, 1, 2, 2, 2], 'i')
col = cupy.array([0, 0, 1, 0, 1, 2], 'i')
A = cupyx.scipy.sparse.coo_matrix((data, (row, col)), shape=(3, 3),dtype=cupy.float32 )
B = cupyx.scipy.sparse.csr_matrix((data, (row, col)), shape=(3, 3) ,dtype=cupy.float32)
C = cupyx.scipy.sparse.csc_matrix((data, (row, col)), shape=(3, 3) ,dtype=cupy.float32)
squared_diff = cp.ElementwiseKernel(
'float32 x, float32 y',
'float32 z',
'z = (x - y) * (x - y)',
'squared_diff')
# Elementwise Kernel for Sparse Matrices.
# Same for A,A ; B,B; C,C
squared_diff(C,C)
# (C-C)*(C-C) works work.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
cupy.ElementwiseKernel — CuPy 11.4.0 documentation
This class can be used to define an elementwise kernel with or without broadcasting. The kernel is compiled at an invocation of the...
Read more >CuPy Documentation - Read the Docs
CuPy provides a ndarray, sparse matrices, and the associated routines for ... Profiler: Supports profiling code using CUDA Profiler and NVTX.
Read more >cupy/community - Gitter
CUDA 11.1 support has been added so now you can use CuPy with your GeForce ... currently we have no plan to add...
Read more >CuPy - Nvidia
import cupy as cp x_gpu = cp.zeros((10,)) ... Support both CPU and GPU with the same code! ... From NumPy. – Sparse Matrix,...
Read more >How to use the cupy.asnumpy function in cupy - Snyk
To help you get started, we've selected a few cupy.asnumpy examples, ... if isinstance(tensor, cp.ndarray): return cp.asnumpy(tensor) return tensor.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I modified my example above as I see what @takagi had in mind. The old example
x-y
needs great care because the sparse matricesx
andy
may not share the same indices for their nonzeros, meaning you can’t just naively run thex-y
ElementwiseKernel onx
andy
’s data.Perhaps you can just call
arr.eliminate_zeros()
afterElementwiseKernel
.