pytorch的torch.cuda.set_per_process_memory_fraction()函数介绍

本文介绍了PyTorch 1.8.0及更高版本中的torch.cuda.set_per_process_memory_fraction()函数,用于设置GPU显存使用比例,以限制显存消耗上限。当尝试分配超过限制的内存时,会引发内存错误。该函数不适用于限制子进程的显存,需在子进程中单独设置。
摘要由CSDN通过智能技术生成

功能:

pytorch(版本1.8.0以后)中设置显存使用比例,即能够完成显存的使用上限设置,用户可以根据需要限制GPU的显存消耗上限。

torch.cuda.set_per_process_memory_fraction(0.5, 0)
     参数1:fraction 限制的上限比例,如0.5 就是总GPU显存的一半,可以是0~1的任意float大小;
     参数2:device 设备号; 如0 表示GPU卡 0号;

功能解释如下:
Set memory fraction for a process. The fraction is used to limit an caching allocator to allocated memory on a CUDA device. The allowed value equals the total visible memory multiplied fraction. If trying to allocate more than the allowed value in a process, will raise an out of memory error in allocator.
参看https://pytorch.org/docs/1.8.0/cuda.html

示例:

import
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值