对tensorflow java api进行cpu优化
来源:互联网 发布:天天有喜知画扮演者 编辑:程序博客网 时间:2024/06/12 20:31
使用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/tensorflowcd 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
- 对tensorflow java api进行cpu优化
- 对tensorflow java api进行cpu优化
- 对Java代码进行优化
- 使用Java API对HBase进行操作
- 通过Java Api对Hbase进行操作
- hbase学习教程(三):HBase优化技巧、HBase基本命令、使用Java API对HBase服务器进行操作
- 对程序进行优化
- 使用Java API对HBase进行CRUD操作
- 使用java提供的api对文件进行压缩解密
- 使用Java Api 对HBase进行简单操作
- JAVA 8 Stream API 对Stream进行分组、分区
- JAVA API调用优化建议--避免对boolean类型判断
- 利用封装、继承对Java代码进行优化
- TensorFlow练习1: 对评论进行分类
- TensorFlow练习2: 对评论进行分类
- TensorFlow练习1: 对评论进行分类
- Tensorflow练习1-对评论进行分类
- 对nova进行api扩展
- Linux自动删除n天前的日志
- 3167:神奇的辣条
- Android线程—四种线程池的学习与总结
- Bootstrap3.0学习第十轮(下拉菜单、按钮组、按钮式下拉菜单)
- Des解密 以及 将。net的DEs加密 用android 的DES解密
- 对tensorflow java api进行cpu优化
- 千万级用户Redis缓存集群搭建以及项目实战
- 使用命令参数方式指定log4j配置文件
- NGINX下配置CACHE-CONTROL
- angularJs操作select列表框
- JSP 国际化
- Java内部类的作用
- 剑指offer第十七题之第二十六题(java详解)
- 可变参数函数