Skip to main content

a KLU solver for JAX

Project description

KLUJAX

version: 0.4.0

A sparse linear solver for JAX based on the efficient KLU algorithm.

CPU & float64

This library is a wrapper around the SuiteSparse KLU algorithms. This means the algorithm is only implemented for C-arrays and hence is only available for CPU arrays with double precision, i.e. float64 or complex128.

Note that float32/complex64 arrays will be cast to float64/complex128!

Usage

The klujax library provides a single function solve(Ai, Aj, Ax, b), which solves for x in the sparse linear system Ax=b, where A is explicitly given in COO-format (Ai, Aj, Ax).

NOTE: the sparse matrix represented by (Ai, Aj, Ax) needs to be coalesced! KLUJAX provides a coalesce function (which unfortunately is not jax-jittable).

Supported shapes (? suffix means optional):

  • Ai: (n_nz,)
  • Aj: (n_nz,)
  • Ax: (n_lhs?, n_nz)
  • b: (n_lhs?, n_col, n_rhs?)
  • A (represented by (Ai, Aj, Ax)): (n_lhs?, n_col, n_col)

KLUJAX will automatically select a sensible way to act on underdefined dimensions of Ax and b:

dim(Ax) dim(b) assumed shape(Ax) assumed shape(b)
1D 1D n_nz n_col
1D 2D n_nz n_col x n_rhs
1D 3D n_nz n_lhs x n_col x n_rhs
2D 1D n_lhs x n_nz n_col
2D 2D n_lhs x n_nz n_lhs x n_col
2D 3D n_lhs x n_nz n_lhs x n_col x n_rhs

Where the A is always acting on the n_col dimension of b. The n_lhs dim is a shared batch dimension between A and b.

Additional dimensions can be added with jax.vmap (alternatively any higher dimensional problem can be reduced to the one above by properly transposing and reshaping Ax and b).

NOTE: JAX now has an experimental sparse library (jax.experimental.sparse). Using this natively in KLUJAX is not yet supported (but converting from BCOO or COO to Ai, Aj, Ax is trivial).

Basic Example

Script:

import klujax
import jax.numpy as jnp

b = jnp.array([8, 45, -3, 3, 19])
A_dense = jnp.array(
    [
        [2, 3, 0, 0, 0],
        [3, 0, 4, 0, 6],
        [0, -1, -3, 2, 0],
        [0, 0, 1, 0, 0],
        [0, 4, 2, 0, 1],
    ]
)
Ai, Aj = jnp.where(jnp.abs(A_dense) > 0)
Ax = A_dense[Ai, Aj]

result_ref = jnp.linalg.inv(A_dense) @ b
result = klujax.solve(Ai, Aj, Ax, b)

print(jnp.abs(result - result_ref) < 1e-12)
print(result)

Output:

[ True True True True True]
[1. 2. 3. 4. 5.]

Installation

The library is statically linked to the SuiteSparse C++ library. It can be installed on most platforms as follows:

pip install klujax

There exist pre-built wheels for Linux and Windows (python 3.8+). If no compatible wheel is found, however, pip will attempt to install the library from source... make sure you have the necessary build dependencies installed (see Installing from Source)

Installing from Source

NOTE: Installing from source should only be necessary when developing the library. If you as the user experience an install from source please create an issue.

Before installing, clone the build dependencies:

git clone --depth 1 --branch v7.2.0 https://github.com/DrTimothyAldenDavis/SuiteSparse suitesparse
git clone --depth 1 --branch main https://github.com/openxla/xla xla
git clone --depth 1 --branch stable https://github.com/pybind/pybind11 pybind11

Linux

On linux, you'll need gcc and g++, then inside the repo:

pip install .

MacOs

On MacOS, you'll need clang, then inside the repo:

pip install .

Windows

On Windows, installing from source is a bit more involved as typically the build dependencies are not installed. To install those, download Visual Studio Community 2017 from here. During installation, go to Workloads and select the following workloads:

  • Desktop development with C++
  • Python development

Then go to Individual Components and select the following additional items:

  • C++/CLI support
  • VC++ 2015.3 v14.00 (v140) toolset for desktop

Then, download and install Microsoft Visual C++ Redistributable from here.

After these installation steps, run the following commands inside a x64 Native Tools Command Prompt for VS 2017:

set DISTUTILS_USE_SDK=1
pip install .

License & Credits

© Floris Laporte 2022, LGPL-2.1

This library was partly based on:

This library vendors an unmodified version of the SuiteSparse libraries in its source (.tar.gz) distribution to allow for static linking. This is in accordance with their LGPL licence.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

klujax-0.4.0-cp313-cp313-win_amd64.whl (137.3 kB view details)

Uploaded CPython 3.13 Windows x86-64

klujax-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.13 manylinux: glibc 2.17+ x86-64

klujax-0.4.0-cp313-cp313-macosx_11_0_arm64.whl (198.2 kB view details)

Uploaded CPython 3.13 macOS 11.0+ ARM64

klujax-0.4.0-cp312-cp312-win_amd64.whl (137.3 kB view details)

Uploaded CPython 3.12 Windows x86-64

klujax-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

klujax-0.4.0-cp312-cp312-macosx_11_0_arm64.whl (198.1 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

klujax-0.4.0-cp311-cp311-win_amd64.whl (137.1 kB view details)

Uploaded CPython 3.11 Windows x86-64

klujax-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

klujax-0.4.0-cp311-cp311-macosx_11_0_arm64.whl (199.4 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

klujax-0.4.0-cp310-cp310-win_amd64.whl (135.7 kB view details)

Uploaded CPython 3.10 Windows x86-64

klujax-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

klujax-0.4.0-cp310-cp310-macosx_11_0_arm64.whl (197.9 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

File details

Details for the file klujax-0.4.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: klujax-0.4.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 137.3 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for klujax-0.4.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 e22e3ae61c2e240960018fda5c01bdc70a0534b3909eb26da86f1182c9c0b576
MD5 6f6361e0bd65c1b6dba66b942356fc74
BLAKE2b-256 5387d2db85ec8044d8cc3bf07c891e1acf88f661d99e7d20b70967e13cca23eb

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a62b9713eb4cad59ef0f135a6bb8baf2bfcf95e1ffb3982f601988991acd9e64
MD5 ad5be15a0e67b34be1dd0e50de89a13e
BLAKE2b-256 f070de9ddad120a4698eaf2f06ba8aaca1fcb9d29714117d1065836c87e3a278

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 70e105ea072a3df6fb8cbb224ca13f40d1e7b9d76960a2593cddef63a6fde98c
MD5 671ba69390af18189107a7cd5ca3feeb
BLAKE2b-256 6bf45173b6192cb0eec5fd281b2a9df205e3a169bcfa2f5e9547917547ad3e34

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: klujax-0.4.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 137.3 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for klujax-0.4.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 0defb316e487dd45c3bca9f8086d646fc2ccc703c6cb834788eb324c81be4c10
MD5 83e1c914602af485a9a3e0c25cf5f07f
BLAKE2b-256 a7fa0ed84d342b6daa1de5b31529a401376283dbb55f260be2d3444facaf8fe0

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e331a292ab3e36e309906758a5aac15c2546b3482e6eb02f3b78ab3ffa33504e
MD5 26e01913b56530fc0987e20199e9881d
BLAKE2b-256 5b3f19c1bfd8705510b0138f0aaffc0e03f67a4d9b683d8031ae7aa7910087a2

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c1659b58804033d0e0bf41cc39000bdef88d2a3578807f81edcbb7a96c7202df
MD5 49d92e382c4096fc13b1af006d93e987
BLAKE2b-256 802aae18e62d011a21558d2bad3223218123a536cdfca9a27ece1f8e59862a4a

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: klujax-0.4.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 137.1 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for klujax-0.4.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 644e592a48259d751487a71461c9f317b210df9b572cae57c1eabaf55db493d2
MD5 6f20f017ff1ea7c5a77671085274c4c1
BLAKE2b-256 95621a20edf57e36e26511cacfcc058f781a9e5b0a495357bb4cd6f9dca63cc9

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f4f1929a202e9f248a35177a983c36f76c5e1cc0b9fbe1f5c415e2a3b1201a9
MD5 cef4c3851e6d4338fb3ffc15106fe81e
BLAKE2b-256 0e2729cc5381edfc042f91827436e40b94cfcf17a6568c3dfd1b9246d50ea50f

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 03da19e414f4727f83d391ae736fab59294a6514e38971b4086fc9b4129e3b94
MD5 c17d8e150d8d0acecc36ce1bc01b711e
BLAKE2b-256 f3e82d6ae598762854702180b4e28baf664159b5e9e103001f2f5beb037fff63

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: klujax-0.4.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 135.7 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for klujax-0.4.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5002ab10eed76dc9dd3c22a6a02e963f2db34e32016223733c25d86da828ecff
MD5 b984b34778009da94823543fc2210996
BLAKE2b-256 9be63f198964f35f63c6e4f5b29c32bc78ae75ad411934bd32928fe317b5b484

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 db7498b885e9c93e655b0b6b9be91ce243b1464ba3b91faaff61086d7ff255ec
MD5 f6e4612a60a8f14bc1f2a4c4f95e87ac
BLAKE2b-256 8f81a17f5e97bad5e614499919526c2c38996cb72215a6c6927de72a78b5b1f5

See more details on using hashes here.

File details

Details for the file klujax-0.4.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for klujax-0.4.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2f3de25411c8cfcec9fbc57a502cdea02f0454fb40b3eb87d48a194adb0ed3f2
MD5 35452c234f668d0d43828e4d50a791fa
BLAKE2b-256 4dde3166292d80288bd8ad6379ffcee859b73cf3dd67bcdda661b75e1ebdd765

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page