[empty_cache]清空显存

查看工程源码时发现在训练完成后,测试模型之前调用了函数torch.cuda.empty_cache()

logger.info('Start evaluating...')
torch.cuda.empty_cache()  # speed up evaluating after training finished
do_evaluation(cfg, model, distributed=args.distributed)

其作用是释放缓存分配器当前持有的所有未占用的缓存内存,以便这些内存可以在其他GPU应用程序中使用,并在nvidia-smi中可见

使用

对于何时使用该函数清空缓存内存,参考:

About torch.cuda.empty_cache()

What is torch.cuda.empty_cache do and where should i add it?

Why does torch.cuda.empty_cache() make the GPU utilization near 0 and slow down the training time?

pytorch GPU显存释放的问题?

并不推荐在实现中频繁调用该函数。仅在显存不足时进行调用即可