当前位置: 代码网 > it编程>前端脚本>Python > 使用PyTorch实现限制GPU显存的可使用上限

使用PyTorch实现限制GPU显存的可使用上限

2024年05月18日 Python 我要评论
从 pytorch 1.4 版本开始,引入了一个新的功能 torch.cuda.set_per_process_memory_fraction(fraction, device),这个功能允许用户为特

从 pytorch 1.4 版本开始,引入了一个新的功能 torch.cuda.set_per_process_memory_fraction(fraction, device),这个功能允许用户为特定的 gpu 设备设置进程可使用的显存上限比例。

测试代码:

torch.cuda.empty_cache()
 
# 设置进程可使用的gpu显存最大比例为50%
torch.cuda.set_per_process_memory_fraction(0.5, device=0)
 
# 计算总内存
total_memory = torch.cuda.get_device_properties(0).total_memory
print("实际总内存:", round(total_memory / (1024 * 1024), 1), "mb")
 
# 尝试分配大量显存的操作
try:
    # 使用10%的显存:
    tmp_tensor = torch.empty(int(total_memory * 0.1), dtype=torch.int8, device='cuda:0')
    print("分配的内存:", round(torch.cuda.memory_allocated(0) / (1024 * 1024), 1), "mb")
    print("保留的内存:", round(torch.cuda.memory_reserved(0) / (1024 * 1024), 1), "mb")
    # 清空显存
    del tmp_tensor
    torch.cuda.empty_cache()
    # 使用50%的显存:
    torch.empty(int(total_memory * 0.5), dtype=torch.int8, device='cuda:0')
except runtimeerror as e:
    print("error allocating tensor:", e)
 
# 打印当前gpu的显存使用情况
print("分配的内存:", torch.cuda.memory_allocated(0) / (1024 * 1024), "mb")
print("保留的内存:", torch.cuda.memory_reserved(0) / (1024 * 1024), "mb")

结果如下

已分配显存:通过torch.cuda.memory_allocated(device)查询,它返回已经直接分配给张量的显存总量。这部分显存是当前正在被tensor对象使用的。

保留(预留)显存:通过torch.cuda.memory_reserved(device)查询,它包括了已分配显存以及一部分由pytorch的cuda内存分配器为了提高分配效率和减少cuda操作所需时间而预留的显存。这部分预留的显存不直接用于存储tensor对象的数据,但可以被视为快速响应未来显存分配请求的“缓冲区”。

知识补充

除了上文的方法,小编还为大家整理了一些其他pytorch限制gpu使用的方法,有需要的可以参考下

限制使用显存

# 指定之后所有操作在 gpu3 上执行
torch.cuda.set_device(3)

# 限制 gpu3 显存使用50%
desired_memory_fraction = 0.5  # 50% 显存
torch.cuda.set_per_process_memory_fraction(desired_memory_fraction)

# 获取当前gpu上的总显存容量
total_memory = torch.cuda.get_device_properties(3).total_memory

# 指定使用 gpu3
tmp_tensor = torch.empty(int(total_memory * 0.4999), dtype=torch.int8, device="cuda") # 此处 cuda 即指 gpu3

# 获取当前已分配的显存,计算可用显存
allocated_memory = torch.cuda.memory_allocated()
available_memory = total_memory - allocated_memory

# 打印结果
print(f"total gpu memory: {total_memory / (1024**3):.2f} gb")
print(f"allocated gpu memory: {allocated_memory / (1024**3):.2f} gb")
print(f"available gpu memory: {available_memory / (1024**3):.2f} gb")

此时占用了50%的显存,而将0.4999改为0.5会爆显存,可能是受浮点数精度影响。

pytorch限制gpu显存的函数与使用

函数形态

torch.cuda.set_per_process_memory_fraction(0.5, 0)

参数1:fraction 限制的上限比例,如0.5 就是总gpu显存的一半,可以是0~1的任意float大小;

参数2:device 设备号; 如0 表示gpu卡 0号;

使用示例:

import torch
# 限制0号设备的显存的使用量为0.5,就是半张卡那么多,比如12g卡,设置0.5就是6g。
torch.cuda.set_per_process_memory_fraction(0.5, 0)
torch.cuda.empty_cache()
# 计算一下总内存有多少。
total_memory = torch.cuda.get_device_properties(0).total_memory
# 使用0.499的显存:
tmp_tensor = torch.empty(int(total_memory * 0.499), dtype=torch.int8, device='cuda')

# 清空该显存:
del tmp_tensor
torch.cuda.empty_cache()

# 下面这句话会触发显存oom错误,因为刚好触碰到了上限:
torch.empty(total_memory // 2, dtype=torch.int8, device='cuda')

"""
it raises an error as follows: 
runtimeerror: cuda out of memory. tried to allocate 5.59 gib (gpu 0; 11.17 gib total capacity; 0 bytes already allocated; 10.91 gib free; 5.59 gib allowed; 0 bytes reserved in total by pytorch)
"""
显存超标后,比不设置限制的错误信息多了一个提示,“5.59 gib allowed;”

注意事项:

函数限制的是进程的显存,这点跟tensorflow的显存限制类似。

到此这篇关于使用pytorch实现限制gpu显存的可使用上限的文章就介绍到这了,更多相关pytorch限制gpu使用上限内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com