Kuaishou 中期考核-降低显存的关键

本文介绍了如何在PyTorch中安全地读取模型参数,避免占用GPU资源,通过`torch.cuda.memory_allocated()`和`torch.cuda.max_memory_allocated()`等函数监控GPU内存使用,并提供了检测CPU虚拟内存的方法。
摘要由CSDN通过智能技术生成

读入模型参数-且不占用显卡

see_memory_usage(‘message’)

# 4. 读入checkpiont参数
    state_dict=torch.load('../train-output/'+ args.model_name_or_path.split('/')[-1] +'/unet/diffusion_pytorch_model.bin', map_location='cpu')
    # state_dict=torch.load('../train-output/'+ args.model_name_or_path.split('/')[-1] +'/unet/diffusion_pytorch_model.bin')

如果不加map_location='cpu',读入的参数就会占用GPU

see_memory_usage(‘message’)

可以通过添加下面函数来检测GPU占用情况

import gc
import psutil
def see_memory_usage(message):
 
    # python doesn't do real-time garbage collection so do it explicitly to get the correct RAM reports
    gc.collect()
 
    # Print message except when distributed but not rank 0
    print(message)
    print(f"MA {round(torch.cuda.memory_allocated() / (1024 * 1024 * 1024),2 )} GB \
        Max_MA {round(torch.cuda.max_memory_allocated() / (1024 * 1024 * 1024),2)} GB \
        CA {round(torch.cuda.memory_reserved() / (1024 * 1024 * 1024),2)} GB \
        Max_CA {round(torch.cuda.max_memory_reserved() / (1024 * 1024 * 1024))} GB ")
 
    vm_stats = psutil.virtual_memory()
    used_GB = round(((vm_stats.total - vm_stats.available) / (1024**3)), 2)
    print(f'CPU Virtual Memory:  used = {used_GB} GB, percent = {vm_stats.percent}%')
 
    # get the peak memory to report correct data, so reset the counter for the next call
    if hasattr(torch.cuda, 'reset_peak_memory_stats'):
        return torch.cuda.reset_peak_memory_stats()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

计算机视觉-Archer

图像分割没有团队的同学可加群

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值