当前位置: 首页>>技术问答>>正文


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

问题描述

我是TensorFlow的新手。我最近安装了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!,这表明安装确实成功了,但是还有其他错误。

那么问题到底是什么以及如何解决呢?谢谢。

 

最佳答案

首先,这是关于什么的警告?

除了常见的算术和逻辑运算之外,现代CPU还提供了许多底层指令(也叫扩展extension),如 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.

译文是:Advanced Vector Extensions(AVX)是Intel在2008年3月提出的,针对Intel和AMD的x86指令集体系结构的扩展,由Intel于2011年第一季度发布的Sandy Bridge处理器首先得到支持,随后在2011年第三季度由AMD与Bulldozer处理器一起发布。AVX提供了新功能,新指令和新编码方案。

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

我想在此强调一下:这仅与CPU有关。

那为什么不使用呢?

因为tensorflow默认发行版是按无CPU扩展(without CPU extensions,例如SSE4.1,SSE4.2,AVX,AVX2,FMA等)来构建的。默认发行版(pip install tensorflow的发行版)旨在与尽可能多的CPU兼容。另一个论点是,即使有了这些扩展,如果想要进行中等和大规模的机器学习训练,CPU也要比GPU慢很多。所以默认情况是不使用的。

你该怎么办?

如果您有GPU,则不必在意AVX的支持,因为大多数昂贵的操作都会在GPU设备上调度(除非明确设置为不这样做)。在这种情况下,您可以通过以下方式忽略此警告

# 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,则应从启用了AVX,AVX2和FMA的CPU优化源中构建Tensorflow(如果您的CPU支持)。参考在this questionthis GitHub issue中进行的讨论。 Tensorflow使用了一个名为bazel的专门构建系统,构建它并不是那么简单,但是肯定是可行的。此后,不仅警告会消失,而且tensorflow性能也应提高。

 

补充资料一

使用如下命令,为您的CPU和操作系统更新tensorflow二进制文件。也可以解决上述问题!

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

您可以在此处找到whl文件的下载网址

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

如果遇到“文件不是zip文件”错误,请将.whl下载到本地计算机,然后使用此cmd进行安装:

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


VAX2

参考资料

 

本文由《纯净天空》出品。文章地址: https://vimsky.com/article/4294.html,未经允许,请勿转载。