【AIGC】二、mac本地采用GPU启动keras运算

打印 上一主题 下一主题

主题 531|帖子 531|积分 1593

一、问题背景

从上一篇文章中,我们已经发现在大模型的运算中,采用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

  1. pip3  install plaidml-keras
复制代码
  笔者在采用命令 pip3 install plaidml-keras 安装最新版本plaidml-keras为0.7.0后,在执行初始化操纵时遇到bug,后续降为0.6.4执行正常。但是后续再次安装为 0.7.0,又正常了。
plaidml in github
  配置默认显卡

在命令行执行
  1. plaidml-setup
复制代码
交互内容如下
  1. (venv) tsingj@tsingjdeMacBook-Pro-2 ~  # plaidml-setup
  2. PlaidML 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,回车。
  1. Multiple devices detected (You can override by setting PLAIDML_DEVICE_IDS).
  2. Please choose a default device:
  3.    1 : metal_intel(r)_uhd_graphics_630.0
  4.    2 : metal_amd_radeon_pro_5300m.0
  5. Default device? (1,2)[1]:1
  6. Selected device:
  7.     metal_intel(r)_uhd_graphics_630.0
复制代码
对于默认选择的设置,设置一个默认设备,这里我们先将metal_intel®_uhd_graphics_630.0设置为默认设备,固然这个设备其实性能比较差,后续我们会再将metal_amd_radeon_pro_5300m.0设置为默认设备进行对比。
写入 1 之后,回车。
  1. Almost done. Multiplying some matrices...
  2. Tile code:
  3.   function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }
  4. Whew. That worked.
  5. Save settings to /Users/tsingj/.plaidml? (y,n)[y]:y
  6. Success!
复制代码
回车,将配置信息写入默认配置文件中,完成配置。
运行采用 CPU运算的代码

本节中采用jupyter进行一个简单算法代码的运行,统计当时间。
step1 先导入keras包,导入数据cifar10,这里大概涉及外网下载,有问题可以参考keras利用基础问题

  1. #!/usr/bin/env python
  2. import numpy as np
  3. import os
  4. import time
  5. import keras
  6. import keras.applications as kapp
  7. from keras.datasets import cifar10
  8. (x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
  9. batch_size = 8
  10. x_train = x_train[:batch_size]
  11. 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利用基础问题

  1. model = kapp.VGG19()
复制代码
step3 模型编译

  1. model.compile(optimizer='sgd', loss='categorical_crossentropy',metrics=['accuracy'])
复制代码
step4 进行一次预测

  1. print("Running initial batch (compiling tile program)")
  2. y = model.predict(x=x_train, batch_size=batch_size)
复制代码
  Running initial batch (compiling tile program)
1/1 ━━━━━━━━━━━━━━━━━━━━ 1s 1s/step
  step5 进行10次预测

  1. # Now start the clock and run 10 batchesprint("Timing inference...")
  2. start = time.time()
  3. for i in range(10):
  4.     y = model.predict(x=x_train, batch_size=batch_size)
  5.     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相干操纵

  1. # Importing PlaidML. Make sure you follow this order
  2. import plaidml.keras
  3. plaidml.keras.install_backend()
  4. import os
  5. os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
复制代码
  注:
1、在采用plaidml=0.7.0 版本时,plaidml.keras.install_backend()操纵会发生报错
2、这步操纵,会通过plaidml导入keras,将背景运算引擎设置为plaidml,而不再采用tenserflow
  step1 先导入keras包,导入数据cifar10

  1. #!/usr/bin/env python
  2. import numpy as np
  3. import os
  4. import time
  5. import keras
  6. import keras.applications as kapp
  7. from keras.datasets import cifar10
  8. (x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
  9. batch_size = 8
  10. x_train = x_train[:batch_size]
  11. x_train = np.repeat(np.repeat(x_train, 7, axis=1), 7, axis=2)
复制代码
step2 导入盘算模型,如果本地不存在该模型数据,会主动进行下载

  1. model = kapp.VGG19()
复制代码
初次运行输出显卡信息
   INFO:plaidml:Opening device “metal_intel®_uhd_graphics_630.0”
  step3 模型编译

  1. model.compile(optimizer='sgd', loss='categorical_crossentropy',metrics=['accuracy'])
复制代码
step4 进行一次预测

  1. print("Running initial batch (compiling tile program)")
  2. y = model.predict(x=x_train, batch_size=batch_size)
复制代码
  Running initial batch (compiling tile program)
  由于输出较快,内容打印只有一行。
step5 进行10次预测

  1. # Now start the clock and run 10 batchesprint("Timing inference...")
  2. start = time.time()
  3. for i in range(10):
  4.     y = model.predict(x=x_train, batch_size=batch_size)
  5.     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
  1. (venv) tsingj@tsingjdeMacBook-Pro-2 ~  # plaidml-setup
  2. PlaidML 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.0   2 : metal_amd_radeon_pro_5300m.0Default device? (1,2)[1]:2Selected device:    metal_amd_radeon_pro_5300m.0Almost done. Multiplying some matrices...
  3. Tile code:
  4.   function (B[X,Z], C[Z,Y]) -> (A) { A[x,y : X,Y] = +(B[x,z] * C[z,y]); }
  5. Whew. That worked.
  6. Save settings to /Users/tsingj/.plaidml? (y,n)[y]:y
  7. Success!
复制代码
step0 通过plaidml导入keras,之后再做keras相干操纵

  1. # Importing PlaidML. Make sure you follow this order
  2. import plaidml.keras
  3. plaidml.keras.install_backend()
  4. import os
  5. os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"
复制代码
  注:
1、在采用plaidml=0.7.0 版本时,plaidml.keras.install_backend()操纵会发生报错
2、这步操纵,会通过plaidml导入keras,将背景运算引擎设置为plaidml,而不再采用tenserflow
  step1 先导入keras包,导入数据cifar10

  1. #!/usr/bin/env python
  2. import numpy as np
  3. import os
  4. import time
  5. import keras
  6. import keras.applications as kapp
  7. from keras.datasets import cifar10
  8. (x_train, y_train_cats), (x_test, y_test_cats) = cifar10.load_data()
  9. batch_size = 8
  10. x_train = x_train[:batch_size]
  11. x_train = np.repeat(np.repeat(x_train, 7, axis=1), 7, axis=2)
复制代码
step2 导入盘算模型,如果本地不存在该模型数据,会主动进行下载

  1. model = kapp.VGG19()
复制代码
  INFO:plaidml:Opening device “metal_amd_radeon_pro_5300m.0”
留意,这里初次执行输入了显卡信息。
  step3 模型编译

  1. model.compile(optimizer='sgd', loss='categorical_crossentropy',metrics=['accuracy'])
复制代码
step4 进行一次预测

  1. print("Running initial batch (compiling tile program)")
  2. y = model.predict(x=x_train, batch_size=batch_size)
复制代码
  Running initial batch (compiling tile program)
  由于输出较快,内容打印只有一行。
step5 进行10次预测

  1. # Now start the clock and run 10 batchesprint("Timing inference...")
  2. start = time.time()
  3. for i in range(10):
  4.     y = model.predict(x=x_train, batch_size=batch_size)
  5.     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在进行机器学习运算中的强盛上风。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美丽的神话

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表