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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.7m

blis-0.2.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 (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.1-cp36-cp36m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.6m

blis-0.2.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 (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.1-cp35-cp35m-manylinux1_x86_64.whl (3.2 MB view details)

Uploaded CPython 3.5m

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

Uploaded CPython 2.7mu

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

Uploaded CPython 2.7m

blis-0.2.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 (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.1.tar.gz.

File metadata

  • Download URL: blis-0.2.1.tar.gz
  • Upload date:
  • Size: 1.5 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.1.tar.gz
Algorithm Hash digest
SHA256 6fad9ef57107f029b7b5410695acd9c16fccccfa51c20da11f310437d8f329f3
MD5 0df5ba7cad27bb8791c14472dbe3d3ba
BLAKE2b-256 5d5e3fb863f66c862dab3631e97c0f7f202b0a5ba21ceb4a436a286c370ee144

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.1-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.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cc50aabc22ea0e235b81e2f8be4987b29489d7fc0fadf02b2350ab2b18c3c80e
MD5 1cf1b1a77af5039dda868532400a8d17
BLAKE2b-256 6b8aa3644e1a73b677f5e0319afc676c4679b058199668bfca26547c0fc9247e

See more details on using hashes here.

File details

Details for the file blis-0.2.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.2.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 df3d416e104a89d97967293471dfedd2cd3c2611738e0ae649cb48ce0efcfd54
MD5 2f99d217b2809c6fc67419369965693e
BLAKE2b-256 3b313b7a003725d8b86779bb67216feac738bc46482df2ab5b585f0226a9a283

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.1-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.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c93e2cdb08893e43f5239f3a3d82ec57c415e9c9a5ff163c988b6273c7b5fd72
MD5 e75bfda01ce7209e91fe070d088273d4
BLAKE2b-256 87c64a7e804f524ac89d8bf0a25dba768b05cd918d163068a7a80c42366f27d3

See more details on using hashes here.

File details

Details for the file blis-0.2.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.2.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 46b1e5f3c36525d7125d4045a3537983b12c9aaa293eac9b1b070982b212cf20
MD5 c8300eee94025a310910e7e05e16bc7e
BLAKE2b-256 44f7ad555bea0593df19076ef4d7d589d044e19030719a9bf72986027254a040

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.1-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.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9f11039a9d2cbcaddcb76e301c622557c403b8e38b3f5135af14f931894a8ee8
MD5 336e2b4a012c383ee434ca1cd2b6dff8
BLAKE2b-256 f02afc91766f8499fcbf7e6c53d786bb3e73da71b6e6ff0fd1ee00603f9f5586

See more details on using hashes here.

File details

Details for the file blis-0.2.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.2.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 c1c3ab69edd6bbc1d9a51bcc42404ff5e5ec1b6bd5bc30d003a9ee33bf0d578e
MD5 672161bfae140068a76e2b438db00cdb
BLAKE2b-256 e2bd13c705062d168fbc3814330bdb5d9e6ce14031fb03ea51df33065525f34b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.1-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.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a31061d309a66f824f5519e9ccd64ccb8b815771ecbf5bdf8d05cc628c806d6c
MD5 f22c0a830b0ff1be742c96ef88e7976a
BLAKE2b-256 6566d6c8784ccd702c13317d9feea9ac81e8c6021760bb3fe5b9b32cc89eff97

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blis-0.2.1-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.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ba152e13214d323a167ff34f7e273630f8533c478635604490b36574cf645711
MD5 733affc0de28c32b28d38e094f708518
BLAKE2b-256 8f249cb8b014a70f272dbd5dd047b2d7cc8da9ed020444bc5d5b0fa08c03267d

See more details on using hashes here.

File details

Details for the file blis-0.2.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.2.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 d730d4462e3bd1df777b79660c4e6ac7a6d58aac4adeb98d348512aa292e0af9
MD5 2c2b4d9f756e186bb0f97f10cf7e08f8
BLAKE2b-256 0bce0dff2abd63d572e98f17a2c6fcf8f3dda66d7f8d574ff0454f071cfd9c15

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