【问题标题】:How to free GPU from CUDA (using Pytorch)?如何从 CUDA 中释放 GPU(使用 Pytorch)?
【发布时间】:2022-06-21 02:52:04
【问题描述】:

我正在使用 spark/face-alignment 来生成几乎相同的面孔。

 fa = face_alignment.FaceAlignment(face_alignment.LandmarksType._2D, flip_input=False) # try to use GPU with Pytorch depenencies.
 imageVector.append( convertImagefa(image, fa))
 del fa
 gc.collect()
 torch.cuda.empty_cache() # trying to clean up cuda.
 return imageVector

我在一台有 4 个线程的机器上,所有线程都试图访问 GPU。因此,我制定了一个策略,即每 4 个请求就使用 GPU。这似乎适合记忆。

我的问题是,当我在 cuda 之后进行清理时,它实际上从未完全清理过。我会看到负载在线程周围移动并释放了一些空间,但 CUDA 永远不会放开最后的 624MiB。有没有办法彻底清理干净?

nvidia-smi                                                                                                                                                              
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A     17132  C   .../face-the-same/bin/python      624MiB |
|    0   N/A  N/A     17260  C   .../face-the-same/bin/python     1028MiB |
|    0   N/A  N/A     17263  C   .../face-the-same/bin/python      624MiB |
|    0   N/A  N/A     17264  C   .../face-the-same/bin/python      624MiB |

仅供参考:我最终使用分布式锁将 GPU 计算固定到一个执行程序/进程 ID。这是来自@Jan 的评论的结果。

【问题讨论】:

    标签: python pytorch cuda


    【解决方案1】:

    根据https://discuss.pytorch.org/t/pytorch-do-not-clear-gpu-memory-when-return-to-another-function/125944/3,这是由于除非您结束脚本,否则 CUDA 上下文仍然存在。他们建议调用torch.cuda.empty_cache() 来清除缓存,但是总会有剩余的。要摆脱这种情况,您可以切换到进程而不是线程,以便可以在不杀死您的程序的情况下实际杀死进程(但我想这将是相当大的努力)。

    【讨论】:

      猜你喜欢
      • 2022-01-27
      • 2020-11-18
      • 2021-07-03
      • 2020-05-27
      • 2020-03-14
      • 2019-02-11
      • 2020-05-22
      • 2023-02-24
      • 2012-01-31
      相关资源
      最近更新 更多