安裝Tensorflow使用AVX指令集

lyyyyy發表於2018-04-22

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


最近上線了兩個機器學習Python服務,但是線上上的效能並不是特別好,而且CPU的負載非常高。然後發現是安裝的Tensorflow未使用AVX指令集導致的。今天分享一下除錯方法和解決方案。

未使用AVX指令集warning

最近發現線上Tensorflow python服務CPU消耗過高,服務效能也非常差,然後分析原因,測試伺服器上使用timeline工具檢視預測操作的耗時如下圖:

timelime-img-1

發現整個計算過程消耗時間過長(30ms)對比本機(使用GPU加速,伺服器機器只用於提供服務,無GPU)耗時差別巨大(1ms,如下圖)

timelime-img-2

同時發現兩個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指令集進行預編譯了,如下圖:

tf-release

但是為何還是會有warning警告呢?

pip與conda安裝的Tensorflow不一致?

與小組的另一位同學討論,發現他使用的tensorflow1.6.0並不會有avx warning,細問之下發現他是使用pip(阿里雲)安裝的,而我是使用conda(清華映象)安裝的。

考慮到conda映象和pip映象的可能不一致,將conda安裝的Tensorflow解除安裝,重新使用pip安裝,avx warning消失。

重新執行服務並列印timeline,如下圖:

timelime-img-3

整體時間下降到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部落格

相關文章