您的 CPU 支持此 TensorFlow 二进制文件未编译使用的指令:AVX AVX2

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

我最近安装了 tensorflow(Windows CPU 版本)并收到以下消息:

Successfully installed tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc2

然后当我尝试 运行

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
sess.run(hello)
'Hello, TensorFlow!'
a = tf.constant(10)
b = tf.constant(32)
sess.run(a + b)
42
sess.close()

(我通过 https://github.com/tensorflow/tensorflow 找到的)

我收到了以下消息:

2017-11-02 01:56:21.698935: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

但是当我运行

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

按原样运行输出Hello, TensorFlow!,说明确实安装成功,但还有其他地方不对。

您知道问题是什么以及如何解决吗?

这个警告是关于什么的?

现代 CPUs 除了通常的算术和逻辑外,还提供了许多低级指令,称为扩展,例如SSE2、SSE4、AVX等 来自Wikipedia:

Advanced Vector Extensions (AVX) are extensions to the x86 instruction set architecture for microprocessors from Intel and AMD proposed by Intel in March 2008 and first supported by Intel with the Sandy Bridge processor shipping in Q1 2011 and later on by AMD with the Bulldozer processor shipping in Q3 2011. AVX provides new features, new instructions and a new coding scheme.

特别是,AVX 引入了 fused multiply-accumulate (FMA) 运算,可以加速线性代数计算,即点积、矩阵乘法、卷积等。几乎每个机器学习训练都涉及大量的这些操作,因此在支持 AVX 和 FMA(高达 300%)的 CPU 上会更快。警告指出您的 CPU 确实支持 AVX(万岁!)。

我想在这里强调一下:这只是关于CPU.

那为什么不用呢?

因为 tensorflow 默认发行版是 without CPU extensions 构建的,例如 SSE4.1、SSE4.2、AVX、AVX2、FMA 等。默认构建(来自 pip install tensorflow 的)旨在与尽可能多的 CPU 兼容。另一个论点是,即使有这些扩展,CPU 也比 GPU 慢很多,预计中大型机器学习训练将在 GPU 上进行。

你应该怎么做?

如果你有 GPU,你不应该关心 AVX 支持,因为大多数昂贵的操作将在 GPU 设备上分派(除非明确设置为不)。在这种情况下,您可以通过

简单地忽略此警告
# Just disables the warning, doesn't take advantage of AVX/FMA to run faster
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

... 或者如果您使用的是 Unix,则设置 export TF_CPP_MIN_LOG_LEVEL=2。 Tensorflow 无论如何都运行良好,但您不会看到这些恼人的警告。


如果你没有 GPU 并且想尽可能地利用 CPU,你应该从优化的源代码构建 tensorflow对于 您的 CPU 如果您的 CPU 支持,则启用 AVX、AVX2 和 FMA。它已在 this question and also this GitHub issue. Tensorflow uses an ad-hoc build system called bazel 中进行了讨论,构建它并不是那么简单,但肯定是可行的。之后,不仅警告会消失,tensorflow 性能应该也会有所提升。

使用此命令为您的 CPU 和 OS 更新 tensorflow 二进制文件

pip install --ignore-installed --upgrade "Download URL"

whl文件的下载url可以在这里找到

https://github.com/lakshayg/tensorflow-build

CPU GPU 优化

即使您拥有 GPU 并将其用于训练和推理,也可以通过从源安装 TensorFlow 获得性能提升。原因是有些 TF 操作只有 CPU 实现,不能在您的 GPU 上 运行。

此外,还有一些性能增强技巧可以很好地利用您的 CPU。 TensorFlow's performance guide 推荐如下:

Placing input pipeline operations on the CPU can significantly improve performance. Utilizing the CPU for the input pipeline frees the GPU to focus on training.

为了获得最佳性能,您应该编写代码以利用 CPU 和 GPU 协同工作,如果您有 GPU,则不要将其全部转储到 GPU 上。 为您的 CPU 优化 TensorFlow 二进制文件可以节省数小时的 运行ning 时间,而且您必须这样做一次。

对于 Windows(感谢所有者 f040225),转到此处:https://github.com/fo40225/tensorflow-windows-wheel 根据 "tf + python + cpu_instruction_extension" 的组合为您的环境获取 url。然后使用这个命令安装:

pip install --ignore-installed --upgrade "URL"

如果您遇到 "File is not a zip file" 错误,请将 .whl 下载到您的本地计算机,然后使用此 cmd 进行安装:

pip install --ignore-installed --upgrade /path/target.whl

对于Windows,您可以检查使用AVX2 编译的official Intel MKL optimization for TensorFlow 轮子。这个解决方案加快了我的推理 ~x3.

conda install tensorflow-mkl

如果你使用的是pip版本的TensorFlow,说明它已经编译好了,你只是安装而已。基本上你安装了 TensorFlow-GPU,但是当你从存储库下载它并尝试构建它时,你应该使用 CPU AVX 支持来构建它。如果忽略它,每次在 CPU 上 运行 时都会收到警告。你也可以看看那些。

Proper way to compile Tensorflow with SSE4.2 and AVX

What is AVX Cpu support in tensorflow

我发现解决此问题的最简单方法是卸载所有内容,然后安装特定版本的 tensorflow-gpu:

  1. 卸载张量流:
    pip uninstall tensorflow
  1. 卸载tensorflow-gpu:(确保运行这个即使你不确定你是否安装它)
    pip uninstall tensorflow-gpu
  1. 安装特定的 tensorflow-gpu 版本:
    pip install tensorflow-gpu==2.0.0
    pip install tensorflow_hub
    pip install tensorflow_datasets

您可以通过将以下代码添加到 python 文件中来检查这是否有效:

from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np

import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds

print("Version: ", tf.__version__)
print("Eager mode: ", tf.executing_eagerly())
print("Hub Version: ", hub.__version__)
print("GPU is", "available" if tf.config.experimental.list_physical_devices("GPU") else "NOT AVAILABLE")

运行 文件,然后输出应该是这样的:

Version:  2.0.0
Eager mode:  True
Hub Version:  0.7.0
GPU is available

希望这对您有所帮助

这个库对我有用 https://pypi.org/project/silence-tensorflow/

安装这个库并按照页面上的说明进行操作,效果非常好!

尝试使用 anaconda。我有同样的错误。一个单独的选择是从源代码构建 tensorflow,这需要很长时间。我尝试使用 conda 并且成功了。

  1. 在 anaconda 中创建一个新环境。
  2. conda -c conda-forge tensorflow

然后,成功了。

他提供了一次列表,被人删除了但是看到​​答案是 Download packages list

输出:

F:\temp\Python>python test_tf_logics_.py
[0, 0, 26, 12, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[ 0  0  0 26 12  0  0  0  2  0  0  0  0  0  0  0  0]
[ 0  0 26 12  0  0  0  2  0  0  0  0  0  0  0  0  0]
2022-03-23 15:47:05.516025: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-03-23 15:47:06.161476: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 10 MB memory:  -> device: 0, name: NVIDIA GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1
[0 0 2 0 0 0 0 7 0 0 0 0 0 0 0 0 0]
...