question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add sparse tensor support to cp.ElementwiseKernel

See original GitHub issue

Description

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:open
  • Created a year ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
leofangcommented, Oct 14, 2022

I modified my example above as I see what @takagi had in mind. The old example x-y needs great care because the sparse matrices x and y may not share the same indices for their nonzeros, meaning you can’t just naively run the x-y ElementwiseKernel on x and y’s data.

0reactions
leofangcommented, Oct 14, 2022

Perhaps you can just call arr.eliminate_zeros() after ElementwiseKernel.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found