使用tensorflow的java接口时,会报下面的提示内容,这是因为官方为了更好的兼容性,在编译jni库文件的时候没有进行任何的cpu指令集优化。我们只需要自己重新编译jni库文件,替换libtensorflow_jni-1.1.0.jar对应的...
使用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。
从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"
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
本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!
本站主要用于日常笔记的记录和生活日志。本站不保证所有内容信息可靠!(大多数文章属于搬运!)如有版权问题,请联系我立即删除:“abcdsjx@126.com”。
QQ: 1164453243
邮箱: abcdsjx@126.com