Skip to main content

Sparse convolution in python using Toeplitz convolution matrix multiplication.

Project description

sparse_convolution

Sparse convolution in python.
Uses Toeplitz convolutional matrix multiplication to perform sparse convolution.
This allows for extremely fast convolution when:

  • The kernel is small (<= 100x100)
  • The input array is sparse (<= 1% density)
  • The input array is small (<= 1000x1000)
  • Many arrays are convolved with the same kernel (large batch size >= 1000)

Install:

The package is available on PyPI.
pip install sparse_convolution


Alternatively, you can install from source.
git clone https://github.com/RichieHakim/sparse_convolution
cd sparse_convolution
pip install -e .

Basic usage:

Convolve a single sparse 2D array with a 2D kernel.

import sparse_convolution as sc
import numpy as np
import scipy.sparse

# Create a single sparse matrix
A = scipy.sparse.rand(100, 100, density=0.1)

# Create a dense kernel
B = np.random.rand(3, 3)

# Prepare class
conv = sc.Toeplitz_convolution2d(
    x_shape=A.shape,
    k=B,
    mode='same',
    dtype=np.float32,
)

# Convolve
C = conv(
    x=A,
    batching=False,
).toarray()

Batching usage:

Convolve multiple sparse 2D arrays with a 2D kernel.
The input arrays must be reshaped into flattened vectors and stacked into a single sparse array of shape: (n_arrays, height * width).

import sparse_convolution as sc
import numpy as np
import scipy.sparse

# Create multiple sparse matrices
# note that the shape of A will be (3, 100**2)
A = scipy.sparse.vstack([
    scipy.sparse.rand(100, 100, density=0.1).reshape(1, -1),
    scipy.sparse.rand(100, 100, density=0.1).reshape(1, -1),
    scipy.sparse.rand(100, 100, density=0.1).reshape(1, -1),
]).tocsr()

# Create a dense kernel
B = np.random.rand(3, 3)

# Prepare class
conv = sc.Toeplitz_convolution2d(
    x_shape=(100, 100),  # note that the input shape here is (100, 100)
    k=B,
    mode='same',
    dtype=np.float32,
)

# Convolve
C = conv(
    x=A,
    batching=True,
)

# Reshape the output back to (3, 100, 100)
C_reshaped = np.stack([c.reshape(100, 100).toarray() for c in C], axis=0)

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sparse_convolution-0.1.5.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

sparse_convolution-0.1.5-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file sparse_convolution-0.1.5.tar.gz.

File metadata

  • Download URL: sparse_convolution-0.1.5.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for sparse_convolution-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2ce8d1375d0770a18bce714208225d242be2bd28e9c2e70b79b73be6c239b822
MD5 6eee6b4f714add1b2e6349a80d115a4e
BLAKE2b-256 06dd6249388abfec5dfff27e7eca2290b4c5499aedf4808caac5798f24bb8ade

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_convolution-0.1.5.tar.gz:

Publisher: pypi_release.yml on RichieHakim/sparse_convolution

Attestations:

File details

Details for the file sparse_convolution-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for sparse_convolution-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 d3bc4714a71d539aeed48529b5f0243f06adff0dec5528a7e8e75fa9b231fe79
MD5 c5bd92ed04da621eee4802d0e7d627a5
BLAKE2b-256 c8952dec6a83d935c210e0928c2d127a6d34ea3f57486e7a20e3d1b98cd11cd0

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_convolution-0.1.5-py3-none-any.whl:

Publisher: pypi_release.yml on RichieHakim/sparse_convolution

Attestations:

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page