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)

Methods

The Toeplitz_convolution2d class supports two backend methods via the method parameter:

  • method='lazy' (default): Computes convolution on-the-fly via sparse COO broadcasting. Instant initialization and low memory usage. Cost scales with nnz(x) * nnz(k), so it is best for sparse inputs (density < ~0.1).
  • method='precomputed': Builds a sparse double-block Toeplitz matrix at init time and uses sparse matmul per call. Per-call cost is largely density-independent. Better for dense inputs (density > ~0.5) or large batches where the one-time build cost is amortized.

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.2.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sparse_convolution-0.2.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sparse_convolution-0.2.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sparse_convolution-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1c03ee0b8a9b4780e82a701dff1e20038356f262b1b996843e04b441ffa87a42
MD5 b3cc4dd62ffa09e14267be4d3e0fdc73
BLAKE2b-256 1e4d4c51c1d8c17726a98ff59f37e8c2482ce52f9fbd8aba5ed2779af1d5d70f

See more details on using hashes here.

Provenance

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

Publisher: pypi_release.yml on RichieHakim/sparse_convolution

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for sparse_convolution-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0410f4559dd2de4b7c9934a2e9133fd3f02068a41f4d17291d9a30d681adc3e
MD5 3cf960bc3b3fcca9b23c8a5aa7f4f730
BLAKE2b-256 e207af16ea8966f7ad6cc3fcf1b51b0af70f935e8f31e7b6affbb7f8394643e9

See more details on using hashes here.

Provenance

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

Publisher: pypi_release.yml on RichieHakim/sparse_convolution

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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