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

2024-04-26 00:08:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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\36\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!,这表明安装确实成功,但还有其他错误。

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


Tags: runimport版本消息hellosessionwindowstf
3条回答

这个警告是关于什么的?

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

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的CPU上速度会更快(高达300%)。警告说明您的CPU确实支持AVX(万岁!)。

我想在这里强调一下:这都是关于CPU的。

那为什么不用呢?

因为tensorflow默认分布是构建在without CPU extensions上的,例如SSE4.1、SSE4.2、AVX、AVX2、FMA等。默认构建(来自pip install tensorflow)旨在与尽可能多的cpu兼容。另一种观点是,即使使用这些扩展,CPU也比GPU慢得多,而且预计中大型机器学习培训将在GPU上进行。

你该怎么办?

如果您有一个GPU,您不应该关心AVX支持,因为最昂贵的操作将在GPU设备上分派(除非显式设置为not)。在这种情况下,您可以忽略此警告

# Just disables the warning, doesn't enable AVX/FMA
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

。。。或者在Unix上设置export TF_CPP_MIN_LOG_LEVEL=2。Tensorflow运行良好,但您不会看到这些烦人的警告。


如果您没有GPU,并且希望尽可能多地利用CPU,您应该从为您的CPU优化的源构建tensorflow,如果您的CPU支持它们,则启用AVX、AVX2和FMA。已经在this questionthis GitHub issue中讨论过了。Tensorflow使用一个名为bazel的特别构建系统,构建它并不是那么简单,但肯定是可行的。在此之后,不仅警告将消失,tensorflow的性能也应该提高。

用GPU优化CPU

从源代码安装TensorFlow可以提高性能,即使您有GPU并将其用于训练和推理。原因是某些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二进制文件可以节省运行时间,而且您必须执行一次。

使用此命令更新CPU&O的tensorflow二进制文件

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

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

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

相关问题 更多 >