Skip to main content

Sparse array containers and primitives for MLX

Project description

mlx-sparse

PyPI License Documentation Status codecov

Warning: beta release This is an early beta. APIs may change, bugs are expected, and some features are still incomplete, especially GPU kernels for various operations. Feedback and issue reports are very welcome!

A lot of the functionality is new and is currently being tested. We welcome any and all feedback! Not all solvers are GPU supported (see here).

Platform note GPU support in this version is Apple Silicon Metal only. CUDA is not currently supported.

mlx-sparse is an attempt at an MLX-native sparse array package. The public API is Python, while performance-critical operations are implemented as MLX primitives in C++ with CPU backends and Metal kernels for fixed-shape sparse operations.

The supported format surface is COO and CSR for 2D sparse arrays. Current functionality includes construction, validation, COO to CSR, CSR to dense, CSR canonicalization, CSR matrix-vector multiply, CSR matrix-matrix multiply, batched dense RHS products, CSR sparse-sparse products, transpose, Hermitian transpose. mlx-sparse also supports sparse linalg solvers (cg, gmres, minres), sparse spectral routines (eigsh, eigs, svds), sparse Cholesky/LU factors, sparse triangular solves, sparse dot/vdot, and autodiff through sparse values and dense RHS operands, including complex64.

Supported value dtypes are float32, float16, bfloat16, and complex64. Supported index dtypes are int32 and int64 on CPU and GPU.

Quick Start

Install from PyPI:

python -m pip install mlx-sparse
import mlx.core as mx
import numpy as np

import mlx_sparse as ms

ms.use_gpu()

data = [2.0, -1.0, 4.0]
row = [0, 0, 1]
col = [0, 2, 1]

a = ms.coo_array((data, (row, col)), shape=(2, 3)).tocsr(canonical=True)
x = mx.array(np.array([3.0, 10.0, 7.0], dtype=np.float32))

y = a @ x
dense = a.todense()
at = a.T

b = mx.array(np.array([1.0, 2.0], dtype=np.float32))
spd = ms.csr_array(
    (
        [4.0, 1.0, 1.0, 3.0],
        [0, 1, 0, 1],
        [0, 2, 4],
    ),
    shape=(2, 2),
    canonical=True,
)
solution, info = ms.linalg.cg(spd, b)
factor = ms.linalg.sparse_cholesky(spd)

The package build compiles src/sparse/*.metal into mlx_sparse/mlx_sparse.metallib when the macOS Metal toolchain is available, and the wheel ships that metallib beside the Python package.

Development

For contributors, use an editable install from the repository root. This builds the native extension and installs the development tooling.

python -m pip install -e ".[dev]"

License

This package is licensed under the Apache License 2.0.

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

mlx_sparse-0.0.3b0.tar.gz (373.2 kB view details)

Uploaded Source

Built Distributions

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

mlx_sparse-0.0.3b0-cp313-cp313-macosx_14_0_universal2.whl (2.4 MB view details)

Uploaded CPython 3.13macOS 14.0+ universal2 (ARM64, x86-64)

mlx_sparse-0.0.3b0-cp312-cp312-macosx_14_0_universal2.whl (2.4 MB view details)

Uploaded CPython 3.12macOS 14.0+ universal2 (ARM64, x86-64)

mlx_sparse-0.0.3b0-cp311-cp311-macosx_14_0_universal2.whl (2.4 MB view details)

Uploaded CPython 3.11macOS 14.0+ universal2 (ARM64, x86-64)

mlx_sparse-0.0.3b0-cp310-cp310-macosx_14_0_universal2.whl (2.4 MB view details)

Uploaded CPython 3.10macOS 14.0+ universal2 (ARM64, x86-64)

File details

Details for the file mlx_sparse-0.0.3b0.tar.gz.

File metadata

  • Download URL: mlx_sparse-0.0.3b0.tar.gz
  • Upload date:
  • Size: 373.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mlx_sparse-0.0.3b0.tar.gz
Algorithm Hash digest
SHA256 b2ad87229977d741fe99892f32ec0310b649cc76ed6a591417cd0d4494687d01
MD5 a93b23dd0b6d1766c366bce4c5a7a08b
BLAKE2b-256 c5145ccba34c7271646b930a4e1824d668364954537d3bfdeea7c660622f57e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.3b0.tar.gz:

Publisher: publish.yaml on waleed-sh/mlx-sparse

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

File details

Details for the file mlx_sparse-0.0.3b0-cp313-cp313-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.3b0-cp313-cp313-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 d52662d0d0ccce1dac72743075dc9b6d3d181c43faf84d01a8be93191dd390a8
MD5 f0dec95ed183a94e6bfc6dbbd80146f3
BLAKE2b-256 dfaa9b685a2bfc125fcd5173dc56ef9cb55372175e70f6c9dce73db3fa1fb10d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.3b0-cp313-cp313-macosx_14_0_universal2.whl:

Publisher: publish.yaml on waleed-sh/mlx-sparse

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

File details

Details for the file mlx_sparse-0.0.3b0-cp312-cp312-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.3b0-cp312-cp312-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 07ff657941cf0d46f79d0448599926bd3cf53bcef44aa84852e2d987bd85538d
MD5 66bd8f962e80bb9dfbe7b987095713ca
BLAKE2b-256 7af12d21d85c48238df9316e9587c07d4e96b9e399257611b1b80e39a394f867

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.3b0-cp312-cp312-macosx_14_0_universal2.whl:

Publisher: publish.yaml on waleed-sh/mlx-sparse

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

File details

Details for the file mlx_sparse-0.0.3b0-cp311-cp311-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.3b0-cp311-cp311-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 d4238737821d764606b3a354322761014f0a7ee18151326539223d3a95e58bfd
MD5 8cb1a05918428eba3410af9d5f6f787e
BLAKE2b-256 0e795e5dd43433cca8976c0ab2117ca29081f053dd5dbe6394e2d188b0840661

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.3b0-cp311-cp311-macosx_14_0_universal2.whl:

Publisher: publish.yaml on waleed-sh/mlx-sparse

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

File details

Details for the file mlx_sparse-0.0.3b0-cp310-cp310-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.3b0-cp310-cp310-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 77c355a420ba1cc35cdd83888ba679c650579ceba4a07519ec93dcb20c5536b3
MD5 d3c03ff3fd645334cc31b170eb766120
BLAKE2b-256 2ce62fb0a543f39e0a6311875e18eef355f7f7b494073d1a6e735166fd75a444

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.3b0-cp310-cp310-macosx_14_0_universal2.whl:

Publisher: publish.yaml on waleed-sh/mlx-sparse

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