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. Feedback and issue reports are very welcome!

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.

Warning: A lot of the linalg functionality is new and is currently being tested. We welcome any and all feedback!

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 = mx.array(np.array([2.0, -1.0, 4.0], dtype=np.float32))
row = mx.array(np.array([0, 0, 1], dtype=np.int32))
col = mx.array(np.array([0, 2, 1], dtype=np.int32))

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(
    (
        mx.array([4.0, 1.0, 1.0, 3.0], dtype=mx.float32),
        mx.array([0, 1, 0, 1], dtype=mx.int32),
        mx.array([0, 2, 4], dtype=mx.int32),
    ),
    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.2b0.tar.gz (174.3 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.2b0-cp313-cp313-macosx_14_0_universal2.whl (670.2 kB view details)

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

mlx_sparse-0.0.2b0-cp312-cp312-macosx_14_0_universal2.whl (670.2 kB view details)

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

mlx_sparse-0.0.2b0-cp311-cp311-macosx_14_0_universal2.whl (670.5 kB view details)

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

mlx_sparse-0.0.2b0-cp310-cp310-macosx_14_0_universal2.whl (670.5 kB view details)

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

File details

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

File metadata

  • Download URL: mlx_sparse-0.0.2b0.tar.gz
  • Upload date:
  • Size: 174.3 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.2b0.tar.gz
Algorithm Hash digest
SHA256 6c5aade9827560078d5e771fdb5392b55cfef9b1d1b45a3afe4038bc8a133594
MD5 6a35872feed819c29816562ebf09a429
BLAKE2b-256 609c93c13a206ff4ebaba2ead4043a0cb7de6c35c697a43b2cbc5684b06cf919

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.2b0.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.2b0-cp313-cp313-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.2b0-cp313-cp313-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 05c958cb2e4dfcbd5bc3af41f1b56d6949d31612ea170061c9894f5aa9d0d001
MD5 d48e800a4632438674d1ee091b9d6c57
BLAKE2b-256 0b35cfd7ec6d6b905cfb624d00c45fdff2248134c1db58d7c0160bbbc8201635

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.2b0-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.2b0-cp312-cp312-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.2b0-cp312-cp312-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 734387e505d268455c0a12c7d450d035195f3e444e62f9af7d892db3ca59eaaa
MD5 f9c69021f5ea3fa1e112a59f6dba5d3e
BLAKE2b-256 6e40fd1bd3b259c5ec2ab5e0405357563f431845e3fb3e320e5ff53e034d191c

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.2b0-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.2b0-cp311-cp311-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.2b0-cp311-cp311-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 04e9bfa840478e0033490f4e78e4607a36d2a7d912ca84ed353fc6c8e10a000a
MD5 e906392c9bb0c6b2c44832e49a619a36
BLAKE2b-256 624d0a0d82f698ce9520715713b7b3b17bdfb7718b2edfa6d2ec61cdd850ef31

See more details on using hashes here.

Provenance

The following attestation bundles were made for mlx_sparse-0.0.2b0-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.2b0-cp310-cp310-macosx_14_0_universal2.whl.

File metadata

File hashes

Hashes for mlx_sparse-0.0.2b0-cp310-cp310-macosx_14_0_universal2.whl
Algorithm Hash digest
SHA256 3486702ce43f3c2e8b98defe1d2e6d52dda3f5affd344e76ae237dd8356ae994
MD5 33655490d0330601677a3d7de99cd7a2
BLAKE2b-256 49151e839e2e691f164537fadbbc0974bac7f4b83643f21ce9c3882ef707cdbf

See more details on using hashes here.

Provenance

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