Skip to main content

The Blis BLAS-like linear algebra library, as a self-contained C-extension.

Project description

Cython BLIS: Fast BLAS-like operations from Python and Cython, without the tears

This repository provides the Blis linear algebra routines as a self-contained Python C-extension.

Currently, we only supports single-threaded execution, as this is actually best for our workloads (ML inference).

Azure Pipelines pypi Version conda Python wheels

Installation

You can install the package via pip:

pip install blis

Wheels should be available, so installation should be fast. If you want to install from source and you're on Windows, you'll need to install LLVM.

Building BLIS for alternative architectures

The provided wheels should work on x86_86 architectures. Unfortunately we do not currently know a way to provide different wheels for alternative architectures, and we cannot provide a single binary that works everywhere. So if the wheel doesn't work for your CPU, you'll need to specify source distribution, and tell Blis your CPU architecture using the BLIS_ARCH environment variable.

a) Installing with generic arch support

BLIS_ARCH="generic" pip install spacy --no-binary blis

b) Building specific support

In order to compile Blis, cython-blis bundles makefile scripts for specific architectures, that are compiled by running the Blis build system and logging the commands. We do not yet have logs for every architecture, as there are some architectures we have not had access to.

See here for list of architectures. For example, here's how to build support for the ARM architecture cortexa57:

git clone https://github.com/explosion/cython-blis && cd cython-blis
git pull && git submodule init && git submodule update && git submodule status
python3 -m venv env3.6
source env3.6/bin/activate
pip install -r requirements.txt
./bin/generate-make-jsonl linux cortexa57
BLIS_ARCH="cortexa57" python setup.py build_ext --inplace
BLIS_ARCH="cortexa57" python setup.py bdist_wheel

Fingers crossed, this will build you a wheel that supports your platform. You could then submit a PR with the blis/_src/make/linux-cortexa57.jsonl and blis/_src/include/linux-cortexa57/blis.h files so that you can run:

BLIS_ARCH=cortexa57 pip install --no-binary=blis

Running the benchmark

After installation, run a small matrix multiplication benchmark:

$ export OMP_NUM_THREADS=1 # Tell Numpy to only use one thread.
$ python -m blis.benchmark
Setting up data nO=384 nI=384 batch_size=2000. Running 1000 iterations
Blis...
Total: 11032014.6484
7.35 seconds
Numpy (Openblas)...
Total: 11032016.6016
16.81 seconds
Blis einsum ab,cb->ca
8.10 seconds
Numpy einsum ab,cb->ca
Total: 5510596.19141
83.18 seconds

The low numpy.einsum performance is expected, but the low numpy.dot performance is surprising. Linking numpy against MKL gives better performance:

Numpy (mkl_rt) gemm...
Total: 11032011.71875
5.21 seconds

These figures refer to performance on a Dell XPS 13 i7-7500U. Running the same benchmark on a 2015 MacBook Air gives:

Blis...
Total: 11032014.6484
8.89 seconds
Numpy (Accelerate)...
Total: 11032012.6953
6.68 seconds

Clearly the Dell's numpy+OpenBLAS performance is the outlier, so it's likely something has gone wrong in the compilation and architecture detection.

Usage

Two APIs are provided: a high-level Python API, and direct Cython access. The best part of the Python API is the einsum function, which works like numpy's, but with some restrictions that allow a direct mapping to Blis routines. Example usage:

from blis.py import einsum
from numpy import ndarray, zeros

dim_a = 500
dim_b = 128
dim_c = 300
arr1 = ndarray((dim_a, dim_b))
arr2 = ndarray((dim_b, dim_c))
out = zeros((dim_a, dim_c))

einsum('ab,bc->ac', arr1, arr2, out=out)
# Change dimension order of output
out = einsum('ab,bc->ca', arr1, arr2)
assert out.shape == (dim_a, dim_c)
# Matrix vector product, with transposed output
arr2 = ndarray((dim_b,))
out = einsum('ab,b->ba', arr1, arr2)
assert out.shape == (dim_b, dim_a)

The Einstein summation format is really awesome, so it's always been disappointing that it's so much slower than equivalent calls to tensordot in numpy. The blis.einsum function gives up the numpy version's generality, so that calls can be easily mapped to Blis:

  • Only two input tensors
  • Maximum two dimensions
  • Dimensions must be labelled a, b and c
  • The first argument's dimensions must be 'a' (for 1d inputs) or 'ab' (for 2d inputs).

With these restrictions, there are ony 15 valid combinations – which correspond to all the things you would otherwise do with the gemm, gemv, ger and axpy functions. You can therefore forget about all the other functions and just use the einsum. Here are the valid einsum strings, the calls they correspond to, and the numpy equivalents:

Equation Maps to Numpy
'a,a->a' axpy(A, B) A+B
'a,b->ab' ger(A, B) outer(A, B)
'a,b->ba' ger(B, A) outer(B, A)
'ab,a->ab' batch_axpy(A, B) A*B
'ab,a->ba' batch_axpy(A, B, trans1=True) (A*B).T
'ab,b->a' gemv(A, B) A*B
'ab,a->b' gemv(A, B, trans1=True) A.T*B
'ab,ac->cb' gemm(B, A, trans1=True, trans2=True) dot(B.T, A)
'ab,ac->bc' gemm(A, B, trans1=True, trans2=False) dot(A.T, B)
'ab,bc->ac' gemm(A, B, trans1=False, trans2=False) dot(A, B)
'ab,bc->ca' gemm(B, A, trans1=False, trans2=True) dot(B.T, A.T)
'ab,ca->bc' gemm(A, B, trans1=True, trans2=True) dot(B, A.T)
'ab,ca->cb' gemm(B, A, trans1=False, trans2=False) dot(B, A)
'ab,cb->ac' gemm(A, B, trans1=False, trans2=True) dot(A.T, B.T)
'ab,cb->ca' gemm(B, A, trans1=False, trans2=True) dot(B, A.T)

We also provide fused-type, nogil Cython bindings to the underlying Blis linear algebra library. Fused types are a simple template mechanism, allowing just a touch of compile-time generic programming:

cimport blis.cy
A = <float*>calloc(nN * nI, sizeof(float))
B = <float*>calloc(nO * nI, sizeof(float))
C = <float*>calloc(nr_b0 * nr_b1, sizeof(float))
blis.cy.gemm(blis.cy.NO_TRANSPOSE, blis.cy.NO_TRANSPOSE,
             nO, nI, nN,
             1.0, A, nI, 1, B, nO, 1,
             1.0, C, nO, 1)

Bindings have been added as we've needed them. Please submit pull requests if the library is missing some functions you require.

Development

To build the source package, you should run the following command:

./bin/copy-source-files.sh

This populates the blis/_src folder for the various architectures, using the flame-blis submodule.

Updating the build files

In order to compile the Blis sources, we use jsonl files that provide the explicit compiler flags. We build these jsonl files by running Blis's build system, and then converting the log. This avoids us having to replicate the build system within Python: we just use the jsonl to make a bunch of subprocess calls. To support a new OS/architecture combination, we have to provide the jsonl file and the header.

Linux

The Linux build files need to be produced from within the manylinux1 docker container, so that they will be compatible with the wheel building process.

First, install docker. Then do the following to start the container:

sudo docker run -it quay.io/pypa/manylinux1_x86_64:latest

Once within the container, the following commands should check out the repo and build the jsonl files for the generic arch:

mkdir /usr/local/repos
cd /usr/local/repos
git clone https://github.com/explosion/cython-blis && cd cython-blis
git pull && git submodule init && git submodule update && git submodule
status
/opt/python/cp36-cp36m/bin/python -m venv env3.6
source env3.6/bin/activate
pip install -r requirements.txt
./bin/generate-make-jsonl linux generic --export
BLIS_ARCH=generic python setup.py build_ext --inplace
# N.B.: don't copy to /tmp, docker cp doesn't work from there.
cp blis/_src/include/linux-generic/blis.h /linux-generic-blis.h
cp blis/_src/make/linux-generic.jsonl /

Then from a new terminal, retrieve the two files we need out of the container:

sudo docker ps -l # Get the container ID
# When I'm in Vagrant, I need to go via cat -- but then I end up with dummy
# lines at the top and bottom. Sigh. If you don't have that problem and
# sudo docker cp just works, just copy the file.
sudo docker cp aa9d42588791:/linux-generic-blis.h - | cat > linux-generic-blis.h
sudo docker cp aa9d42588791:/linux-generic.jsonl - | cat > linux-generic.jsonl

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

blis-0.7.1.dev0.tar.gz (2.7 MB view details)

Uploaded Source

Built Distributions

blis-0.7.1.dev0-cp38-cp38-win_amd64.whl (6.3 MB view details)

Uploaded CPython 3.8 Windows x86-64

blis-0.7.1.dev0-cp38-cp38-manylinux2014_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.8

blis-0.7.1.dev0-cp38-cp38-macosx_10_9_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

blis-0.7.1.dev0-cp37-cp37m-win_amd64.whl (6.3 MB view details)

Uploaded CPython 3.7m Windows x86-64

blis-0.7.1.dev0-cp37-cp37m-manylinux2014_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.7m

blis-0.7.1.dev0-cp37-cp37m-macosx_10_9_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

blis-0.7.1.dev0-cp36-cp36m-win_amd64.whl (6.2 MB view details)

Uploaded CPython 3.6m Windows x86-64

blis-0.7.1.dev0-cp36-cp36m-manylinux2014_x86_64.whl (9.8 MB view details)

Uploaded CPython 3.6m

blis-0.7.1.dev0-cp36-cp36m-macosx_10_9_x86_64.whl (5.8 MB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

File details

Details for the file blis-0.7.1.dev0.tar.gz.

File metadata

  • Download URL: blis-0.7.1.dev0.tar.gz
  • Upload date:
  • Size: 2.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0.tar.gz
Algorithm Hash digest
SHA256 2b919b3a1f46138ec43e8008b15511d9772d89af4320485b7e37288eaff147ac
MD5 402d9e49acf882a5108ab205ed6a6248
BLAKE2b-256 935c58d98fbde44a8b89bb4af2433e488f22a6dcd041323bdd92b7694ad90359

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 6.3 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 746ea158a89e427ca120294dad483ccaebfad53cc260d39d8fe296955144a591
MD5 3f30d992a718477075dff9106690a893
BLAKE2b-256 c1dc53a768ad021475516f1b8df8a647d68e5ae0732d26862a5c0452f16ceae8

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 9.8 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ad36176e1aceeda3eea73da5b3225bfa10b3a481c9626ba813628f82f298623f
MD5 743ee73f56db10d8f29b13dec5262316
BLAKE2b-256 48b713fc0e70e31519520fb3d9d67c6bdc0037494cf97c571f795a1af2b146e4

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 5.8 MB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 0f20a0d5b1eb0df5c7e54061cc4ef71fb3e2f15963b964eddf8184869af4e339
MD5 972a236848002afb0324a5183b55917e
BLAKE2b-256 d1d13f76f8f68125438f9f5d44d9f642cf5ca473340b34d8915f51f14f2c2d10

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 6.3 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 02487cf6cc41cddc2e2a1c62bfde2e9980a468de35dd6bad7c48af5af0d5ebee
MD5 8e3e1c7d7e02801780f0c75dd942b8df
BLAKE2b-256 4e4c8fb18934662ba93cb990dc8d988cd7bfc232b4582b7b19e63a29509c48e0

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 9.8 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3df7efcea6f5d3743852524e3e6bc1e5abea8acf697b8300d82f1df28af6d479
MD5 1fd09053a6135fc8ef3dfe3206f32c9a
BLAKE2b-256 d9b922383f8d41c30189761bd31add05862a232ce5708845a1905eea549716b3

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 5.8 MB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ff2fc328c661e29b0d99e8ff2c3d27cdc92f083b5b6714d20cf45715db68a60d
MD5 03aed80360eddf925437e65694373367
BLAKE2b-256 ae34109a5e6d424cae106534cf30f4441f27056f2b46b6c1885d54d59bb77353

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 6.2 MB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 e20ce39d1ea1e279ebc0a120e145bd62ef8afea979cb90ac76070652837225eb
MD5 6715651036356b676a9a65d15f25cf67
BLAKE2b-256 d66baa042acacf28469fe13d3373e9e41812378409c6fb853bbdbd267c05a28a

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 9.8 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e878dee2407a2cd1f8af9bdcaeb8445f4e8c1c75e058b114d2c68c2835e388a1
MD5 9f60590358c590b1f9ea703db8bf5aa6
BLAKE2b-256 8533c16e3e616075fc6a61669fda14fe7a6a6da8fc113b1193f6cd700ceef97a

See more details on using hashes here.

File details

Details for the file blis-0.7.1.dev0-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: blis-0.7.1.dev0-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 5.8 MB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.8

File hashes

Hashes for blis-0.7.1.dev0-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 81a62324986cea4305276b415d856d393f2bf52880324b87d5c903a9a9e8afd7
MD5 42d950e83b37887d192ab57774cd1bca
BLAKE2b-256 74fc4b53fe449f35c40608380ed0bc80dc631942185c7ab099cf383e8c34f7fa

See more details on using hashes here.

Supported by

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