TensorFlow如何充分使用所有CPU核数,提高TensorFlow的CPU使用率,以及Intel的MKL加速

许多朋友使用服务器时,碰巧服务器没有安装GPU或者GPU都被占满了。可是,服务器有很多CPU都是空闲的,其实,把这些CPU都充分利用起来,也可以有不错的训练效果。

但是,如果你是用CPU版的TF,有时TensorFlow并不能把所有CPU核数使用到,这时有个小技巧David 9要告诉大家:

with tf.Session(config=tf.ConfigProto(
  device_count={"CPU":12},
  inter_op_parallelism_threads=1,
  intra_op_parallelism_threads=1,
  gpu_options=gpu_options,
)) as sess:

在Session定义时,ConfigProto中可以尝试指定下面三个参数:

  1. device_count, 告诉tf Session使用CPU数量上限,如果你的CPU数量较多,可以适当加大这个值
  2. inter_op_parallelism_threadsintra_op_parallelism_threads告诉session操作的线程并行程度,如果值越小,线程的复用就越少,越可能使用较多的CPU核数。如果值为0,TF会自动选择一个合适的值。

David 9亲自试验,训练似乎有1倍速度的提高。

另外,有的朋友的服务器上正好都是Intel的CPU,很可能需要把Intel的MKL包编译进TensorFlow中,以增加训练效率。这里David 9把MKL编译进TF的关键点也指出一下。

官方的指导是如下3 步

  1. Run “./configure” from the TensorFlow source directory, and it will download latest Intel MKL for machine learning automatically in tensorflow/third_party/mkl/mklml if you select the options to use Intel MKL
  2. Execute the following commands to create a pip package that can be used to install the optimized TensorFlow build.
    • PATH can be changed to point to a specific version of GCC compiler:
      export PATH=/PATH/gcc/bin:$PATH
    • LD_LIBRARY_PATH can also be changed to point to new GLIBC :
      export LD_LIBRARY_PATH=/PATH/gcc/lib64:$LD_LIBRARY_PATH.
    • Build for best performance on Intel Xeon and Intel Xeon Phi processors:
      bazel build --config=mkl --copt="-DEIGEN_USE_VML" -c opt //tensorflow/tools/pip_package:
      build_pip_package

3. Install the optimized TensorFlow wheel

bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/path_to_save_wheel
pip install --upgrade --user ~/path_to_save_wheel /wheel_name.whl

与官网编译TF的大致流程类似,就是先./configure,再用bazel编译TensorFlow。

最后用编译好的bazel工具生成whl的包,用来安装pip TensorFlow。

唯一的不同要注意用–config=mkl的选项编译TensorFlow:

bazel build –config=mkl –copt=”-DEIGEN_USE_VML” -c opt //tensorflow/tools/pip_package: build_pip_package

这样,用pip安装完成TF后,mkl就集成在TF中了。

 

参考文献:

  1. https://software.intel.com/es-es/articles/tensorflow-optimizations-on-modern-intel-architecture
  2. https://richardstechnotes.wordpress.com/2016/08/09/encouraging-tensorflow-to-use-more-cores/
  3. https://www.tensorflow.org/install/install_sources

 

本文采用署名 – 非商业性使用 – 禁止演绎 3.0 中国大陆许可协议进行许可。著作权属于“David 9的博客”原创,如需转载,请联系微信: david9ml,或邮箱:yanchao727@gmail.com

或直接扫二维码:

发布者

David 9

邮箱:yanchao727@gmail.com 微信: david9ml

《TensorFlow如何充分使用所有CPU核数,提高TensorFlow的CPU使用率,以及Intel的MKL加速》上有2条评论

  1. 您好作者,我非常非常喜欢人工智能研发,当我听到这个词,了解这个行业,在网上开始看些视频教程学习的时候,我就越发兴奋! 我已经准备在今年过年的时候离职,专门去学习并尝试入行人工智能研发,请问在学习之后如何才能找到比较对口的工作

    1. 现在AI方向的工作需求非常大,主要还是你能不能胜任,可以选一个细分方向,从简单的kaggle项目做起。
      如需要指导,可联系我的微信:david9ml

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注