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.4b1.tar.gz (903.5 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.4b1-cp313-cp313-macosx_14_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.13macOS 14.0+ ARM64

mlx_sparse-0.0.4b1-cp312-cp312-macosx_14_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.12macOS 14.0+ ARM64

mlx_sparse-0.0.4b1-cp311-cp311-macosx_14_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.11macOS 14.0+ ARM64

mlx_sparse-0.0.4b1-cp310-cp310-macosx_14_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.10macOS 14.0+ ARM64

File details

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

File metadata

  • Download URL: mlx_sparse-0.0.4b1.tar.gz
  • Upload date:
  • Size: 903.5 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.4b1.tar.gz
Algorithm Hash digest
SHA256 26d8b37f811fe9578a880122b41a5e96323d91cfbc313c75ec9a9417556aa253
MD5 a7be973a900836fb7ced5c8d82991c97
BLAKE2b-256 2aed767821d367052e281a6b430c0455d48a299cc52e58ee651d8c2ba423c46a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b1-cp313-cp313-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 02a23a0478bede51da683bec6c850ea6fe96afb35a0ab11453269111b43c6586
MD5 af7b22906146e464272181a53b742b96
BLAKE2b-256 544276162e155656b02ac84838015adfdf211a73a3be37283bae89aaef52869b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b1-cp312-cp312-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 d1625ecd7375920273a12a5a947446f7733e87813e38a7c75180b36b50431d24
MD5 a1eba5766e219d93ddba646d6a1c23c9
BLAKE2b-256 7ff8b5afd7a7324bdcf0620c2c657d4d9cc2e0ee945a8348ed3262cb9cef26da

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b1-cp311-cp311-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 faf737524efb22185a96d30c09053672190ceb710931b1c943243c4157dbd8bc
MD5 6a1343b7f693134c2e3571d5f55e78aa
BLAKE2b-256 b4ceca327f8debe8c9b057413257737be518ee793e8c7ab3785d630286faa833

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for mlx_sparse-0.0.4b1-cp310-cp310-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 00cdb17ded0ce10ec7e3071f351510da4360455f50df8bb2e30947d486ce07ee
MD5 a8f3b02c64e2698e7f13ee64b6229bd1
BLAKE2b-256 8dd3a4f37b9241101612d60b16c1e7cc1168c5223f8c9ded2d6cbfa1bcf16f2b

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.4b1-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