对tensorflow java api进行cpu优化

来源:互联网 发布:淘宝店铺入驻规则 编辑:程序博客网 时间:2024/06/11 12:00

使用tensorflow的java接口时,会报下面的提示内容,这是因为官方为了更好的兼容性,在编译jni库文件的时候没有进行任何的cpu指令集优化。我们只需要自己重新编译jni库文件,替换libtensorflow_jni-1.1.0.jar对应的文件即可。

2017-05-27 14:57:30.437555: 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.
2017-05-27 14:57:30.437638: 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.
2017-05-27 14:57:30.437690: 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.
2017-05-27 14:57:30.437694: 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.
2017-05-27 14:57:30.437721: 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.


编译tensorflow需要使用Bazel,安装Bazel需要jdk8。

Bazel安装

从https://github.com/bazelbuild/bazel/releases 下载 bazel-xxx-installer-linux-x86_64.sh
./bazel-xxx-installer-linux-x86_64.sh --user
export PATH="$PATH:$HOME/bin"


编译针对服务器cpu优化的jni库文件

git clone https://github.com/tensorflow/tensorflow
cd tensorflow
./configure


在下面的地方根据自己的情况输入cpu优化选项

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:


如果编译机和运行机一样的配置,那么使用-march=native就好了,它会自动使用可用的优化。如果机器不一样,那么需要指定运行机可用的cpu优化项,对应上面的提示内容,我输入的是

-msse4.1 -msse4.2 -mavx -mavx2 -mfma


然后使用下面命令编译jni库文件

bazel build --config opt //tensorflow/java:libtensorflow_jni


tensorflow开发在stackoverflow上面提到:Regarding the command to build TensorFlow with bazel, if -march=native was set during configuring it is not necessary to explicitly add other optimization flags as TensorFlow will already compile with all SIMD instructions available in your CPU architecture.

也就是configure设置好优化选项以后,build的时候不需要额外再指定。

完成后会在bazel-bin/tensorflow/java下面生成我们需要的libtensorflow_jni.so(我的是linux系统,其它系统文件名不一样)。把新生成的libtensorflow_jni.so替换libtensorflow_jni-1.1.0.jar里面的libtensorflow_jni.so。


下面是优化前后运行时间的对比,效果还是很明显的

优化前   time cost 7781 ms
优化后   time cost 4376 ms

原创粉丝点击