qalis
qalis

Reputation: 1523

Adding Intel MKL and MKL-DNN in Docker

I have ML code (e.g. Numpy, Scipy, LightGBM, PyTorch) deployed with Docker. I am using Python with Poetry, installing packages with pip.

What should I do in order to use MKL and MKL-DNN? I know that the most standard way is to use Anaconda, but I cannot (large business, without commercial Anaconda license).

Will pip install mkl suffice?

How to install MKL-DNN, so that PyTorch will use it?

Upvotes: 2

Views: 2060

Answers (2)

Adrien Pacifico
Adrien Pacifico

Reputation: 1979

I tried to add MKL to my docker container (debian based) reading intel documentation: I failed.

However, there is a docker image OneAPI docker image that comes with numpy (1.21 which is eight month old) and mkl as default BLAS. Here is what numpy returns on my machine (a laptop with a i7-i10875H )

>>> import numpy as np
>>> np.__config__.show()
blas_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/opt/intel/oneapi/intelpython/latest/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/opt/intel/oneapi/intelpython/latest/include']
blas_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/opt/intel/oneapi/intelpython/latest/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/opt/intel/oneapi/intelpython/latest/include']
lapack_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/opt/intel/oneapi/intelpython/latest/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/opt/intel/oneapi/intelpython/latest/include']
lapack_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/opt/intel/oneapi/intelpython/latest/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/opt/intel/oneapi/intelpython/latest/include']
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2,SSE3,SSSE3,SSE41,POPCNT,SSE42
    found = 
    not found = AVX512_ICL

However, I tried with anaconda and an basic docker image, and to my surprise, the anaconda virtual env used the CBLAS and my docker image used the Openblas BLAS.

I did not perform benchmarks, but since the mkl implementation uses all instruction set architecture except AVX512_ICL, I would expect it to be faster.

Anaconda

I was also surprise to test that in my anaconda environment, and to my surprise, the blas is not mkl.

$ conda create -n test numpy --yes
[...]
$ conda activate test

>>> import numpy as np
>>> np.__config__.show()
blas_info:
    libraries = ['cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/adrienpacifico/anaconda3/envs/test/lib']
    include_dirs = ['/home/adrienpacifico/anaconda3/envs/test/include']
    language = c
    define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
    define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
    libraries = ['cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/adrienpacifico/anaconda3/envs/test/lib']
    include_dirs = ['/home/adrienpacifico/anaconda3/envs/test/include']
    language = c
lapack_info:
    libraries = ['lapack', 'blas', 'lapack', 'blas']
    library_dirs = ['/home/adrienpacifico/anaconda3/envs/test/lib']
    language = f77
lapack_opt_info:
    libraries = ['lapack', 'blas', 'lapack', 'blas', 'cblas', 'blas', 'cblas', 'blas']
    library_dirs = ['/home/adrienpacifico/anaconda3/envs/test/lib']
    language = c
    define_macros = [('NO_ATLAS_INFO', 1), ('HAVE_CBLAS', None)]
    include_dirs = ['/home/adrienpacifico/anaconda3/envs/test/include']
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2,SSE3
    found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
    not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_KNM,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL

My base environment uses openblas.

My docker image based on python image --> Openblas

Dockerfile:

FROM python:3.10
ENV SHELL=/bin/bash
RUN apt-get update && \
    apt-get install build-essential
RUN apt-get install -y sudo libaio1 wget unzip htop
RUN pip install numpy
openblas64__info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
    runtime_library_dirs = ['/usr/local/lib']
blas_ilp64_opt_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
    runtime_library_dirs = ['/usr/local/lib']
openblas64__lapack_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
    runtime_library_dirs = ['/usr/local/lib']
lapack_ilp64_opt_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
    runtime_library_dirs = ['/usr/local/lib']
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2,SSE3
    found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
    not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_KNM,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL

Upvotes: 1

FlyingTeller
FlyingTeller

Reputation: 20482

Will pip install mkl suffice?

No, it will not, see the section in the numpy install docs:

The NumPy wheels on PyPI, which is what pip installs, are built with OpenBLAS. The OpenBLAS libraries are included in the wheel. This makes the wheel larger, and if a user installs (for example) SciPy as well, they will now have two copies of OpenBLAS on disk.

So you will need to built numpy from source.

I know that the most standard way is to use Anaconda, but I cannot (large business, without commercial Anaconda license).

Have you considered using miniforge and miniconda? IANAL, but I am quite certain that you are just not allowed to use the ana-/miniconda distributions and the anaconda channel in large scale commercial products, but conda-forge can still be used free of charge. You should be able to set up all the requirements that you mentioned from conda-forge. At least you would probably have an easier time compiling pytorch from source

Upvotes: 1

Related Questions