构建TensorFlow时无法链接MKL

时间:2019-04-13 21:04:35

标签: tensorflow gcc intel-mkl

我尝试使用MKL构建tensorflow v1.13。构建成功执行,正确创建了pip包,但是当我测试该包时,显然最终没有使用MKL,即当我在Python 3.6.8中运行时:

import tensorflow
print("Is MKL enabled?{}".format(tensorflow.pywrap_tensorflow.IsMklEnabled()))

它返回“ False”

我在以下conda环境中使用CentOS 7:

# Name                    Version                   Build  Channel
blas                      1.0                         mkl  
ca-certificates           2019.1.23                     0  
certifi                   2018.1.18                py36_2    intel
cloog                     0.18.0                        0  
cython                    0.29.6           py36he6710b0_0  
gcc                       4.8.5                         7  
gmp                       6.1.2                h6c8ec71_1  
icc_rt                    2019.3                intel_199    intel
intel-openmp              2019.3                intel_199    intel
intelpython               2019.3                        0    intel
isl                       0.12.2                        0  
keras-applications        1.0.7                    pypi_0    pypi
keras-preprocessing       1.0.9                    pypi_0    pypi
libedit                   3.1.20181209         hc058e9b_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 8.2.0                hdf63c60_1  
libgfortran-ng            7.3.0                hdf63c60_0  
libstdcxx-ng              8.2.0                hdf63c60_1  
mkl                       2019.3                intel_199    intel
mkl-dnn                   0.14                          2    intel
mkl_fft                   1.0.11           py36h7b7c402_0    intel
mkl_random                1.0.2            py36h7b7c402_4    intel
mock                      2.0.0                    py36_0  
mpc                       1.0.3                hec55b23_5  
mpfr                      3.1.5                h11a74b3_2  
ncurses                   6.1                  he6710b0_1  
numpy                     1.16.2           py36h7e9f1db_0  
numpy-base                1.16.2           py36hde5b4d6_0  
openmp                    2018.0.3                intel_0    intel
openssl                   1.1.1b               h7b6447c_1  
pbr                       5.1.3                      py_0  
pip                       19.0.3                   py36_0  
python                    3.6.8                h0371630_0  
readline                  7.0                  h7b6447c_5  
setuptools                40.8.0                   py36_0  
six                       1.12.0                   py36_0  
sqlite                    3.27.2               h7b6447c_0  
tbb                       2019.4                intel_199    intel
tk                        8.6.8                hbc83047_0  
wheel                     0.33.1                   py36_0  
xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               h7b6447c_3  

Bazel版本为0.23.0 GCC版本为7.3.1(请参见下文)

我使用以下命令行构建tensorflow包:

scl enable devtoolset-7 bash
bazel build --config=mkl --config=opt --copt=-march=x86-64 --copt=-mavx --copt=-msse4.1 --copt=-msse4.2 --copt=-O2 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" //tensorflow/tools/pip_package:build_pip_package
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

很显然,发生了许多警告,但它们都与MKL没有关系。值得注意的是,我在这里使用GCC7。我尝试了GCC4和GCC5。我在用 那并没有改变问题。有人对为什么未链接MKL有任何线索吗?

1 个答案:

答案 0 :(得分:1)

由于tensorflow version == 1.13是最新版本,它可能不稳定(https://www.tensorflow.org/versions/

使用tensorflow version == 1.11并通过以下步骤可以很好地进行安装。

git clone https://github.com/tensorflow/tensorflow 
cd tensorflow 
git checkout r1.11 
conda create -n <conda_environment_name> python=3.6
source activate <conda_environment_name> 
conda install numpy bazel  
conda install -c intel mkl-dnn  
conda install -c conda-forge keras-preprocessing

通过在TensorFlow源代码树的根目录中运行以下命令来配置系统构建:

$./configure

支持Intel®MKL-DNN的bazel构建命令。

 bazel build --config=mkl -c opt //tensorflow/tools/pip_package:build_pip_package 

创建目录tfwheels来放置wheel文件并运行以下命令

mkdir tfwheels 
bazel-bin/tensorflow/tools/pip_package/build_pip_package /home/<your_path>/tfwheels/tensorflow_pkg
pip install /home/<your_path>/tfweels/tensorflow_pkg/tensorflow-<version>-cp36-cp36m-linux_x86_64.whl

对于tensorflow版本== 1.12(类似步骤)

参考 https://software.intel.com/en-us/articles/intel-optimization-for-tensorflow-installation-guide

希望这会有所帮助。