Skip to main content

a KLU solver for JAX

Project description

KLUJAX

version: 0.4.2

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.2-cp313-cp313-win_amd64.whl (139.9 kB view details)

Uploaded CPython 3.13Windows x86-64

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

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

klujax-0.4.2-cp313-cp313-macosx_11_0_arm64.whl (198.4 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

klujax-0.4.2-cp312-cp312-win_amd64.whl (139.8 kB view details)

Uploaded CPython 3.12Windows x86-64

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

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

klujax-0.4.2-cp312-cp312-macosx_11_0_arm64.whl (198.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

klujax-0.4.2-cp311-cp311-win_amd64.whl (139.3 kB view details)

Uploaded CPython 3.11Windows x86-64

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

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

klujax-0.4.2-cp311-cp311-macosx_11_0_arm64.whl (199.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

klujax-0.4.2-cp310-cp310-win_amd64.whl (138.0 kB view details)

Uploaded CPython 3.10Windows x86-64

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

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

klujax-0.4.2-cp310-cp310-macosx_11_0_arm64.whl (198.1 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

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

File metadata

  • Download URL: klujax-0.4.2-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 139.9 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.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 fa4c6dec9fb3787a4cd37a8a60c00f5c00910baae4dbc3b5907443e09d4bfc1a
MD5 8b62965ed9d7b238b760f99999926bfc
BLAKE2b-256 242bf30f4b11aa0b83cb966a28b7861c27db0f59a16cc20d4181912309b5b135

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9a2ce7802c0b6f95c176c2052f78f5334be8f63020dd95f8f4dd5e4d67086f25
MD5 e34591653551ca9602117bdd8abd750b
BLAKE2b-256 c728e44d010f0b6e92732183b54464fda68239d46fc75465a5ea73f191a72f16

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4afdaa2a5357f9a8b357a8eab7b4537f8223b5835dd5d049033130de06e6dc1d
MD5 702452de7aa9909830013231edbd7d74
BLAKE2b-256 78680f4edf4d88e8a81e8ac8fb025069a4c46411738feaaad038e8da5715602f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: klujax-0.4.2-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 139.8 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.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 69a44a5d3107fd2bd442c2122003360b10730125aca6c7c688b40bc1dab2c76f
MD5 f37f6f0c547532a3ff8de1067fc9c94e
BLAKE2b-256 5c48bcde855eabb5638632dbe19d1a5db2ad518e7a11e0c952b1ffd4acad3292

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4481232d0c728be30a3db800d3b65f0b42986287e2d930aa569504727f640bb8
MD5 b61e4d58263ede686b663d8e68f3afbc
BLAKE2b-256 e3820eec327697345f02689255b8012cdd1be3e9eaf62108db9f4725a5289ef3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cd436d36c04951693559edf442066c34a616ecbabb9f17e3589b1670cee993b4
MD5 274b7d9b6c6ab218318b6fc8a65c1413
BLAKE2b-256 aea024d0f6d680011613528cd70d3f5ec11b21db9c50dc8aead3a99f949d78bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: klujax-0.4.2-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 139.3 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.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 3c412eab5a78fa9a5b70e75611661d6c7c353f80871588c02e6ce9e9670122e0
MD5 f7a1a7625da574e386e45a19574165ca
BLAKE2b-256 f3b25440b2bd58c18c0fe49de3f2f8a30c87bb66df0878f7debd018df6e7abdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 17d750c172120ccf0c90c5a1a71c65e62d932800557c71f532927a474375ac7d
MD5 beb7c9197a35fa240119b43803fb22e5
BLAKE2b-256 d1d7a4614c2585acb61b9d762add4ac71d96476b20c3e62a2020147964f168bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 90cde09fdc366045ec974c99df06f9090e2d3d0d217d541f0de14c89ec0a03c4
MD5 b684657459f3acee027bf7112bc4359e
BLAKE2b-256 fd3e9763ca040dbfd1cce72a6ebf6f761e44305bb24d68adbfcc4b925477b693

See more details on using hashes here.

File details

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

File metadata

  • Download URL: klujax-0.4.2-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 138.0 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.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 d9c4187f6be3ed39d7608e2e4d9888c7929e8fd8a8c4b0cc68e8d79371a8985e
MD5 13d0fd61eef8efcfd9deeaf5f17f39b3
BLAKE2b-256 48217bcb694a563a866926120d1f3482686e43ecc30e3a8646568db8677fe9f8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ce3abce76bf046cef9e52319fab75d890123511080784cb3909940150166fed1
MD5 b59d011eb49faf5e86fce58c7b5b166d
BLAKE2b-256 f1540f3bc626df1c09d66b85ace33937b46c67b28c7b2e553d23b8b81c72ee6a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for klujax-0.4.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9b6d926cc29ddf5e70311e60c4f5f41a99707b8e5484482c04f93f8d1912c545
MD5 b360851c5c835085044d2ed25a73b18d
BLAKE2b-256 393b48efe987b8da9c20993b9798dcb5808ddfd39ca5068a7cc2a613e0096b53

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