Tensorflow中的GPU分配方法

2023-11-10

Tensorflow中的GPU分配方法

默认情况下 TensorFlow 会使用其所能够使用的所有 GPU,这样,会出现浪费的情况。

列出当前设备上的GPU和CPU

首先,通过 tf.config.experimental.list_physical_devices,我们可以获得当前主机上某种特定运算设备类型(如 GPU 或 CPU )的列表,例如,在一台具有 4 块 GPU 和一个 CPU 的工作站上运行以下代码:

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print(gpus, cpus)

输出如下:

[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:1', device_type='GPU'), 
PhysicalDevice(name='/physical_device:GPU:2', device_type='GPU'), PhysicalDevice(name='/physical_device:GPU:3', device_type='GPU')] 
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]

可见,该工作站具有 4 块 GPU:GPU:0 、 GPU:1 、 GPU:2 、 GPU:3 ,以及一个 CPU CPU:0 。

控制程序可以使用的GPU

然后,通过 tf.config.experimental.set_visible_devices ,可以设置当前程序可见的设备范围(当前程序只会使用自己可见的设备,不可见的设备不会被当前程序使用)。例如,如果在上述 4 卡的机器中我们需要限定当前程序只使用下标为 0、1 的两块显卡(GPU:0 和 GPU:1),可以使用以下代码:

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
tf.config.experimental.set_visible_devices(devices=gpus[0:2], device_type='GPU')python

使用环境变量 CUDA_VISIBLE_DEVICES 也可以控制程序所使用的 GPU。假设发现四卡的机器上显卡 0,1 使用中,显卡 2,3 空闲,Linux 终端输入:

export CUDA_VISIBLE_DEVICES=2,3

或者直接在命令行前增加:

CUDA_VISIBLE_DEVICES=2,3 python do_something.py

或者在代码中加入

import os
os.environ['CUDA_VISIBLE_DEVICES'] = "2,3"

即可指定程序只在显卡 2,3 上运行。

控制GPU的显存

默认情况下,TensorFlow 将使用几乎所有可用的显存,以避免内存碎片化所带来的性能损失。不过,TensorFlow 提供两种显存使用策略,让我们能够更灵活地控制程序的显存使用方式:

  • 仅在需要时申请显存空间(程序初始运行时消耗很少的显存,随着程序的运行而动态申请显存);
  • 限制消耗固定大小的显存(程序不会超出限定的显存大小,若超出的报错)。

按需申请显存空间

可以通过 tf.config.experimental.set_memory_growth 将 GPU 的显存使用策略设置为 “仅在需要时申请显存空间”。以下代码将所有 GPU 设置为仅在需要时申请显存空间:

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(device=gpu, True)

限制显存使用最大值

以下代码通过 tf.config.experimental.set_virtual_device_configuration 选项并传入 tf.config.experimental.VirtualDeviceConfiguration 实例,设置 TensorFlow 固定消耗 GPU:0 的 1GB 显存(其实可以理解为建立了一个显存大小为 1GB 的 “虚拟 GPU”):

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
tf.config.experimental.set_virtual_device_configuration(
    gpus[0],
    [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])

Tensorflow 1.x的设置方法

TensorFlow 1.X 的 Graph Execution 下,可以在实例化新的 session 时传入 tf.compat.v1.ConfigPhoto 类来设置 TensorFlow 使用显存的策略。具体方式是实例化一个 tf.ConfigProto 类,设置参数,并在创建 tf.compat.v1.Session 时指定 Config 参数。以下代码通过 allow_growth 选项设置 TensorFlow 仅在需要时申请显存空间:

config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.compat.v1.Session(config=config)

以下代码通过 per_process_gpu_memory_fraction 选项设置 TensorFlow 固定消耗 40% 的 GPU 显存:

config = tf.compat.v1.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
tf.compat.v1.Session(config=config)

单 GPU 模拟多 GPU 环境

当我们的本地开发环境只有一个 GPU,但却需要编写多 GPU 的程序在工作站上进行训练任务时,TensorFlow 为我们提供了一个方便的功能,可以让我们在本地开发环境中建立多个模拟 GPU,从而让多 GPU 的程序调试变得更加方便。以下代码在实体 GPU GPU:0 的基础上建立了两个显存均为 2GB 的虚拟 GPU。

gpus = tf.config.experimental.list_physical_devices('GPU')
tf.config.experimental.set_virtual_device_configuration(
    gpus[0],
    [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048),
     tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)])

我们在 单机多卡训练 的代码前加入以上代码,即可让原本为多 GPU 设计的代码在单 GPU 环境下运行。当输出设备数量时,程序会输出:

Number of devices: 2

参考资料:https://tf.wiki/zh/basic/tools.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Tensorflow中的GPU分配方法 的相关文章

随机推荐