您的位置:首页 > 大数据 > 人工智能

现代英特尔® 架构上的 TensorFlow* 优化——正如去年参加Intel AI会议一样,Intel自己提供了对接自己AI CPU优化版本的Tensorflow,下载链接见后,同时可以基于谷歌官方的tf版本直接编译生成安装包

2018-01-19 17:58 1416 查看

现代英特尔® 架构上的 TensorFlow* 优化

转自:https://software.intel.com/zh-cn/articles/tensorflow-optimizations-on-modern-intel-architecture

英特尔:Elmoustapha Ould-Ahmed-Vall,Mahmoud Abuzaina,Md Faijul Amin,Jayaram Bobba,Roman S Dubtsov,Evarist M Fomenko,Mukesh Gangadhar,Niranjan Hasabnis,Jing Huang,Deepthi Karkada,Young Jin Kim,Srihari Makineni,Dmitri Mishura,Karthik Raman,AG Ramesh,Vivek V Rane,Michael Riera,Dmitry Sergeev,Vamsi Sripathi,Bhavani Subramanian,Lakshay Tokas,Antonio C Valles

谷歌:Andy Davis,Toby Boyd,Megan Kacholia,Rasmus Larsen,Rajat Monga,Thiru Palanisamy,Vijay Vasudevan,Yao Zhang

作为一款领先的深度学习和机器学习框架,TensorFlow* 对英特尔和谷歌发挥英特尔硬件产品的最佳性能至关重要。本文 向人工智能 (AI) 社区介绍了在基于英特尔® 至强和英特尔® 至强融核™ 处理器的平台上实施的 TensorFlow* 优化。在去年举办的首届英特尔人工智能日上,英特尔公司的柏安娜和谷歌的 Diane Green 共同宣布了双方的合作,这些优化是英特尔和谷歌工程师密切合作取得的成果。

我们介绍了在优化实践中遇到的各种性能挑战以及采用的解决方法,还报告了对通用神经网络模型示例的性能改进。这些优化带来了多个数量级的性能提升。例如,根据我们的测量,英特尔® 至强融核™ 处理器 7250 (KNL) 上的训练性能提升了高达 70 倍,推断性能提升了高达 85 倍。基于英特尔® 至强® 处理器 E5 v4 (BDW) 和英特尔至强融核处理器 7250 的平台为下一代英特尔产品奠定了基础。用户尤其希望今年晚些时候推出的英特尔至强(代号为 Skylake)和英特尔至强融合(代号为 Knights Mill)处理器将提供显著的性能提升。

在现代 CPU 上优化深度学习模型的性能面临众多挑战,和优化高性能计算 (HPC) 中其他性能敏感型应用所面临的挑战差别不大:

需要重构代码,以利用现代矢量指令。这意味着将所有关键基元(如卷积、矩阵乘法和批归一化)被向量化为最新 SIMD 指令(英特尔至强处理器为 AVX2,英特尔至强融核处理器为d AVX512)。

要想实现最佳性能,需要特别注意高效利用所有内核。这意味着在特定层或操作实施并行化以及跨层的并行化。

根据执行单元的需要,提供尽可能多的数据。这意味着需要平衡使用预取、缓存限制技术和改进空间和时间局部性的数据格式。

为了满足这些要求,英特尔开发了众多优化型深度学习基元,计划应用于不同的深度学习框架,以确保通用构建模块的高效实施。除了矩阵乘法和卷积以外,创建模块还包括:

直接批量卷积

内积

池化:最大、最小、平均

标准化:跨通道局部响应归一化 (LRN),批归一化

激活:修正线性单元 (ReLU)

数据操作:多维转置(转换)、拆分、合并、求和和缩放。

请参阅 本文,获取关于面向深度神经网络的英特尔® 数学核心函数(英特尔® MKL-DNN)的优化基元的更多详情。

在 TensorFlow 中,我们实施了英特尔优化版运算,以确保这些运算能在任何情况下利用英特尔 MKL-DNN 基元。同时,这也是支持英特尔® 架构可扩展性能的必要步骤,我们还需要实施大量其他优化。特别是,因为性能原因,英特尔 MKL 使用了不同于 TensorFlow 默认布局的另一种布局。我们需要最大限度地降低两种格式的转换开销。我们还想确保数据科学家和其他 TensorFlow 用户不需要改变现有的神经网络模型,便可使用这些优化。



图形优化

我们推出了大量图形优化通道,以:

在 CPU 上运行时,将默认的 TensorFlow 操作替换为英特尔优化版本。确保用户能运行现有的 Python 程序,在不改变神经网络模型的情况下提升性能。

消除不必要且昂贵的数据布局转换。

将多个运算融合在一起,确保在 CPU 上高效地重复使用高速缓存。

处理支持快速向后传播的中间状态。

这些图形优化进一步提升了性能,没有为 TensorFlow 编程人员带来任何额外负担。数据布局优化是一项关键的性能优化。对于 CPU 上的某些张量运算而言,本地 TensorFlow 数据格式通常不是最高效的数据布局。在这种情况下,将来自 TensorFlow 本地格式的数据布局转换运算插入内部格式,在 CPU 上执行运算,并将运算输出转换回 TensorFlow 格式。但是,这些转换造成了性能开销,应尽力降低这些开销。我们的数据布局优化发现了能利用英特尔 MKL 优化运算完全执行的子图,并消除了子图运算中的转换。自动插入的转换节点在子图边界执行数据布局转换。融合通道是另一个关键优化,它将多个运算自动融合为高效运行的单个英特尔 MKL 运算。

其他优化

我们还调整众多 TensorFlow 框架组件,以确保在各种深度学习模型上实现最高的 CPU 性能。 我们使用 TensorFlow 中现成的池分配器开发了一款自定义池分配器。 我们的自定义池分配器确保了 TensorFlow 和英特尔 MKL 共享相同的内存池(使用英特尔 MKL imalloc 功能),不必过早地将内存返回至操作系统,因此避免了昂贵的页面缺失和页面清除。 此外,我们还认真优化了多个线程库(TensorFlow 使用的 pthread 和英特尔 MKL 使用的 OpenMP),使它们能共存,而不是互相争夺 CPU 资源。

性能实验

我们的优化(如上述优化)在英特尔至强和英特尔至强融核平台上实现了显著的性能提升。 为了更好地展示性能改进,我们提供了以下最佳方法(或 BKM)和 3 个通用 ConvNet 性能指标评测的基准和优化性能值。

以下参数对英特尔至强(代号为 Broadwell)和英特尔至强融核(代号为 Knights Landing)的性能非常重要,建议您针对特定的神经网络模型和平台优化这些参数。 我们认真优化了这些参数,力求在英特尔至强和英特尔至强融核处理器上获得 convnet 性能指标评测的最佳性能。
数据格式:建议用户针对特定的神经网络模型指定 NCHW 格式,以实现最佳性能。 TensorFlow 默认的 NHWC 格式不是 CPU 上最高效的数据布局,将带来额外的转换开销。

Inter-op / intra-op:建议数据科学家和用户在 TensorFlow 中试验 intra-op 和 inter-op 参数,为每个模型和 CPU 平台搭配最佳设置。这些设置将影响某层或跨层的并行性。

批处理大小 (Batch size):批处理大小是影响可用并行性(以使用全部内核)、工作集大小和总体内存性能的另一个重要参数。

OMP_NUM_THREADS:最佳性能需要高效使用所有可用内核。由于该设置控制超线程等级(1 到 4),因此,对英特尔至强融核处理器的性能尤为重要。

矩阵乘法中的转置 (Transpose in Matrix multiplication):对于某些矩阵大小,转置第二个输入矩阵 b 有助于改进 Matmul 层的性能(改进高速缓存的重复使用)。以下 3 个模型所用的所有 Matmul 运算亦是如此。用户应在其他尺寸的矩阵中试验该设置。

KMP_BLOCKTIME:用户应试验各种设置,以确定每个线程完成并行区域执行后等待的时间,单位为毫秒。

英特尔® 至强® 处理器(代号为 Broadwell - 双插槽 - 22 个内核)上的示例设置



英特尔® 至强融核™ 处理器(代号为 Knights Landing - 68 个内核)上的示例设置



英特尔® 至强® 处理器(代号为 Broadwell – 双插槽 – 22 个内核)的性能结果



英特尔® 至强融核™ 处理器(代号为 Knights Landing – 68 个内核)的性能结果



英特尔® 至强® 处理器(代号为 Broadwell)和英特尔® 至强融核™ 处理器(代号为 Knights Landing)上不同批处理尺寸的性能结果 - 训练







利用 CPU 优化安装 TensorFlow

按照“现已推出英特尔优化型 TensorFlow 系统” 中的指令安装包含 pip或 conda 的预构建二进制软件包,或按照以下指令从源构建:

运行 TensorFlow 源目录中的 "./configure",如果您选择了使用英特尔 MKL 的选项,将自动下载 tensorflow/third_party/mkl/mklml 中的面向机器学习的最新版英特尔 MKL。

执行以下命令创建 pip程序包,以安装经过优化的 TensorFlow 创建。

可更改 PATH,使其指向特定 GCC 编译器版本:
export PATH=/PATH/gcc/bin:$PATH

也可以更改 LD_LIBRARY_PATH,使其指向新 GLIBC:
export LD_LIBRARY_PATH=/PATH/gcc/lib64:$LD_LIBRARY_PATH.

专为在英特尔至强和英特尔至强融核处理器上实现最佳性能而创建:
bazel build --config=mkl --copt=”-DEIGEN_USE_VML” -c opt //tensorflow/tools/pip_package:
build_pip_package

安装优化版 TensorFlow 系统
bazel-bin/tensorflow/tools/pip_package/build_pip_package ~/path_to_save_wheel
pipinstall --upgrade --user ~/path_to_save_wheel /wheel_name.whl

系统配置



对人工智能意味着什么

优化 TensorFlow 意味着高度可用、广泛应用的框架创建的深度学习应用现在能更快速地运行于英特尔处理器,以扩大灵活性、可访问性和规模。 例如,英特尔至强融核处理器能以近乎线性的方式跨内核和节点横向扩展,可显著减少训练机器学习模型的时间。 我们不断增强英特尔处理器的性能,以处理更大、更困难的人工智能工作负载,TensorFlow 也能随着性能的进步而升级。

英特尔和谷歌共同优化 TensorFlow 的合作体现了双方面向开发人员和数据科学家普及人工智能的不懈努力,力求在从边缘到云的所有设备上随时运行人工智能应用。 英特尔相信这是创建下一代人工智能算法和模型的关键,有助于解决最紧迫的业务、科学、工程、医学和社会问题。

本次合作已经在基于英特尔至强和英特尔至强融核处理器的领先平台上实现了显著的性能提升。 这些优化现已在谷歌的 TensorFlow GitHub 存储库中推出。 我们建议人工智能社区尝试这些优化,并期待获得基于优化的反馈与贡献。

转自:https://software.intel.com/zh-cn/articles/intel-optimized-tensorflow-wheel-now-available#

Intel Optimized Tensorflow Wheel Now Available

Intel's Tensorflow optimizations are now available for Linux as a wheel installable through pip.

For more information on the optimizations as well as performance data, see this blog post.

To install the wheel into an existing Python installation, simply run

# Python 2.7
pip
install
https://anaconda.org/intel/tensorflow/1.4.0/download/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl
# Python 3.5
pip
install
https://anaconda.org/intel/tensorflow/1.3.0/download/tensorflow-1.3.0-cp35-cp35m-linux_x86_64.whl
# Python 3.6
pip
install
https://anaconda.org/intel/tensorflow/1.4.0/download/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl
Edit 10/12/17: Wheel paths have been updated to 1.3.0

Edit 11/22/17: Wheel paths have been updated to 1.4.0

To create a conda environment with Intel Tensorflow that also takes advantage of the Intel Distribution for Python’s optimized numpy, run

conda create -n tf -c intel python=<2|3> pipnumpy
. activate tf
# Python 3.6
pip
install
https://anaconda.org/intel/tensorflow/1.4.0/download/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl
# Python 2.7
pip
install
https://anaconda.org/intel/tensorflow/1.4.0/download/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl

Conda Package Now Available in Intel Python 2018

A conda package of Intel's optimized Tensorflow comes with the new 2018 Intel Python distribution on Linux. You can also create a conda environment with Intel Optimized Tensorflow with the following commands:

conda create -n intel_tf -c intel --override-channels tensorflow
source
activate intel_tf
参考:https://software.intel.com/en-us/articles/build-and-install-tensorflow-on-intel-architecture

可以直接用谷歌官方的tf来编译支持Intel的mkl CPU。

Building a TensorFlow* Pip Package for Installation

If the program Git* is not currently installed on your system, issue the following command:

sudo apt install git


Clone the GitHub repository by issuing the following command:

git clone https://github.com/tensorflow/tensorflow[/code] 
The tensorflow directory created during cloning contains a script named configure that must be executed prior to creating the pippackage and installing TensorFlow. This script allows you to identify the pathname, dependencies, and other build configuration options. For TensorFlow optimized on Intel architecture, this script also allows you to set up Intel® Math Kernel Library (Intel® MKL) related environment settings. Execute the following commands:

cd tensorflow

./configure


Important: Select ‘Y’ to build TensorFlow with Intel MKL support, and ‘Y’ to download MKL LIB from the web.
Select the default settings for the other configuration parameters.
When the script has completed running, issue the following command to
build the pippackage:

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

bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg


Installing TensorFlow—Native Pip Option

At this point in the process the newly created pippackage will be located in tmp/tensorflow_pkg.
The next step is to install TensorFlow, which can be done either as a
native pipinstallation, or in an Anaconda* virtual environment as
described in the next section. For a native pipinstallation simply
enter the following command:

sudo pipinstall /tmp/tensorflow_pkg/tensorflow-1.2.0rc1-cp27-cp27mu-linux_x86_64.whl


(Note: The name of the wheel, as shown above in italics, may be different for your particular build.)

Once these steps have been completed be sure to validate the installation
before proceeding to the next section. Note: When running the Python
validation script provided in the link, be sure to change to a different
directory, for example:

cd ..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐