Tensorflow V2.0 相容性小實驗

為了一圓 AI 大夢, 我買了一塊 GTX 3090. 原本在 PCHOME 觀望三分鐘, 嫌貴沒按下去, 後來就再也買不到了!!!  電腦的東西不是應該慢慢降價嗎? “早買早享受, 晚買享折扣" 是大家都知道的定律啊!? 不過這次不一樣, 顯卡價錢還一天比一天高. 最後只好破釜沉舟買了蝦皮最後一張現貨, 而且是散熱貼片品質不好的技嘉. 

卡片到手之後, 先找個 BenchMark 來燒機, 結果老舊的 i7 扯了後腿, 分數還差正常的 3090 一成. 腦筋一賺, 來挖礦吧! 結果以前會動的 guiminer 程式都連不上礦池. 上網一查才知道電腦已經不能挖比特幣了. 不死心的我研究了一下, 得知可以在 Nicehash 挖別的幣換比特幣, 這樣果然就行了. 但是算力全開的話, VRM 瞬間就飆到 110 度. 嚇得死人了. 只敢開溫和模式當背景程式跑.

好了, 言歸正傳. 算 AI 會不會燒 VRM 要試了才知道.  然而, 客戶的事情如雪片般飛來, 我始終不能定下心跑程式. 兩個月過去, 比特幣都挖了 0.005 顆了, 但是正事還沒開工. 好不容易有清明連假, 趕快把電腦環境安裝起來. 有一篇文章跟我以前的軌跡很相似 [1], 於是我就按照這個思路來恢復環境.

不過 Google 排名靠前 – 很多人引用的文章相對都是比較舊的. 前述文章中的 Cuda 9 已經該進版到 Cuda 11.2, Cudnn 由 7.5 進版到 8.1.1, 其他流程都差不多. 按照文章一路破關, 然後在測試環境這邊卡住了. 當我執行 c = tf.matmul(a, b) 會得到:

" failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED"

根據 [2] 所述, 這是分配記憶體的問題. 按照官網 [3] 要下tf.config.experimental.set_visible_devices.

但這個還是只能治標, 不能治本. 晚點還是會遇到

tensorflow.python.framework.errors_impl.InternalError: Blas xGEMM launch failed : a.shape=[1,2,3], b.shape=[1,3,2], m=2, n=2, k=3 [Op:MatMul]

看起來正確解答是這三行 [4]:

import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU') 
tf.config.experimental.set_memory_growth(physical_devices[0], True)

但是要在 init 的時候下, 不能臨時抱佛腳. 所以要重來一次.

然後在 tensorflow 2.0, 過去的 tf.Session() 也被淘汰了, 要改用 f.compat.v1.Session(). tf.ConfigProto 也換成  tf.compat.v1.ConfigProto (待確認). 比較嚴重的區別是 tensorflow 2.0 預設開 eager execution. 會遇到

RuntimeError: The Session graph is empty. Add operations to the graph before calling run()

所以有三個方法:

A. 直接設定跟舊版相容 [7]:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

B. 或是關掉 eager execution [6]:

tf.compat.v1.disable_eager_execution()

C. 或者先宣告 session, 而不是等要用的時候才宣告. 例如 [6]:

 # Launch the graph in a session.
 with tf.compat.v1.Session() as ses:

     # Build a graph.
     a = tf.constant(5.0)
     b = tf.constant(6.0)
     c = a * b

     # Evaluate the tensor `c`.
     print(ses.run(c))

重來一次完整的步驟就是. 從 Anaconda 打開 Python 的 terminal.

import tensorflow as tf

physical_devices = tf.config.list_physical_devices('GPU') 
tf.config.experimental.set_memory_growth(physical_devices[0], True)
tf.compat.v1.disable_eager_execution()
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))
print(sess.run(c))

 

[REF]

  1. https://ithelp.ithome.com.tw/articles/10237846
  2. https://www.itread01.com/content/1545441880.html
  3. https://www.tensorflow.org/guide/gpu
  4. ://stackoverflow.com/questions/43990046/tensorflow-blas-gemm-launch-failed
  5. https://stackoverflow.com/questions/55142951/tensorflow-2-0-attributeerror-module-tensorflow-has-no-attribute-session
  6. https://github.com/OlafenwaMoses/ImageAI/issues/400
  7. https://github.com/tensorflow/tensorflow/issues/25446

 

 

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

%d 位部落客按了讚: