Blis BLAS-like Linear Algebra Library
Blis is a Python library that provides a high-performance, self-contained C-extension for BLAS-like dense linear algebra operations. Developed by Explosion (the creators of spaCy), it focuses on delivering fast matrix multiplication and other linear algebra routines without requiring external system dependencies. The library is actively maintained, with a current version of 1.3.3, and releases often include support for newer Python versions and updates to underlying dependencies like NumPy.
Warnings
- breaking Blis versions 1.0.0 and later introduce a breaking change due to dependency on NumPy 2.0. Wheels built against NumPy 1.x are not compatible with NumPy 2.0. If you distribute binaries, they must be built against NumPy 2.x to be compatible with both NumPy 1.x and 2.x runtimes.
- gotcha For optimal performance on non-x86_64 or osx/arm64 architectures, you may need to compile `blis` from source with specific architecture flags. The provided wheels are optimized for common architectures, but for other CPUs, a generic (slower) build might be used unless `BLIS_ARCH` environment variable is set during installation.
- gotcha Blis is designed for single-threaded execution for its intended workloads (ML inference). While it is re-entrant and safe to use concurrently from multiple threads with immutable data, concurrent mutation of input NumPy arrays from different threads while Blis is running can lead to data races or segmentation faults.
- gotcha Earlier versions around `v1.0.0` experienced memory access errors and instability on Windows. This issue was addressed in `v1.2.0` by reverting to an older, more stable version of the vendored Blis library.
Install
-
pip install blis
Imports
- einsum
from blis.py import einsum
- cy
cimport blis.cy
Quickstart
import numpy as np
from blis.py import einsum
dim_a, dim_b, dim_c = 500, 128, 300
arr1 = np.random.rand(dim_a, dim_b).astype('float32')
arr2 = np.random.rand(dim_b, dim_c).astype('float32')
out = np.zeros((dim_a, dim_c), dtype='float32')
# Perform matrix multiplication using einsum
einsum('ab,bc->ac', arr1, arr2, out=out)
print(f"Output shape: {out.shape}")
# Example of returning a new array with transposed output
out_transposed = einsum('ab,bc->ca', arr1, arr2)
print(f"Transposed output shape: {out_transposed.shape}")