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

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

问题描述

我已经根据 ARM )Mac上安装了 TensorFlow "rel ="nofollow noreferrer">这些说明.一切正常.

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

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

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')]

Apple的TensorFlow发行版的文档中,我发现以下一些令人困惑的

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

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

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'.

所以我尝试运行:

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.

这时我被困住了.如何在GPU上将 keras 模型训练到MacBook Air?

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

TensorFlow版本: 2.4.0-rc0

TensorFlow version: 2.4.0-rc0

推荐答案

完全禁用急切执行但对 tf可能没有用.功能.尝试此操作并检查您的GPU使用情况,警告消息可能为误导

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)


Mac优化的TensorFlow 的当前发行版有几个问题尚未解决的问题( TensorFlow 2.4rc0 ).最终,渴望模式是 TensorFlow 2.x 中的默认行为,并且在此处所述.


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.

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

这就是为什么即使我们明确设置了 device_name ='gpu'的原因,由于仍处于急切模式,它也会切换回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.

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

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天全站免登陆