让 TensorFlow 在 ARM Mac 上使用 GPU [英] Make TensorFlow use the GPU on an ARM Mac

查看:81
本文介绍了让 TensorFlow 在 ARM Mac 上使用 GPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据 这些说明.一切正常.

然而,模型训练是在 CPU 上进行的.如何将训练切换到 GPU?

在:tensorflow.config.list_physical_devices()输出:[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

Apple 的 TensorFlow 发行版 的文档中,我发现以下内容有点令人困惑 段落:

<块引用>

无需对现有的 TensorFlow 脚本进行任何更改即可使用 ML Compute 作为 TensorFlow 和 TensorFlow Addons 的后端.有一个可选的 mlcompute.set_mlc_device(device_name='any') API 用于 ML Compute 设备选择.device_name 的默认值为any",这意味着 ML Compute 将选择您系统上的最佳可用设备,包括多 GPU 配置上的多个 GPU.其他可用选项是 CPUGPU.请注意,在 Eager 模式下,ML Compute 将使用 CPU.例如,选择 CPU 设备,您可以执行以下操作:

# 导入 mlcompute 模块以使用可选的 set_mlc_device API 通过 ML Compute 进行设备选择.从 tensorflow.python.compiler.mlcompute 导入 mlcompute# 选择CPU设备.mlcompute.set_mlc_device(device_name='cpu') # 可用选项有 'cpu'、'gpu' 和 'any'.

所以我试着跑:

from tensorflow.python.compiler.mlcompute import mlcomputemlcompute.set_mlc_device(device_name='gpu')

并得到:

警告:tensorflow:Eager 模式使用 CPU.切换到 CPU.

此时我被卡住了.如何将 GPU 上的 keras 模型训练到我的 MacBook Air?

TensorFlow 版本:2.4.0-rc0

解决方案

更新

tensorflow_macos tf 2.4 存储库已被所有者归档.对于 tf 2.5,请参阅此处.>


完全禁用 Eager Execution 可能没有用,但是 tf.功能.试试这个并检查您的 GPU 使用情况,警告消息可能是误导.

 将 tensorflow 导入为 tftf.config.run_functions_eagerly(假)


Mac 优化的 TensorFlow 的当前版本有几个问题尚未修复(TensorFlow 2.4rc0).最终,急切模式是 TensorFlow 2.x 中的默认行为,并且在 TensorFlow-MacOS.但与官方不同的是,这个优化版强制使用CPU为eager模式.正如他们在此处所述.

<块引用>

... 在 Eager 模式下,ML Compute 将使用 CPU.

这就是为什么即使我们明确设置了 device_name='gpu',它也会在 Eager 模式仍然开启时切换回 CPU.

from tensorflow.python.compiler.mlcompute import mlcomputemlcompute.set_mlc_device(device_name='gpu')警告:tensorflow:Eager 模式使用 CPU.切换到 CPU.

禁用 Eager 模式 可能有助于程序利用 GPU,但这不是一般行为,可能导致到这样的 CPU/GPU 上令人费解的性能.目前,最合适的方法是选择 device_name='any',这样 ML Compute 将查询系统上可用的设备并选择最佳设备(s) 用于训练网络.

I have installed TensorFlow on an M1 (ARM) Mac according to these instructions. Everything works fine.

However, model training is happening on the CPU. How do I switch training to the GPU?

In: tensorflow.config.list_physical_devices()
Out: [PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

In the documentation of Apple's TensorFlow distribution I found the following slightly confusing paragraph:

It is not necessary to make any changes to your existing TensorFlow scripts to use ML Compute as a backend for TensorFlow and TensorFlow Addons. There is an optional mlcompute.set_mlc_device(device_name='any') API for ML Compute device selection. The default value for device_name is 'any', which means ML Compute will select the best available device on your system, including multiple GPUs on multi-GPU configurations. Other available options are CPU and GPU. Please note that in eager mode, ML Compute will use the CPU. For example, to choose the CPU device, you may do the following:

# Import mlcompute module to use the optional set_mlc_device API for device selection with ML Compute.
from tensorflow.python.compiler.mlcompute import mlcompute

# Select CPU device.
mlcompute.set_mlc_device(device_name='cpu') # Available options are 'cpu', 'gpu', and 'any'.

So I try to run:

from tensorflow.python.compiler.mlcompute import mlcompute
mlcompute.set_mlc_device(device_name='gpu')

and get:

WARNING:tensorflow: Eager mode uses the CPU. Switching to the CPU.

At this point I am stuck. How can I train keras models on the GPU to my MacBook Air?

TensorFlow version: 2.4.0-rc0

解决方案

Update

The tensorflow_macos tf 2.4 repository has been archived by the owner. For tf 2.5, refer to here.


It's probably not useful to disable the eager execution fully but to tf. functions. Try this and check your GPU usages, the warning message can be misleading.

import tensorflow as tf
tf.config.run_functions_eagerly(False)


The current release of Mac-optimized TensorFlow has several issues that yet not fixed (TensorFlow 2.4rc0). Eventually, the eager mode is the default behavior in TensorFlow 2.x, and that is also unchanged in the TensorFlow-MacOS. But unlike the official, this optimized version uses CPU forcibly for eager mode. As they stated here.

... in eager mode, ML Compute will use the CPU.

That's why even we set explicitly the device_name='gpu', it switches back to CPU as the eager mode is still on.

from tensorflow.python.compiler.mlcompute import mlcompute
mlcompute.set_mlc_device(device_name='gpu')

WARNING:tensorflow: Eager mode uses the CPU. Switching to the CPU.

Disabling the eager mode may work for the program to utilize the GPU, but it's not a general behavior and can lead to such puzzling performance on both CPU/GPU. For now, the most appropriate approach can be to choose device_name='any', by that the ML Compute will query the available devices on the system and selects the best device(s) for training the network.

这篇关于让 TensorFlow 在 ARM Mac 上使用 GPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆