安装Tensorflow使用AVX指令集

部署运行你感兴趣的模型镜像

Blog的Github地址:github.com/liuyan731/b…


最近上线了两个机器学习Python服务,但是在线上的性能并不是特别好,而且CPU的负载非常高。然后发现是安装的Tensorflow未使用AVX指令集导致的。今天分享一下调试方法和解决方案。

未使用AVX指令集warning

最近发现线上Tensorflow python服务CPU消耗过高,服务性能也非常差,然后分析原因,测试服务器上使用timeline工具查看预测操作的耗时如下图:

发现整个计算过程消耗时间过长(30ms)对比本机(使用GPU加速,服务器机器只用于提供服务,无GPU)耗时差别巨大(1ms,如下图)

同时发现两个timeline上面的op的名称不一样,在测试服务器上操作为:_MklConv2DWithBias、_MklRelu等,在本机上为Conv2D、Relu等。

这时想到在运行Tensorflow的时候,import会warning提示没有使用intel avx、sse指令集:

Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2

The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.

The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.

The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

因为这个只是warning并不影响程序的运行,所以一直没有在意,同时当时在网上查找解决方案,有很多“建议”让不管或直接把这个warning关掉...

考虑到这个AVX/SSE指令集可能会提高性能,所以着手重新编译安装Tensorflow,编译安装可以参考:stackoverflow.com/questions/4… ,过程还是有点复杂,需要使用bazel。

但是我并没有真正去编译安装orz,因为发现我们使用的Tensorflow 1.6.0 已经开始使用AVX指令集进行预编译了,如下图:

但是为何还是会有warning警告呢?

pip与conda安装的Tensorflow不一致?

与小组的另一位同学讨论,发现他使用的tensorflow1.6.0并不会有avx warning,细问之下发现他是使用pip(阿里云)安装的,而我是使用conda(清华镜像)安装的。

考虑到conda镜像和pip镜像的可能不一致,将conda安装的Tensorflow卸载,重新使用pip安装,avx warning消失。

重新运行服务并打印timeline,如下图:

整体时间下降到2ms,性能大大提升,同时CPU负载大大降低!至此性能优化到合理的水平。

所以至少对于Tensorflow 1.6.0版本pip安装与conda安装是不一致的。

ps

  • 使用pip安装完Tensorflow后,重新import tensorflow报错:ImportError: /usr/lib64/libstdc++.so.6: version CXXABI_1.3.7’ not found 的解决方案
  • AVX: Advanced Vector Extensions(AVX) are extensions to the x86 instruction set architecture for microprocessors from Intel and AMD proposed by Intel in March 2008 and first supported by Intel with the Sandy Bridge processor shipping in Q1 2011 and later on by AMD with the Bulldozer processor shipping in Q3 2011. AVX provides new features, new instructions and a new coding scheme.
  • SSE:In computing, Streaming SIMD Extensions (SSE) is an SIMD instruction set extension to the x86 architecture, designed by Intel and introduced in 1999 in their Pentium III series of processors shortly after the appearance of AMD's 3DNow!. SSE contains 70 new instructions, most of which work on single precision floating point data. SIMD instructions can greatly increase performance when exactly the same operations are to be performed on multiple data objects. Typical applications are digital signal processing and graphics processing.

补充

TF Timeline模块

参考:Tensorflow Timeline介绍及简单使用

pip使用国内镜像

mkdir .pip
vi .pip/pip.conf
 
[list]
format=columns
 
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
 
[install]
trusted-host=mirrors.aliyun.com
复制代码

思考

程序中的warning也是需要引起大家足够的重视,说不定里面就有一个大坑。。。

2018/4/22 done

此文章也同步至个人Github博客

转载于:https://juejin.im/post/5adc96906fb9a07aa541e5df

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

在 Linux 系统下,可以通过多种方式检查 CPU 是否支持 AVX 指令集。以下是几种常用的方法: ### 1. 使用 `/proc/cpuinfo` 文件 Linux 系统中的 `/proc/cpuinfo` 文件提供了 CPU 的详细信息,包括支持的指令集。可以通过以下命令检查是否支持 AVX: ```bash cat /proc/cpuinfo | grep avx ``` 如果输出中包含 `avx` 或 `avx2` 等关键词,则表示 CPU 支持 AVX 指令集。如果没有输出,则表示不支持 AVX 指令集[^1]。 ### 2. 使用 `lscpu` 命令 `lscpu` 是一个用于显示 CPU 架构信息的命令行工具。可以通过以下命令查看 CPU 是否支持 AVX: ```bash lscpu | grep -i avx ``` 如果输出中包含 `avx` 或 `avx2` 等关键词,则表示 CPU 支持 AVX 指令集。如果没有输出,则表示不支持 AVX 指令集。 ### 3. 使用 `cpuid` 工具 `cpuid` 是一个专门用于查询 CPU 功能的工具。可以通过以下命令安装使用 `cpuid`: ```bash sudo apt-get install cpuid cpuid | grep -i avx ``` 如果输出中包含 `avx` 或 `avx2` 等关键词,则表示 CPU 支持 AVX 指令集。如果没有输出,则表示不支持 AVX 指令集。 ### 4. 使用 `gcc` 编译测试程序 可以通过编写一个简单的 C 程序来检查 CPU 是否支持 AVX 指令集。以下是一个示例程序: ```c #include <stdio.h> #include <cpuid.h> int main() { unsigned int eax, ebx, ecx, edx; __get_cpuid(1, &eax, &ebx, &ecx, &edx); if (ecx & bit_AVX) { printf("CPU supports AVX instruction set.\n"); } else { printf("CPU does not support AVX instruction set.\n"); } return 0; } ``` 在编译时需要包含 `cpuid.h` 头文件,并使用 `-mavx` 选项启用 AVX 支持: ```bash gcc -mavx avx_check.c -o avx_check ./avx_check ``` 如果输出中显示 CPU 支持 AVX 指令集,则表示支持 AVX;否则表示不支持 AVX。 ### 5. 使用 `Python` 脚本检查 可以通过 Python 脚本来检查 CPU 是否支持 AVX 指令集。以下是一个示例脚本: ```python import os def check_avx_support(): with open('/proc/cpuinfo', 'r') as f: cpuinfo = f.read() if 'avx' in cpuinfo: print("CPU supports AVX instruction set.") else: print("CPU does not support AVX instruction set.") check_avx_support() ``` 运行该脚本后,如果输出中显示 CPU 支持 AVX 指令集,则表示支持 AVX;否则表示不支持 AVX。 ### 注意事项 - 某些旧版本的 Linux 内核或发行版可能不支持 AVX 指令集,即使 CPU 本身支持 AVX 指令集。在这种情况下,可能需要更新内核或发行版以获得更好的支持[^2]。 - 如果遇到类似 "Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2" 的警告信息,可能是因为使用TensorFlow 二进制文件未启用 AVX 支持。可以尝试从源代码编译 TensorFlow,并启用 AVX 支持以获得更好的性能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值