Skip to main content

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

Project description

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

Build Status pypi Version

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->bc'

gemm(A, B, trans1=True, trans2=False)

dot(A.T, B)

'ab,ac->cb'

gemm(B, A, trans1=True, trans2=True)

dot(B.T, A)

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

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

Uploaded Source

Built Distributions

blis-0.2.2-cp37-cp37m-win_amd64.whl (3.1 MB view details)

Uploaded CPython 3.7m Windows x86-64

blis-0.2.2-cp37-cp37m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.7m

blis-0.2.2-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 (3.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.2.2-cp36-cp36m-win_amd64.whl (3.0 MB view details)

Uploaded CPython 3.6m Windows x86-64

blis-0.2.2-cp36-cp36m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.6m

blis-0.2.2-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 (3.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.2.2-cp35-cp35m-win_amd64.whl (3.0 MB view details)

Uploaded CPython 3.5m Windows x86-64

blis-0.2.2-cp35-cp35m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.5m

blis-0.2.2-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 (3.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.2.2-cp27-cp27mu-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 2.7mu

blis-0.2.2-cp27-cp27m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 2.7m

blis-0.2.2-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 (3.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.2.2.tar.gz.

File metadata

  • Download URL: blis-0.2.2.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/3.6.6

File hashes

Hashes for blis-0.2.2.tar.gz
Algorithm Hash digest
SHA256 fa913c0b3f1d3bc8eda7bc4bfca3151f4f148d83a0a3539f45c6f3f1007951c3
MD5 ca935b8716efe8c07d3e08c5001a6492
BLAKE2b-256 9e8ab83ea88d69d7548b4d17863a9e500272a81ccb50539ee64e7b4e054f607d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 3.1 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.2.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 a419f48f90ebd494d7206b7f6ac6b1144825f75e1eac51c1a0463b6aead05967
MD5 5661b597eab91803c2fef6d987f3e1cd
BLAKE2b-256 b3ab75af6aa4ebc7cddb2472226430cdc623a8bb4836ee088fd93e383014cb32

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.2 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.2.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 94a11d1c4ed09de187c854abf92c4bc8ff66a316c078a193a84478e0a7305eda
MD5 a0e2f515becace19e0e842ece1b6b179
BLAKE2b-256 852de53b19a0e087a778832eaaa7490257c51aa13a27a7981470a812f6d10581

See more details on using hashes here.

File details

Details for the file blis-0.2.2-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.2.2-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 3a9315c9ba4058604880c6a9a70c703990dfbfd819b9f43affcde3cd53e1da6e
MD5 b10d0aaa9136d03a138be5acf7913c7c
BLAKE2b-256 274bc99fb6a3f221b8cd3d2d0189810268bd97bf62ba9ad64085744116ea93f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 3.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.2.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 9f113326e8f7c05e1bb9313cbf8ad25ac8035eba200c17ced40664dda50c7f55
MD5 9facfacd3426ba44587dd7f1c91c9dbe
BLAKE2b-256 3facbd9d803cf81df69827d2a0d856930d3c87e0015c2b3e9a09b5c5adae7476

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.2 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.2.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 52959f0761146869abee3c095a2314cd333f5c5a02442a5912e81580f24362c3
MD5 cf63e88837be7842b064e7e714920207
BLAKE2b-256 f0a89f771a1497b999d0c2d69782facf211e15478d58dd9fe0dfe22646e9affd

See more details on using hashes here.

File details

Details for the file blis-0.2.2-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.2.2-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 a8619889699f7847a01d4cc2bcdfff6f27e381828838ba31e08441de856670e6
MD5 6a8a3aa1a717057638180994345b192f
BLAKE2b-256 f54420ef1ee6eeb2b19fd5f46cd48a6062b3976942cf90b239195af153edc577

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp35-cp35m-win_amd64.whl
  • Upload date:
  • Size: 3.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.2.2-cp35-cp35m-win_amd64.whl
Algorithm Hash digest
SHA256 0d546a781991f1a8c4c35410818133eb22f0032c130f3b0935ac903a600d9044
MD5 6a4c9225132967c2f59d453897ddf9a9
BLAKE2b-256 e899ee9c9f7314695b835ecdd6415d21b9b8a35edcbca7d37ce24a49379c4ff5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.2 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.2.2-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 37459f1519aa2cc5d74d37b5a4c4ddc67dad21f4c13981af4664a88c3bbaec24
MD5 cfd882e6af2e2872d6fb65116b0cb200
BLAKE2b-256 26708fa9be3e41d2ced08ed0a13b0043bf033209eb9e7b7022ff0f2af5375285

See more details on using hashes here.

File details

Details for the file blis-0.2.2-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.2.2-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 5da16e03f37be979d6eb799a1b70446a986fa1b64fd6288ee61e5e432f93a1b2
MD5 887b156b3e949d798881edfc3f273c99
BLAKE2b-256 67e07c2984dfcccc7375dc6695f78361b2563b19661cb12a45a5d4735b5a55f3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp27-cp27mu-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.2 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.2.2-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0fdf4ada49eeb8efd211f16df9e2ea54ddac74688c2b76175264bef235cee7d7
MD5 6eeb93dcdfae843937cd4d525f4a1a3c
BLAKE2b-256 ec794591f5ac8cc0ac7afffdd331f7c448e169a8c45221fee10c55482f97a9f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.2-cp27-cp27m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 3.2 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.2.2-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 eb6bbe52202cd7635f4ae6d272da194b52eb16ace1a1632ac5cbeec35ebbe5fd
MD5 6d389513ac02abb4fad9f60063d92937
BLAKE2b-256 1e5fe8cfb3e15af8dba050c4d8daacf04ac6bee11ad51773e79b174b00290856

See more details on using hashes here.

File details

Details for the file blis-0.2.2-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.2.2-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 d7c8252ce50258772a84b5807146a67ccb29756cb283185cbfa59c112339444a
MD5 b05c36fdea6d39a13630edfa23238629
BLAKE2b-256 7d177631f595dd3cbd939c34ad2f43543b7a3dffbae8bc43a1b01a447f24276e

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