mlx-sparse 0.0.1b0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. mlx_sparse-0.0.1b0/.github/workflows/ci.yml +49 -0
  2. mlx_sparse-0.0.1b0/.github/workflows/publish.yaml +94 -0
  3. mlx_sparse-0.0.1b0/.gitignore +20 -0
  4. mlx_sparse-0.0.1b0/.pre-commit-config.yaml +9 -0
  5. mlx_sparse-0.0.1b0/.readthedocs.yaml +13 -0
  6. mlx_sparse-0.0.1b0/CMakeLists.txt +101 -0
  7. mlx_sparse-0.0.1b0/LICENSE +201 -0
  8. mlx_sparse-0.0.1b0/MANIFEST.in +16 -0
  9. mlx_sparse-0.0.1b0/PKG-INFO +93 -0
  10. mlx_sparse-0.0.1b0/README.md +64 -0
  11. mlx_sparse-0.0.1b0/benchmarks/bench_csr_matmul.py +101 -0
  12. mlx_sparse-0.0.1b0/benchmarks/bench_csr_matvec.py +100 -0
  13. mlx_sparse-0.0.1b0/docs/_static/custom.css +44 -0
  14. mlx_sparse-0.0.1b0/docs/api/constructors.rst +106 -0
  15. mlx_sparse-0.0.1b0/docs/api/containers.rst +70 -0
  16. mlx_sparse-0.0.1b0/docs/api/device.rst +39 -0
  17. mlx_sparse-0.0.1b0/docs/api/index.rst +14 -0
  18. mlx_sparse-0.0.1b0/docs/api/operations.rst +69 -0
  19. mlx_sparse-0.0.1b0/docs/conf.py +111 -0
  20. mlx_sparse-0.0.1b0/docs/index.rst +87 -0
  21. mlx_sparse-0.0.1b0/docs/installation.rst +137 -0
  22. mlx_sparse-0.0.1b0/docs/notebooks/01_first_steps.ipynb +266 -0
  23. mlx_sparse-0.0.1b0/docs/notebooks/02_csr_matvec.ipynb +274 -0
  24. mlx_sparse-0.0.1b0/docs/notebooks/03_csr_matmul.ipynb +276 -0
  25. mlx_sparse-0.0.1b0/docs/notebooks/04_constructors.ipynb +445 -0
  26. mlx_sparse-0.0.1b0/docs/notebooks/05_sparse_sparse.ipynb +311 -0
  27. mlx_sparse-0.0.1b0/docs/notebooks/06_autodiff.ipynb +324 -0
  28. mlx_sparse-0.0.1b0/docs/notebooks/07_dtype_device.ipynb +339 -0
  29. mlx_sparse-0.0.1b0/docs/notebooks/08_canonicalization.ipynb +347 -0
  30. mlx_sparse-0.0.1b0/docs/notebooks/09_scipy_bridge.ipynb +290 -0
  31. mlx_sparse-0.0.1b0/docs/notebooks/10_benchmarks.ipynb +363 -0
  32. mlx_sparse-0.0.1b0/docs/notebooks/11_graph_algorithms.ipynb +273 -0
  33. mlx_sparse-0.0.1b0/docs/notebooks/12_neural_layers.ipynb +354 -0
  34. mlx_sparse-0.0.1b0/docs/notebooks/index.rst +24 -0
  35. mlx_sparse-0.0.1b0/docs/performance.rst +184 -0
  36. mlx_sparse-0.0.1b0/docs/quickstart.rst +212 -0
  37. mlx_sparse-0.0.1b0/docs/requirements.txt +4 -0
  38. mlx_sparse-0.0.1b0/docs/supported.rst +249 -0
  39. mlx_sparse-0.0.1b0/docs/tutorials/finite_difference.rst +155 -0
  40. mlx_sparse-0.0.1b0/docs/tutorials/getting_started.rst +211 -0
  41. mlx_sparse-0.0.1b0/docs/tutorials/graph_laplacian.rst +156 -0
  42. mlx_sparse-0.0.1b0/docs/tutorials/index.rst +11 -0
  43. mlx_sparse-0.0.1b0/docs/tutorials/physics_workloads.rst +243 -0
  44. mlx_sparse-0.0.1b0/docs/tutorials/scipy_interop.rst +192 -0
  45. mlx_sparse-0.0.1b0/docs/user_guide/autodiff.rst +158 -0
  46. mlx_sparse-0.0.1b0/docs/user_guide/device_execution.rst +147 -0
  47. mlx_sparse-0.0.1b0/docs/user_guide/dtype_policy.rst +120 -0
  48. mlx_sparse-0.0.1b0/docs/user_guide/index.rst +11 -0
  49. mlx_sparse-0.0.1b0/docs/user_guide/sparse_formats.rst +223 -0
  50. mlx_sparse-0.0.1b0/docs/user_guide/validation.rst +129 -0
  51. mlx_sparse-0.0.1b0/examples/finite_difference_2d.py +61 -0
  52. mlx_sparse-0.0.1b0/examples/graph_laplacian.py +60 -0
  53. mlx_sparse-0.0.1b0/examples/quantum_hamiltonian.py +75 -0
  54. mlx_sparse-0.0.1b0/examples/sparse_linear_layer.py +73 -0
  55. mlx_sparse-0.0.1b0/mlx_sparse/__init__.py +111 -0
  56. mlx_sparse-0.0.1b0/mlx_sparse/_construct.py +637 -0
  57. mlx_sparse-0.0.1b0/mlx_sparse/_coo.py +257 -0
  58. mlx_sparse-0.0.1b0/mlx_sparse/_csr.py +413 -0
  59. mlx_sparse-0.0.1b0/mlx_sparse/_device.py +152 -0
  60. mlx_sparse-0.0.1b0/mlx_sparse/_ext_loader.py +36 -0
  61. mlx_sparse-0.0.1b0/mlx_sparse/_fallback.py +297 -0
  62. mlx_sparse-0.0.1b0/mlx_sparse/_host.py +33 -0
  63. mlx_sparse-0.0.1b0/mlx_sparse/_native.py +127 -0
  64. mlx_sparse-0.0.1b0/mlx_sparse/_ops.py +240 -0
  65. mlx_sparse-0.0.1b0/mlx_sparse/_typing.py +70 -0
  66. mlx_sparse-0.0.1b0/mlx_sparse/_validation.py +208 -0
  67. mlx_sparse-0.0.1b0/mlx_sparse/_version.py +24 -0
  68. mlx_sparse-0.0.1b0/mlx_sparse.egg-info/PKG-INFO +93 -0
  69. mlx_sparse-0.0.1b0/mlx_sparse.egg-info/SOURCES.txt +121 -0
  70. mlx_sparse-0.0.1b0/mlx_sparse.egg-info/dependency_links.txt +1 -0
  71. mlx_sparse-0.0.1b0/mlx_sparse.egg-info/not-zip-safe +1 -0
  72. mlx_sparse-0.0.1b0/mlx_sparse.egg-info/requires.txt +21 -0
  73. mlx_sparse-0.0.1b0/mlx_sparse.egg-info/top_level.txt +1 -0
  74. mlx_sparse-0.0.1b0/pyproject.toml +96 -0
  75. mlx_sparse-0.0.1b0/setup.cfg +4 -0
  76. mlx_sparse-0.0.1b0/setup.py +45 -0
  77. mlx_sparse-0.0.1b0/src/bindings.cpp +124 -0
  78. mlx_sparse-0.0.1b0/src/sparse/common.cpp +183 -0
  79. mlx_sparse-0.0.1b0/src/sparse/common.h +81 -0
  80. mlx_sparse-0.0.1b0/src/sparse/coo_tocsr.cpp +234 -0
  81. mlx_sparse-0.0.1b0/src/sparse/coo_tocsr.h +31 -0
  82. mlx_sparse-0.0.1b0/src/sparse/coo_tocsr.metal +87 -0
  83. mlx_sparse-0.0.1b0/src/sparse/csr_matmul.cpp +673 -0
  84. mlx_sparse-0.0.1b0/src/sparse/csr_matmul.h +39 -0
  85. mlx_sparse-0.0.1b0/src/sparse/csr_matmul.metal +220 -0
  86. mlx_sparse-0.0.1b0/src/sparse/csr_matvec.cpp +619 -0
  87. mlx_sparse-0.0.1b0/src/sparse/csr_matvec.h +39 -0
  88. mlx_sparse-0.0.1b0/src/sparse/csr_matvec.metal +198 -0
  89. mlx_sparse-0.0.1b0/src/sparse/csr_sort_indices.cpp +212 -0
  90. mlx_sparse-0.0.1b0/src/sparse/csr_sort_indices.h +31 -0
  91. mlx_sparse-0.0.1b0/src/sparse/csr_sort_indices.metal +70 -0
  92. mlx_sparse-0.0.1b0/src/sparse/csr_todense.cpp +188 -0
  93. mlx_sparse-0.0.1b0/src/sparse/csr_todense.h +29 -0
  94. mlx_sparse-0.0.1b0/src/sparse/csr_todense.metal +55 -0
  95. mlx_sparse-0.0.1b0/src/sparse/csr_transpose.cpp +244 -0
  96. mlx_sparse-0.0.1b0/src/sparse/csr_transpose.h +32 -0
  97. mlx_sparse-0.0.1b0/src/sparse/csr_transpose.metal +101 -0
  98. mlx_sparse-0.0.1b0/src/sparse/identity_like.cpp +25 -0
  99. mlx_sparse-0.0.1b0/src/sparse/identity_like.h +27 -0
  100. mlx_sparse-0.0.1b0/src/sparse/metal_common.h +57 -0
  101. mlx_sparse-0.0.1b0/tests/conftest.py +76 -0
  102. mlx_sparse-0.0.1b0/tests/test_constructor_edge_cases.py +171 -0
  103. mlx_sparse-0.0.1b0/tests/test_constructors.py +132 -0
  104. mlx_sparse-0.0.1b0/tests/test_conversion_api.py +81 -0
  105. mlx_sparse-0.0.1b0/tests/test_coo_tocsr.py +61 -0
  106. mlx_sparse-0.0.1b0/tests/test_cpu_gpu_parity.py +91 -0
  107. mlx_sparse-0.0.1b0/tests/test_csr_matmul.py +139 -0
  108. mlx_sparse-0.0.1b0/tests/test_csr_matvec.py +126 -0
  109. mlx_sparse-0.0.1b0/tests/test_csr_todense.py +65 -0
  110. mlx_sparse-0.0.1b0/tests/test_device_api.py +124 -0
  111. mlx_sparse-0.0.1b0/tests/test_dtypes.py +124 -0
  112. mlx_sparse-0.0.1b0/tests/test_fallback_dispatch.py +146 -0
  113. mlx_sparse-0.0.1b0/tests/test_grad.py +223 -0
  114. mlx_sparse-0.0.1b0/tests/test_importless_error_paths.py +67 -0
  115. mlx_sparse-0.0.1b0/tests/test_imports.py +82 -0
  116. mlx_sparse-0.0.1b0/tests/test_native_extension.py +32 -0
  117. mlx_sparse-0.0.1b0/tests/test_numerical_correctness.py +170 -0
  118. mlx_sparse-0.0.1b0/tests/test_performance_regression.py +139 -0
  119. mlx_sparse-0.0.1b0/tests/test_public_api.py +42 -0
  120. mlx_sparse-0.0.1b0/tests/test_sparse_api_edges.py +150 -0
  121. mlx_sparse-0.0.1b0/tests/test_streams_and_devices.py +31 -0
  122. mlx_sparse-0.0.1b0/tests/test_transpose.py +65 -0
  123. mlx_sparse-0.0.1b0/tests/test_validation_regressions.py +180 -0
@@ -0,0 +1,49 @@
1
+ name: CI
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches:
7
+ - main
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: macos-15
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: actions/setup-python@v5
15
+ with:
16
+ python-version: "3.12"
17
+
18
+ - name: Verify Metal toolchain
19
+ run: |
20
+ xcrun -sdk macosx -find metal
21
+
22
+ - name: Install
23
+ run: python -m pip install -e ".[dev,docs]"
24
+
25
+ - name: Verify metallib build output
26
+ run: test -f mlx_sparse/mlx_sparse.metallib
27
+
28
+ - name: Python format
29
+ run: |
30
+ python -m black --check mlx_sparse tests benchmarks examples
31
+ python -m isort --check-only mlx_sparse tests benchmarks examples
32
+
33
+ - name: C++ format
34
+ run: |
35
+ xcrun clang-format --dry-run --Werror src/bindings.cpp src/sparse/*.cpp src/sparse/*.h src/sparse/*.metal
36
+
37
+ - name: Tests
38
+ run: python -m pytest -q --cov=mlx_sparse --cov-report=term-missing --cov-fail-under=88 --cov-branch --cov-append --cov-report=xml
39
+
40
+ - name: Docs
41
+ run: python -m sphinx -b html docs/ docs/_build/html
42
+
43
+ - name: Build wheel
44
+ run: python -m build --wheel
45
+
46
+ - name: Upload coverage reports to Codecov
47
+ uses: codecov/codecov-action@v5
48
+ with:
49
+ token: ${{ secrets.CODECOV_TOKEN }}
@@ -0,0 +1,94 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "v*"
7
+
8
+ jobs:
9
+ build-wheels:
10
+ name: Wheel / Python ${{ matrix.python-version }}
11
+ runs-on: macos-15
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
16
+
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ with:
20
+ # Full history is required so setuptools-scm can read the git tag.
21
+ fetch-depth: 0
22
+
23
+ - uses: actions/setup-python@v5
24
+ with:
25
+ python-version: ${{ matrix.python-version }}
26
+
27
+ - name: Verify Metal toolchain
28
+ run: xcrun -sdk macosx -find metal
29
+
30
+ - name: Install build frontend
31
+ run: python -m pip install --upgrade pip build
32
+
33
+ - name: Build wheel
34
+ env:
35
+ MACOSX_DEPLOYMENT_TARGET: "14.0"
36
+ run: python -m build --wheel
37
+
38
+ - name: Upload wheel artifact
39
+ uses: actions/upload-artifact@v4
40
+ with:
41
+ name: wheel-py${{ matrix.python-version }}
42
+ path: dist/*.whl
43
+ if-no-files-found: error
44
+
45
+ build-sdist:
46
+ name: Source distribution
47
+ runs-on: macos-15
48
+
49
+ steps:
50
+ - uses: actions/checkout@v4
51
+ with:
52
+ fetch-depth: 0
53
+
54
+ - uses: actions/setup-python@v5
55
+ with:
56
+ python-version: "3.13"
57
+
58
+ - name: Install build frontend
59
+ run: python -m pip install --upgrade pip build
60
+
61
+ - name: Build sdist
62
+ run: python -m build --sdist
63
+
64
+ - name: Upload sdist artifact
65
+ uses: actions/upload-artifact@v4
66
+ with:
67
+ name: sdist
68
+ path: dist/*.tar.gz
69
+ if-no-files-found: error
70
+
71
+ publish:
72
+ name: Publish to PyPI
73
+ needs: [build-wheels, build-sdist]
74
+ runs-on: ubuntu-latest
75
+
76
+ environment:
77
+ name: pypi
78
+ url: https://pypi.org/project/mlx-sparse/
79
+
80
+ permissions:
81
+ id-token: write
82
+
83
+ steps:
84
+ - name: Download all artifacts
85
+ uses: actions/download-artifact@v4
86
+ with:
87
+ path: dist/
88
+ merge-multiple: true
89
+
90
+ - name: List distributions
91
+ run: ls -lh dist/
92
+
93
+ - name: Publish to PyPI
94
+ uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,20 @@
1
+ .DS_Store
2
+ .pytest_cache/
3
+ .ruff_cache/
4
+ .mypy_cache/
5
+ __pycache__/
6
+ *.py[cod]
7
+
8
+ build/
9
+ dist/
10
+ *.egg-info/
11
+
12
+ .venv/
13
+
14
+ mlx_sparse/_ext*.so
15
+ mlx_sparse/*.dylib
16
+ mlx_sparse/*.metallib
17
+ mlx_sparse/_version.py
18
+
19
+ docs/_build
20
+ /docs/misc/
@@ -0,0 +1,9 @@
1
+ repos:
2
+ - repo: https://github.com/psf/black
3
+ rev: 26.5.1
4
+ hooks:
5
+ - id: black
6
+ - repo: https://github.com/pycqa/isort
7
+ rev: 7.0.0
8
+ hooks:
9
+ - id: isort
@@ -0,0 +1,13 @@
1
+ version: 2
2
+
3
+ build:
4
+ os: ubuntu-24.04
5
+ tools:
6
+ python: "3.12"
7
+
8
+ sphinx:
9
+ configuration: docs/conf.py
10
+
11
+ python:
12
+ install:
13
+ - requirements: docs/requirements.txt
@@ -0,0 +1,101 @@
1
+ cmake_minimum_required(VERSION 3.27)
2
+
3
+ project(mlx_sparse_ext LANGUAGES CXX)
4
+
5
+ set(CMAKE_CXX_STANDARD 17)
6
+ set(CMAKE_CXX_STANDARD_REQUIRED ON)
7
+ set(CMAKE_POSITION_INDEPENDENT_CODE ON)
8
+
9
+ option(BUILD_SHARED_LIBS "Build extensions as a shared library" ON)
10
+ option(MLX_SPARSE_BUILD_METAL "Build the mlx_sparse Metal library" ON)
11
+
12
+ if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
13
+ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
14
+ endif()
15
+
16
+ if(DEFINED ENV{VIRTUAL_ENV})
17
+ set(Python_EXECUTABLE
18
+ "$ENV{VIRTUAL_ENV}/bin/python"
19
+ CACHE FILEPATH "Python executable from active virtualenv" FORCE)
20
+ endif()
21
+ find_package(Python 3.10 COMPONENTS Interpreter Development.Module REQUIRED)
22
+
23
+ if(NOT DEFINED nanobind_DIR)
24
+ execute_process(
25
+ COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
26
+ OUTPUT_STRIP_TRAILING_WHITESPACE
27
+ OUTPUT_VARIABLE nanobind_DIR)
28
+ endif()
29
+ find_package(nanobind CONFIG REQUIRED PATHS "${nanobind_DIR}" NO_DEFAULT_PATH)
30
+
31
+ if(NOT DEFINED MLX_DIR)
32
+ execute_process(
33
+ COMMAND "${Python_EXECUTABLE}" -m mlx --cmake-dir
34
+ OUTPUT_STRIP_TRAILING_WHITESPACE
35
+ OUTPUT_VARIABLE MLX_DIR)
36
+ endif()
37
+ find_package(MLX CONFIG REQUIRED PATHS "${MLX_DIR}" NO_DEFAULT_PATH)
38
+
39
+ add_library(mlx_sparse_native)
40
+
41
+ target_sources(
42
+ mlx_sparse_native
43
+ PUBLIC
44
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/common.cpp
45
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/coo_tocsr.cpp
46
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_matmul.cpp
47
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_matvec.cpp
48
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_sort_indices.cpp
49
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_todense.cpp
50
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_transpose.cpp
51
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/identity_like.cpp)
52
+
53
+ target_include_directories(
54
+ mlx_sparse_native
55
+ PUBLIC
56
+ ${CMAKE_CURRENT_LIST_DIR}/src)
57
+
58
+ target_link_libraries(mlx_sparse_native PUBLIC mlx)
59
+
60
+ execute_process(
61
+ COMMAND xcrun -sdk macosx -find metal
62
+ RESULT_VARIABLE MLX_SPARSE_METAL_FIND_RESULT
63
+ OUTPUT_QUIET
64
+ ERROR_QUIET)
65
+
66
+ if(MLX_BUILD_METAL AND MLX_SPARSE_BUILD_METAL AND MLX_SPARSE_METAL_FIND_RESULT EQUAL 0)
67
+ mlx_build_metallib(
68
+ TARGET mlx_sparse_metallib
69
+ TITLE mlx_sparse
70
+ SOURCES
71
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/coo_tocsr.metal
72
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_matmul.metal
73
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_matvec.metal
74
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_sort_indices.metal
75
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_todense.metal
76
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/csr_transpose.metal
77
+ DEPS
78
+ ${CMAKE_CURRENT_LIST_DIR}/src/sparse/metal_common.h
79
+ INCLUDE_DIRS
80
+ ${PROJECT_SOURCE_DIR}
81
+ ${CMAKE_CURRENT_LIST_DIR}/src
82
+ ${MLX_INCLUDE_DIRS}
83
+ OUTPUT_DIRECTORY
84
+ ${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
85
+
86
+ add_dependencies(mlx_sparse_native mlx_sparse_metallib)
87
+ else()
88
+ message(STATUS "Skipping mlx_sparse.metallib build; Metal compiler is unavailable.")
89
+ endif()
90
+
91
+ nanobind_add_module(
92
+ _ext
93
+ NB_STATIC STABLE_ABI LTO NOMINSIZE
94
+ NB_DOMAIN mlx
95
+ ${CMAKE_CURRENT_LIST_DIR}/src/bindings.cpp)
96
+
97
+ target_link_libraries(_ext PRIVATE mlx_sparse_native)
98
+
99
+ if(BUILD_SHARED_LIBS)
100
+ target_link_options(_ext PRIVATE -Wl,-rpath,@loader_path)
101
+ endif()
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,16 @@
1
+ include CMakeLists.txt
2
+ include CHANGELOG.md
3
+ include LICENSE
4
+ include README.md
5
+ include pyproject.toml
6
+ include setup.py
7
+
8
+ recursive-include src *.cpp *.h *.metal
9
+ recursive-include mlx_sparse *.py *.metallib
10
+ recursive-include benchmarks *.py *.md
11
+ recursive-include docs *.css *.md *.py *.rst
12
+ recursive-include examples *.py
13
+ recursive-include tests *.py
14
+
15
+ prune build
16
+ prune docs/_build
@@ -0,0 +1,93 @@
1
+ Metadata-Version: 2.4
2
+ Name: mlx-sparse
3
+ Version: 0.0.1b0
4
+ Summary: Sparse array containers and primitives for MLX
5
+ Author: mlx-sparse contributors
6
+ License-Expression: Apache-2.0
7
+ Requires-Python: >=3.10
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: mlx>=0.31
11
+ Provides-Extra: dev
12
+ Requires-Dist: build>=1.2; extra == "dev"
13
+ Requires-Dist: black>=26.5.0; extra == "dev"
14
+ Requires-Dist: isort>=6; extra == "dev"
15
+ Requires-Dist: nanobind>=2.0; extra == "dev"
16
+ Requires-Dist: numpy>=1.26; extra == "dev"
17
+ Requires-Dist: pre-commit>=4; extra == "dev"
18
+ Requires-Dist: pytest>=8; extra == "dev"
19
+ Requires-Dist: pytest-cov>=6; extra == "dev"
20
+ Requires-Dist: scipy>=1.11; extra == "dev"
21
+ Provides-Extra: bench
22
+ Requires-Dist: numpy>=1.26; extra == "bench"
23
+ Requires-Dist: scipy>=1.11; extra == "bench"
24
+ Provides-Extra: docs
25
+ Requires-Dist: sphinx<10,>=8; extra == "docs"
26
+ Requires-Dist: pydata-sphinx-theme>=0.16; extra == "docs"
27
+ Requires-Dist: myst-nb>=1.4; extra == "docs"
28
+ Dynamic: license-file
29
+
30
+ # mlx-sparse
31
+
32
+ > **Warning: beta release**
33
+ > This is an early beta. APIs may change, bugs are expected, and some features
34
+ > are still incomplete. Feedback and issue reports are very welcome!
35
+
36
+ `mlx-sparse` is an attempt at an MLX-native sparse array package. The public API is Python,
37
+ while performance-critical operations are implemented as MLX primitives in C++
38
+ with CPU backends and Metal kernels for fixed-shape sparse operations.
39
+
40
+ The supported format surface is COO and CSR for 2D sparse arrays. Current
41
+ functionality includes construction, validation, COO to CSR, CSR to dense, CSR
42
+ canonicalization, CSR matrix-vector multiply, CSR matrix-matrix multiply,
43
+ batched dense RHS products, CSR sparse-sparse products, transpose, Hermitian
44
+ transpose, and autodiff through sparse values and dense RHS operands, including
45
+ `complex64`.
46
+
47
+ Supported value dtypes are `float32`, `float16`, `bfloat16`, and `complex64`.
48
+ Supported index dtypes are `int32` and `int64` on CPU and GPU.
49
+
50
+ ## Quick Start
51
+
52
+ Install from PyPI:
53
+
54
+ ```bash
55
+ python -m pip install mlx-sparse
56
+ ```
57
+
58
+ ```python
59
+ import mlx.core as mx
60
+ import numpy as np
61
+
62
+ import mlx_sparse as ms
63
+
64
+ ms.use_gpu()
65
+
66
+ data = mx.array(np.array([2.0, -1.0, 4.0], dtype=np.float32))
67
+ row = mx.array(np.array([0, 0, 1], dtype=np.int32))
68
+ col = mx.array(np.array([0, 2, 1], dtype=np.int32))
69
+
70
+ a = ms.coo_array((data, (row, col)), shape=(2, 3)).tocsr(canonical=True)
71
+ x = mx.array(np.array([3.0, 10.0, 7.0], dtype=np.float32))
72
+
73
+ y = a @ x
74
+ dense = a.todense()
75
+ at = a.T
76
+ ```
77
+
78
+ The package build compiles `src/sparse/*.metal` into
79
+ `mlx_sparse/mlx_sparse.metallib` when the macOS Metal toolchain is available,
80
+ and the wheel ships that metallib beside the Python package.
81
+
82
+ ## Development
83
+
84
+ For contributors, use an editable install from the repository root. This builds
85
+ the native extension and installs the development tooling.
86
+
87
+ ```bash
88
+ python -m pip install -e ".[dev]"
89
+ ```
90
+
91
+ ## License
92
+
93
+ This package is licensed under the [Apache License 2.0](https://github.com/waleed-sh/mlx-sparse/blob/main/LICENSE).
@@ -0,0 +1,64 @@
1
+ # mlx-sparse
2
+
3
+ > **Warning: beta release**
4
+ > This is an early beta. APIs may change, bugs are expected, and some features
5
+ > are still incomplete. Feedback and issue reports are very welcome!
6
+
7
+ `mlx-sparse` is an attempt at an MLX-native sparse array package. The public API is Python,
8
+ while performance-critical operations are implemented as MLX primitives in C++
9
+ with CPU backends and Metal kernels for fixed-shape sparse operations.
10
+
11
+ The supported format surface is COO and CSR for 2D sparse arrays. Current
12
+ functionality includes construction, validation, COO to CSR, CSR to dense, CSR
13
+ canonicalization, CSR matrix-vector multiply, CSR matrix-matrix multiply,
14
+ batched dense RHS products, CSR sparse-sparse products, transpose, Hermitian
15
+ transpose, and autodiff through sparse values and dense RHS operands, including
16
+ `complex64`.
17
+
18
+ Supported value dtypes are `float32`, `float16`, `bfloat16`, and `complex64`.
19
+ Supported index dtypes are `int32` and `int64` on CPU and GPU.
20
+
21
+ ## Quick Start
22
+
23
+ Install from PyPI:
24
+
25
+ ```bash
26
+ python -m pip install mlx-sparse
27
+ ```
28
+
29
+ ```python
30
+ import mlx.core as mx
31
+ import numpy as np
32
+
33
+ import mlx_sparse as ms
34
+
35
+ ms.use_gpu()
36
+
37
+ data = mx.array(np.array([2.0, -1.0, 4.0], dtype=np.float32))
38
+ row = mx.array(np.array([0, 0, 1], dtype=np.int32))
39
+ col = mx.array(np.array([0, 2, 1], dtype=np.int32))
40
+
41
+ a = ms.coo_array((data, (row, col)), shape=(2, 3)).tocsr(canonical=True)
42
+ x = mx.array(np.array([3.0, 10.0, 7.0], dtype=np.float32))
43
+
44
+ y = a @ x
45
+ dense = a.todense()
46
+ at = a.T
47
+ ```
48
+
49
+ The package build compiles `src/sparse/*.metal` into
50
+ `mlx_sparse/mlx_sparse.metallib` when the macOS Metal toolchain is available,
51
+ and the wheel ships that metallib beside the Python package.
52
+
53
+ ## Development
54
+
55
+ For contributors, use an editable install from the repository root. This builds
56
+ the native extension and installs the development tooling.
57
+
58
+ ```bash
59
+ python -m pip install -e ".[dev]"
60
+ ```
61
+
62
+ ## License
63
+
64
+ This package is licensed under the [Apache License 2.0](https://github.com/waleed-sh/mlx-sparse/blob/main/LICENSE).