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).

Travis Appveyor pypi Version Python wheels

Overview

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.

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.3.1.tar.gz (1.7 MB view details)

Uploaded Source

Built Distributions

blis-0.3.1-cp37-cp37m-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.7m Windows x86-64

blis-0.3.1-cp37-cp37m-manylinux1_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.7m

blis-0.3.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.7m macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64

blis-0.3.1-cp36-cp36m-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.6m Windows x86-64

blis-0.3.1-cp36-cp36m-manylinux1_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.6m

blis-0.3.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.6m macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64

blis-0.3.1-cp35-cp35m-win_amd64.whl (5.0 MB view details)

Uploaded CPython 3.5m Windows x86-64

blis-0.3.1-cp35-cp35m-manylinux1_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.5m

blis-0.3.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.0 MB view details)

Uploaded CPython 3.5m macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64

blis-0.3.1-cp27-cp27mu-manylinux1_x86_64.whl (3.7 MB view details)

Uploaded CPython 2.7mu

blis-0.3.1-cp27-cp27m-manylinux1_x86_64.whl (3.7 MB view details)

Uploaded CPython 2.7m

blis-0.3.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (4.0 MB view details)

Uploaded CPython 2.7m macOS 10.10+ Intel (x86-64, i386) macOS 10.10+ x86-64 macOS 10.6+ Intel (x86-64, i386) macOS 10.9+ Intel (x86-64, i386) macOS 10.9+ x86-64

File details

Details for the file blis-0.3.1.tar.gz.

File metadata

  • Download URL: blis-0.3.1.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.6

File hashes

Hashes for blis-0.3.1.tar.gz
Algorithm Hash digest
SHA256 1c6f0e338163adcfa9921ebe99c925b4fec775947eb397e0a89538bc7d0057be
MD5 bdc5c243d65cf73479366d5963c00717
BLAKE2b-256 b222d4e4d8ffb0447b71f8dde1f2b8ef68c4914a963b127511a2258627063323

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: blis-0.3.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 5.0 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 e6beedfcfddd34c13ee706975979abd5f25f132e675d105539a53c437aaf11f1
MD5 15ef56ea91787bc376f699cac135d725
BLAKE2b-256 e212302c9b5f2536c92444479de57481288eac987e58f8d849599dca04389e62

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: blis-0.3.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 313c7c44ad45383ca88536a32d460353d3c3053f2b66615bf67a214486ef90c5
MD5 9dae2fa779b345097cc1aee96a35254f
BLAKE2b-256 23142c43ec36db2dbc8f522c91bfc8f0eae0ff987a1ca700233c4dea59502afb

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for blis-0.3.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 2887340c5344367b992b7a00fac53fa76978d381003e8470488b359060b4963b
MD5 6e179c2cf0dc3445d73f593f47f7fbbf
BLAKE2b-256 b566ca72e3798d2b7f3f17e2cee58cff609eb046888b4ef60a9ebba48fc14898

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: blis-0.3.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 5.0 MB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 0b30c3922b84ca25a8c696b82950955b69b4f3ce56c22bc12325612c057fedb0
MD5 758d3ad1b8d02d08061aa08ede1a2e40
BLAKE2b-256 db8f6bd0610c93ce586c83f7f6e505de9cddc2e9498e67eed1af65480a6d7e30

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: blis-0.3.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5f8ae83a1703ac0db8eab00b07579c8d07236fafa6c609fe2a102a725504f453
MD5 d1ac84962900be1330c4b4d4bf5f045d
BLAKE2b-256 6f968fd784ab6e76cadd2b2dc8da3a74fddfafc8ef80004a6cb36597611797d3

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for blis-0.3.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 cbc7e945b4d0b0d8ee21e0b80d538a5c64db2e8fbd31fbd8826a8cdeb5b2e5f7
MD5 63f512cef7d014a09a364ddea5c007bd
BLAKE2b-256 14a54a0dcf5018b86bb688d73f35aaae57d4a919803374aadd84121256731ec2

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp35-cp35m-win_amd64.whl.

File metadata

  • Download URL: blis-0.3.1-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 5.0 MB
  • Tags: CPython 3.5m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 58b4a840aa27f227345681155b3d683e852f1eaaede23bdd60fcc92aa8fc79ac
MD5 c9d7e16959395f2f0e375c456a0d143b
BLAKE2b-256 e8a2a789663d739812c99dc225a231d92b12b99ea96f17d2b5c94db5689ab28e

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: blis-0.3.1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bed7866ce91ad3957118cbc332863b1f26b5fb3092e5c4c1318947e8e126fba3
MD5 7f63561dee2b84ac6b7eb58eccb59c42
BLAKE2b-256 2de5aee1dd904455623d88396cd2cbe42d33464dc62993e9fbfeb7d784585418

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for blis-0.3.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 0f57d24c2278cf293267f636d51d14d743d691b10fc2b44c97ea7c1668e4fe28
MD5 49d2e008736996f8291dbd72a092a91f
BLAKE2b-256 f6473aa315e1828cd62b3bea97af026a5d10c27b6a7e6c92ae5cd02e0e47476e

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

  • Download URL: blis-0.3.1-cp27-cp27mu-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 2.7mu
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 4277e82401a544fbdd78cbda92c382a7a013bd912ea34fc7bf6e9682084cd721
MD5 064024fc14f5ae62472d6b044649c1aa
BLAKE2b-256 f82347bca4f790e150fd030c4c4d67f19174f9efe5447b8564ba366bb2976366

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp27-cp27m-manylinux1_x86_64.whl.

File metadata

  • Download URL: blis-0.3.1-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.7 MB
  • Tags: CPython 2.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for blis-0.3.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6a5c31eea061481fd57c22551b045b9d290a72a7b68740fb67c5ecea92b44100
MD5 4bc9aeaf7f8712914f8b8645dcab9ef0
BLAKE2b-256 a78a50be52bf457634eb5bf15533bbbc489d3e4f186de03443a79975c334f80a

See more details on using hashes here.

File details

Details for the file blis-0.3.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl.

File metadata

File hashes

Hashes for blis-0.3.1-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Algorithm Hash digest
SHA256 fa543271bcdb564062cc89bb29497610065fb0730326e0cdd99ab913bee2e68a
MD5 69d1c80cc0dbb092eb94b241c42c5ac7
BLAKE2b-256 f0c43aeb3f0231c06e300c85d6fc3372f9838d45b3c3faa239eb6cf76abc0355

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