欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[转] ubuntu解决tensorflow提示未编译SSE3/4.1/4.2

程序员文章站 2022-06-13 23:08:28
...
在我的机器上出现的提示信息如下所示:
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.  
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
 
那么需要说明的是:这些是warnings,不是error。这些warings的意思是说:你的机器上有这些指令集可以用,并且用了他们会加快你的CPU运行速度,但是你的TensorFlow在编译的时候并没有用到这些指令集。
 
我的tensorflow在安装的时候采用的pip install指令,这种安装方式会存在这种问题。主要有两种解决方法,一种是修改警告信息的显示级别,使这种信息不再出现,另外一种就是自己重新编译安装tensorflow,在编译的时候使用这些指令集。这里我尝试第二种解决方法。并且由于我的机器上没有高效的GPU,所以我尝试安装的是CPU版本。
 
首先,卸载已经安装的tensorflow:
sudo pip uninstall tensorflow  
 
然后,克隆Tensorflow仓库:
git clone --recurse-submodules https://github.com/tensorflow/tensorflow  
 
上面的命令会在你的当前文件夹中创建一个叫做“tensorflow”的文件夹,下载的文件都存在里面。
 
由于编译安装tensorflow的时候要用到Bazel工具,所以接下来我们安装Bazel。按照官网指导(https://bazel.build/versions/master/docs/install-ubuntu.html)输入以下命令:
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list  
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add - 
sudo apt-get update && sudo apt-get install bazel
sudo apt-get upgrade bazel  
 
然后安装tensorflow所需要的其他依赖
sudo apt-get install python-numpy python-dev python-pip python-wheel  
 
然后进入tensorflow文件夹,运行tensorflow的配置程序:
cd tensorflow/  
./configure  
 
对我来说,在配置过程中出现如下错误:
Problem with java installation: couldn't find/access rt.jar in /usr/lib/jvm/java-9-openjdk-amd64  
 
我没有仔细研究原因,但是我用如下命令把java-9卸载之后就没有问题了。
sudo apt-get purge openjdk-9*  
 
然后用如下命令来生成一个pip的安装包:
bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package  
 
这是一个相当耗时的过程。
 
上述命令会生成一个叫做build_pip_package的脚本,按照如下命令运行这个脚本,在/tmp/tensorflow_pkg文件夹中创建pip的安装包:
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg  
 
然后运行下面的命令来安装。需要说明的是,由于平台的不同,可能软件包的名字是不一样的。
sudo pip install /tmp/tensorflow_pkg/tensorflow-1.1.0rc1-cp27-cp27mu-linux_x86_64.whl
 
安装成功,意味着大功告成。