对tensorflow java api进行cpu优化

白色玫瑰 程序猿

时间: 2023-05-22 阅读: 1 字数:3825

{}
使用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。

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

原文地址:https://blog.csdn.net/lyg5623/article/details/72781770?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168475008016800180689615%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=168475008016800180689615&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-10-72781770-null-null.142^v87^insert_down28,239^v2^insert_chatgpt&utm_term=java%E4%BC%98%E5%8C%96

本文章网址:https://www.sjxi.cn/detil/e61e946eae8847f68516d904e0b2d340

打赏作者

本站为非盈利网站,如果您喜欢这篇文章,欢迎支持我们继续运营!

最新评论
当前未登陆哦
登陆后才可评论哦

湘ICP备2021009447号

×

(穷逼博主)在线接单

QQ: 1164453243

邮箱: abcdsjx@126.com

前端项目代做
前后端分离
Python 爬虫脚本
Java 后台开发
各种脚本编写
服务器搭建
个人博客搭建
Web 应用开发
Chrome 插件编写
Bug 修复