Skip to main content

Sparse array containers and primitives for MLX

Project description

mlx-sparse

PyPI License Documentation Status codecov

[!WARNING] This is an early beta. It currently supports only Apple systems. 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 the solver support matrix).

[!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, CSR, and CSC for 2D sparse arrays. Current functionality includes construction, validation, format conversions, canonicalization, sparse-dense products, batched dense RHS products, sparse-sparse products, transpose, and Hermitian transpose. mlx-sparse also supports sparse linalg solvers (cg, gmres, minres), reusable direct solves (factorized), 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

Published macOS wheels are built with optional Apple Accelerate sparse direct solver support enabled. For editable/source builds, enable the same path with:

CMAKE_ARGS="-DMLX_SPARSE_ENABLE_ACCELERATE=ON" python -m pip install -e .

See the full installation guide for platform requirements, feature gates, and verification commands.

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.4b0.tar.gz (435.6 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.4b0-cp313-cp313-macosx_14_0_arm64.whl (2.9 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

mlx_sparse-0.0.4b0-cp312-cp312-macosx_14_0_arm64.whl (2.9 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

mlx_sparse-0.0.4b0-cp311-cp311-macosx_14_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

mlx_sparse-0.0.4b0-cp310-cp310-macosx_14_0_arm64.whl (3.0 MB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

File details

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

File metadata

  • Download URL: mlx_sparse-0.0.4b0.tar.gz
  • Upload date:
  • Size: 435.6 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.4b0.tar.gz
Algorithm Hash digest
SHA256 770f73b4bb09bee1a92148359efe371ba82f57ade58b925d93af418392cb8dd6
MD5 1a54af2da8750833fdebafc76aeba9b2
BLAKE2b-256 a606622593d57d487b40277bcaaaae4526223c4895fe79237f78da4dd8c41d70

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.4b0.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.4b0-cp313-cp313-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b0-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 e8b0a2c1258cd448bba6c54211caf6dc770657ed53d4115ee8a024b614bce087
MD5 b8e7697eebab66e63de76fe8f6cc960e
BLAKE2b-256 d02902e349f2118e41e3d9cf8782f82edf269c9b4a5718d34d23ed596cea7396

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.4b0-cp313-cp313-macosx_14_0_arm64.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.4b0-cp312-cp312-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b0-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 04998acd56a2b45c47b28033250241481a5d548993d01b3045658516d21bfc9b
MD5 a318d4793ff7bbcd838faa78045cbec4
BLAKE2b-256 35edb8d3d632b3fba1257378020316f70040b0763f3c14a19b2793503ebb0db2

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.4b0-cp312-cp312-macosx_14_0_arm64.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.4b0-cp311-cp311-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b0-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 92cdcc26a6338da0505f50d022bfbce5dbc0ad111a668eac24857049be36d4d2
MD5 408fd1a1f0f4a89af427111af27bf108
BLAKE2b-256 06da98265d1840816dd2d4d37573d78463d9e97b8986b2ac5a1eb94dc68ce42d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.4b0-cp311-cp311-macosx_14_0_arm64.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.4b0-cp310-cp310-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b0-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 3ab6f42fbd4475fbb10d8d7bca77c962e1302e244e345648884699c5a78d788f
MD5 f800c2b0178c2303ffe7077fb8368882
BLAKE2b-256 a86db1ed494de7deccfda871b6dfb69550efe1e76ff1a4a7c8139aae0ea74b86

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.4b0-cp310-cp310-macosx_14_0_arm64.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