【AIGC】二、mac本地采用GPU啟動keras運算

mac本地采用GPU啟動keras運算

  • 一、問題背景
  • 二、技術背景
  • 三、實驗驗證
    • 本機配置
    • 安裝PlaidML
      • 安裝plaidml-keras
      • 配置默認顯卡
    • 運行采用 CPU運算的代碼
        • step1 先導入keras包,導入數據cifar10,這里可能涉及外網下載,有問題可以參考[keras使用基礎問題](https://editor.csdn.net/md/?articleId=140331142)
        • step2 導入計算模型,如果本地不存在該模型數據,會自動進行下載,有問題可以參考[keras使用基礎問題](https://editor.csdn.net/md/?articleId=140331142)
        • step3 模型編譯
        • step4 進行一次預測
        • step5 進行10次預測
    • 運行采用 GPU運算的代碼
      • 采用顯卡metal_intel(r)_uhd_graphics_630.0
        • step0 通過plaidml導入keras,之后再做keras相關操作
        • step1 先導入keras包,導入數據cifar10
        • step2 導入計算模型,如果本地不存在該模型數據,會自動進行下載
        • step3 模型編譯
        • step4 進行一次預測
        • step5 進行10次預測
      • 采用顯卡metal_amd_radeon_pro_5300m.0
        • step0 通過plaidml導入keras,之后再做keras相關操作
        • step1 先導入keras包,導入數據cifar10
        • step2 導入計算模型,如果本地不存在該模型數據,會自動進行下載
        • step3 模型編譯
        • step4 進行一次預測
        • step5 進行10次預測
  • 四、評估討論

一、問題背景

從上一篇文章中,我們已經發現在大模型的運算中,采用cpu進行運算時,對于cpu的使用消耗很大。因此我們這里會想到如果機器中有GPU顯卡,如何能夠發揮顯卡在向量運算中的優勢,將機器學習相關的運算做的又快又好。

二、技術背景

我們知道當前主流的支持機器學習比較好的顯卡為 Nvida系列的顯卡,俗稱 N卡,但是在mac機器上,通常集成的都是 AMD系列的顯卡。兩種不同的硬件指令集的差異導致上層需要有不同的實現技術。
但是在 AMD顯卡中,有一種PlaidML的技術,通過該插件,可以封裝不同顯卡的差異。

PlaidML項目地址:https://github.com/plaidml/plaidml
目前 PlaidML 已經支持 Keras、ONNX 和 nGraph 等工具,直接用 Keras 建個模,MacBook 輕輕松松調用 GPU。
通過這款名為 PlaidML 的工具,不論英偉達、AMD 還是英特爾顯卡都可以輕松搞定深度學習訓練了。

參考:Mac使用PlaidML加速強化學習訓練

三、實驗驗證

本次操作,對于一個常規的keras的算法,分別在cpu和 gpu下進行多輪計算,統計耗時。進行對比統計。

本機配置

本次采用的mac機器的軟件、硬件參數如下
在這里插入圖片描述

安裝PlaidML

由于安裝依賴包的過程,需要有命令交互,因此安裝plaidML包操作在命令行進行,代碼執行在 jupyter中進行。

由于采用虛擬環境時會jupyter會有需要創建 kernal的技術點,因此這里建議暫時直接用原python環境進行驗證。了解jupyter在虛擬環境的配置特點的同學可以嘗試在虛擬環境中操作。

安裝plaidml-keras

pip3  install plaidml-keras

筆者在采用命令 pip3 install plaidml-keras 安裝最新版本plaidml-keras為0.7.0后,在執行初始化操作時遇到bug,后續降為0.6.4執行正常。但是后續再次安裝為 0.7.0,又正常了。
plaidml in github

配置默認顯卡

在命令行執行

plaidml-setup

交互內容如下

(venv) tsingj@tsingjdeMacBook-Pro-2 ~  # plaidml-setupPlaidML Setup (0.6.4)Thanks for using PlaidML!Some Notes:* Bugs and other issues: https://github.com/plaidml/plaidml* Questions: https://stackoverflow.com/questions/tagged/plaidml* Say hello: https://groups.google.com/forum/#!forum/plaidml-dev* PlaidML is licensed under the Apache License 2.0Default Config Devices:metal_intel(r)_uhd_graphics_630.0 : Intel(R) UHD Graphics 630 (Metal)metal_amd_radeon_pro_5300m.0 : AMD Radeon Pro 5300M (Metal)Experimental Config Devices:llvm_cpu.0 : CPU (LLVM)metal_intel(r)_uhd_graphics_630.0 : Intel(R) UHD Graphics 630 (Metal)opencl_amd_radeon_pro_5300m_compute_engine.0 : AMD AMD Radeon Pro 5300M Compute Engine (OpenCL)opencl_cpu.0 : Intel CPU (OpenCL)opencl_intel_uhd_graphics_630.0 : Intel Inc. Intel(R) UHD Graphics 630 (OpenCL)metal_amd_radeon_pro_5300m.0 : AMD Radeon Pro 5300M (Metal)Using experimental devices can cause poor performance, crashes, and other nastiness.Enable experimental device support? (y,n)[n]:

列舉現實當前可以支持的顯卡列表,選擇默認支持支持的2個顯卡,還是試驗階段所有支持的6 種硬件。
可以看到默認支持的 2 個顯卡即最初截圖中現實的兩個顯卡。為了測試穩定起見,這里先選擇N,回車。

Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:1 : metal_intel(r)_uhd_graphics_630.02 : metal_amd_radeon_pro_5300m.0Default device? (1,2)[1]:1Selected device:metal_intel(r)_uhd_graphics_630.0

對于默認選擇的設置,設置一個默認設備,這里我們先將metal_intel?_uhd_graphics_630.0設置為默認設備,當然這個設備其實性能比較差,后續我們會再將metal_amd_radeon_pro_5300m.0設置為默認設備進行對比。
寫入 1 之后,回車。

Almost done. Multiplying some matrices...
Tile code:function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }
Whew. That worked.Save settings to /Users/tsingj/.plaidml? (y,n)[y]:y
Success!

回車,將配置信息寫入默認配置文件中,完成配置。

運行采用 CPU運算的代碼

本節中采用jupyter進行一個簡單算法代碼的運行,統計其時間。

step1 先導入keras包,導入數據cifar10,這里可能涉及外網下載,有問題可以參考keras使用基礎問題
#!/usr/bin/env python
import numpy as np
import os
import time
import keras
import keras.applications as kapp
from keras.datasets import cifar10
(x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
batch_size = 8
x_train = x_train[:batch_size]
x_train = np.repeat(np.repeat(x_train, 7, axis=1), 7, axis=2)

注意,這里默認的keral的運算后端應該是采用了tenserflow,查看輸出

2024-07-11 14:36:02.753107: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

step2 導入計算模型,如果本地不存在該模型數據,會自動進行下載,有問題可以參考keras使用基礎問題
model = kapp.VGG19()
step3 模型編譯
model.compile(optimizer='sgd', loss='categorical_crossentropy',metrics=['accuracy'])
step4 進行一次預測
print("Running initial batch (compiling tile program)")
y = model.predict(x=x_train, batch_size=batch_size)

Running initial batch (compiling tile program)
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 1s/step

step5 進行10次預測
# Now start the clock and run 10 batchesprint("Timing inference...")
start = time.time()
for i in range(10):y = model.predict(x=x_train, batch_size=batch_size)print("Ran in {} seconds".format(time.time() - start))

1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 891ms/step
Ran in 0.9295139312744141 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 923ms/step
Ran in 1.8894760608673096 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 893ms/step
Ran in 2.818492889404297 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 932ms/step
Ran in 3.7831668853759766 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 892ms/step
Ran in 4.71358585357666 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 860ms/step
Ran in 5.609835863113403 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 878ms/step
Ran in 6.5182459354400635 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 871ms/step
Ran in 7.423128128051758 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 896ms/step
Ran in 8.352543830871582 seconds
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 902ms/step
Ran in 9.288795948028564 seconds

運行采用 GPU運算的代碼

采用顯卡metal_intel?_uhd_graphics_630.0

step0 通過plaidml導入keras,之后再做keras相關操作
# Importing PlaidML. Make sure you follow this order
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"

注:
1、在采用plaidml=0.7.0 版本時,plaidml.keras.install_backend()操作會發生報錯
2、這步操作,會通過plaidml導入keras,將后臺運算引擎設置為plaidml,而不再采用tenserflow

step1 先導入keras包,導入數據cifar10
#!/usr/bin/env python
import numpy as np
import os
import time
import keras
import keras.applications as kapp
from keras.datasets import cifar10
(x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
batch_size = 8
x_train = x_train[:batch_size]
x_train = np.repeat(np.repeat(x_train, 7, axis=1), 7, axis=2)
step2 導入計算模型,如果本地不存在該模型數據,會自動進行下載
model = kapp.VGG19()

首次運行輸出顯卡信息

INFO:plaidml:Opening device “metal_intel?_uhd_graphics_630.0”

step3 模型編譯
model.compile(optimizer='sgd', loss='categorical_crossentropy',metrics=['accuracy'])
step4 進行一次預測
print("Running initial batch (compiling tile program)")
y = model.predict(x=x_train, batch_size=batch_size)

Running initial batch (compiling tile program)

由于輸出較快,內容打印只有一行。

step5 進行10次預測
# Now start the clock and run 10 batchesprint("Timing inference...")
start = time.time()
for i in range(10):y = model.predict(x=x_train, batch_size=batch_size)print("Ran in {} seconds".format(time.time() - start))

Ran in 4.241918087005615 seconds
Ran in 8.452141046524048 seconds
Ran in 12.665411949157715 seconds
Ran in 16.849968910217285 seconds
Ran in 21.025720834732056 seconds
Ran in 25.212764024734497 seconds
Ran in 29.405478954315186 seconds
Ran in 33.594977140426636 seconds
Ran in 37.7886438369751 seconds
Ran in 41.98136305809021 seconds

采用顯卡metal_amd_radeon_pro_5300m.0

在plaidml-setup設置的選擇顯卡的階段,不再選擇顯卡metal_intel?_uhd_graphics_630.0,而是選擇metal_amd_radeon_pro_5300m.0

(venv) tsingj@tsingjdeMacBook-Pro-2 ~  # plaidml-setupPlaidML Setup (0.6.4)Thanks for using PlaidML!Some Notes:* Bugs and other issues: https://github.com/plaidml/plaidml* Questions: https://stackoverflow.com/questions/tagged/plaidml* Say hello: https://groups.google.com/forum/#!forum/plaidml-dev* PlaidML is licensed under the Apache License 2.0Default Config Devices:metal_intel(r)_uhd_graphics_630.0 : Intel(R) UHD Graphics 630 (Metal)metal_amd_radeon_pro_5300m.0 : AMD Radeon Pro 5300M (Metal)Experimental Config Devices:llvm_cpu.0 : CPU (LLVM)metal_intel(r)_uhd_graphics_630.0 : Intel(R) UHD Graphics 630 (Metal)opencl_amd_radeon_pro_5300m_compute_engine.0 : AMD AMD Radeon Pro 5300M Compute Engine (OpenCL)opencl_cpu.0 : Intel CPU (OpenCL)opencl_intel_uhd_graphics_630.0 : Intel Inc. Intel(R) UHD Graphics 630 (OpenCL)metal_amd_radeon_pro_5300m.0 : AMD Radeon Pro 5300M (Metal)Using experimental devices can cause poor performance, crashes, and other nastiness.Enable experimental device support? (y,n)[n]:nMultiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
Please choose a default device:1 : metal_intel(r)_uhd_graphics_630.02 : metal_amd_radeon_pro_5300m.0Default device? (1,2)[1]:2Selected device:metal_amd_radeon_pro_5300m.0Almost done. Multiplying some matrices...
Tile code:function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }
Whew. That worked.Save settings to /Users/tsingj/.plaidml? (y,n)[y]:y
Success!
step0 通過plaidml導入keras,之后再做keras相關操作
# Importing PlaidML. Make sure you follow this order
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"

注:
1、在采用plaidml=0.7.0 版本時,plaidml.keras.install_backend()操作會發生報錯
2、這步操作,會通過plaidml導入keras,將后臺運算引擎設置為plaidml,而不再采用tenserflow

step1 先導入keras包,導入數據cifar10
#!/usr/bin/env python
import numpy as np
import os
import time
import keras
import keras.applications as kapp
from keras.datasets import cifar10
(x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
batch_size = 8
x_train = x_train[:batch_size]
x_train = np.repeat(np.repeat(x_train, 7, axis=1), 7, axis=2)
step2 導入計算模型,如果本地不存在該模型數據,會自動進行下載
model = kapp.VGG19()

INFO:plaidml:Opening device “metal_amd_radeon_pro_5300m.0”
注意,這里首次執行輸入了顯卡信息。

step3 模型編譯
model.compile(optimizer='sgd', loss='categorical_crossentropy',metrics=['accuracy'])
step4 進行一次預測
print("Running initial batch (compiling tile program)")
y = model.predict(x=x_train, batch_size=batch_size)

Running initial batch (compiling tile program)

由于輸出較快,內容打印只有一行。

step5 進行10次預測
# Now start the clock and run 10 batchesprint("Timing inference...")
start = time.time()
for i in range(10):y = model.predict(x=x_train, batch_size=batch_size)print("Ran in {} seconds".format(time.time() - start))

查看輸出

Ran in 0.43606019020080566 seconds
Ran in 0.8583459854125977 seconds
Ran in 1.2787911891937256 seconds
Ran in 1.70143723487854 seconds
Ran in 2.1235032081604004 seconds
Ran in 2.5464580059051514 seconds
Ran in 2.9677979946136475 seconds
Ran in 3.390064001083374 seconds
Ran in 3.8117799758911133 seconds
Ran in 4.236911058425903 seconds

四、評估討論

顯卡metal_intel?_uhd_graphics_630.0的內存值為1536 MB,雖然作為顯卡,其在進行運算中性能不及本機的 6核CPU;
顯卡metal_amd_radeon_pro_5300m.0,內存值為4G,其性能與本機 CPU對比,提升將近 1 倍數。

由此可以看到對于采用 GPU在進行機器學習運算中的強大優勢。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/43708.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/43708.shtml
英文地址,請注明出處:http://en.pswp.cn/web/43708.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

echarts中tooltip添加點擊事件代碼示例

echarts中tooltip添加點擊事件代碼示例_javascript技巧_腳本之家 點擊事件無法使用this 或者 this無法使用:

Qt圖形編輯類使用總結

Qt的圖形編輯通常會涉及以下三個類:QGraphicsView類、QGraphicsScene類及QGraphicsItem類。 QGraphicsView 是構建復雜圖形用戶界面的強大工具,尤其適用于那些需要動態更新、可交互的2D圖形化應用程序,如圖表繪制、流程圖編輯器、游戲地圖顯示等等。通過結合使用 QGraphics…

13--memcache與redis

前言:數據庫讀取速度較慢一直是無法解決的問題,大型網站應對的方式主要是使用緩存服務器來緩解這種情況,減少數據庫訪問次數,以提高動態Web等應用的速度、提高可擴展性。 1、簡介 Memcached/redis是高性能的分布式內存緩存服務器…

ret2csu簡單總結

一個比較進階的rop利用方式。 Why ret to csu? 當程序給的gadget不夠,或者輸入長度受限時,可以考慮利用csu中的眾多gadget以及一個call指令來劫持控制流。 __libc_csu_init 匯編源碼: .text:0000000000400790 ; void __fastcall _libc_c…

無人直播賺錢的底層邏輯是什么?一文揭曉!

當前,網絡直播已經成為各類商家提高曝光和引流獲客的主要渠道之一,這在為商家帶來新機遇的同時,也讓他們因人手不足或資金匱乏等原因而陷入無人問津窘境之中。在此背景下,無人直播軟件一經出現,便引起了眾多商家的關注…

多器官功能障礙綜合征

多器官功能障礙綜合征(Multiple Organ Dysfunction Syndrome,MODS)是指機體在遭受嚴重感染、創傷、休克、大手術等急性疾病過程中,同時或序貫發生兩個或兩個以上器官功能障礙,以致不能維持內環境穩定的臨床綜合征。 MO…

28V飛機庫維修電源在飛機庫中的作用

飛機庫作為飛機停放和維護的重要場所,其設施的完善和電源系統的穩定運行是保證飛機正常運行的前提。隨著我國航空事業的飛速發展,飛機維修行業面臨著越來越大的挑戰。在飛機維修過程中,電源系統作為關鍵組成部分,其穩定性和可靠性…

網絡服務與應用-廣域網技術(華為ip認證學習筆記)

網絡服務與應用 FTP:文件傳輸協議 TCP 傳輸 20 端口發送,21 接收端口 1. 采用 C/S 結構 2. 傳輸模式 (1)ASCII 模式:傳輸文本 (2)二進制模式:傳輸非文本 3. 工作模式 (1&…

LeetCode題練習與總結:尋找旋轉排序數組中的最小值--153

一、題目描述 已知一個長度為 n 的數組,預先按照升序排列,經由 1 到 n 次 旋轉 后,得到輸入數組。例如,原數組 nums [0,1,2,4,5,6,7] 在變化后可能得到: 若旋轉 4 次,則可以得到 [4,5,6,7,0,1,2]若旋轉 …

【MIT 6.5840/6.824】Lab1 MapReduce

MapReduce MapReduce思想實現思路感受 6.5840/6.824 Lab與筆記匯總 本文對應的Lab版本為MIT6.5840-Spring2024的Lab1 本博客只提供思路,不會公開任何代碼 本lab耗時約6h,碼量約500行 MapReduce思想 MapReduce的思想屬于是比較簡單的,分為兩…

3. 排序算法代碼-python

目錄 1.冒泡排序2.快速排序3.插入排序4.希爾排序5.選擇排序6.堆排序7.歸并排序8. 二分查找 1.冒泡排序 冒泡排序""" def BubbleSort(nums):listLength len(nums)while listLength > 0:for i in range(listLength - 1):if nums[i] > nums[i1]:nums[i], n…

References in code to package

【IntelliJ IDEA】IDE學習使用(不時更新)_idea references in code to class-CSDN博客

【筆記】從零開始做一個精靈龍女-畫貼圖階段(上)

此文只是我的筆記,不包全看懂,有問題可評論 PS貼圖加工 1.打開ps 拖入uv圖,新建圖層,設置背景色為灰色,改一下圖層名字 2.按z縮小一下uv圖層,拖入實體uv圖片(目的是更好上色,比如…

鴻蒙語言基礎類庫:【@ohos.util.Vector (線性容器Vector)】

線性容器Vector 說明: 本模塊首批接口從API version 8開始支持。后續版本的新增接口,采用上角標單獨標記接口的起始版本。開發前請熟悉鴻蒙開發指導文檔:gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md點擊或者復制轉到。 Vect…

云原生(Cloud native)

云原生(Cloud native) 一 定義 目前比較權威的定義主要來自Pivotal公司和云原生計算基金會(Cloud Native Computing Foundation,簡稱CNCF)。 1.1 Pivotal 4個要點: DevOps、持續交付、微服務、容器化。六…

【Java后端】Service層讀取yml配置文件中內容

前言 最近寫代碼,看到別人寫的讀取application.yml配置文件中數據,寫的挺規范,挺好的;雖然之前也讀取過yml文件,但用的其他方法,沒這個規范,所以記錄下 正文 假設要讀取視頻地址,…

微信小程序切換商戶號

1.登錄微信公眾平臺小程序 2.功能->微信支付 3.關聯成功后會志一關聯商戶號列表顯示 4.登錄你需要切換的商戶號 在下面選擇你需要開通的產品服務 5.切換到賬戶中心的api安全里面 只需要改變當前下面的配置即可切換小程序的收款商戶號 申請API證書按照官方的指引即可解…

關于redis的運維面試題-2

21. Redis的客戶端連接數限制如何設置? 在Redis中,客戶端連接數的限制可以通過配置文件redis.conf來設置,也可以通過命令行直接設置。以下是如何通過配置文件和命令行來設置Redis客戶端連接數限制的步驟和示例代碼。 通過配置文件設置客戶端…

JS計算某一年的土地租金收入和土地承租支出

涉及到多年的地租 , 例如 2024年5月15日 - 2026年5月15日 , 總承包租金是60000 假設 當前年是2024年 , 則計算2024年5月15日-2024年12月31日的租金收入 , 如果是2025年則是2025年1月1日-2025年12月31日 //示例交易數據 var transactions [ { type: "轉出土地收益&qu…

怎么區分住宅IP還是機房IP?機房IP和住宅IP有哪些不同?

在網絡技術的應用中,IP地址扮演著至關重要的角色。了解IP地址的種類及其特性,對于進行網絡管理、優化網絡安全策略、以及實施數據分析等任務至關重要。本文將深入探討如何區分住宅IP和機房IP,并分析兩者的主要差異。 一、IP地址分類簡介 IP…