【发布时间】:2021-10-18 09:54:44
【问题描述】:
我想在 jupyter notebook 中使用 Tensorflow 时释放和重用 GPU。
我想像这样的工作流程:
- 进行 TF 计算。
- 释放 GPU
- 等一下
- 第 1 步。
这是我正确使用的代码。步骤 1 到 3 有效,步骤 4 无效:
import time
import tensorflow as tf
from numba import cuda
def free_gpu():
device = cuda.get_current_device()
cuda.close()
def test_calc():
a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
# Run on the GPU
c = tf.matmul(a, b)
test_calc()
free_gpu()
time.sleep(10)
test_calc()
如果我在 Jupyter Notebooks 中运行此代码,我的内核就会死掉。
cuda.close() 和 cuda.close() 是否有替代方案可以在不破坏 TF 的情况下释放 GPU?
【问题讨论】:
-
真正简短的回答是不要打电话给
numba.cuda.close()。这会杀死 tensorflow 绑定的上下文,然后什么都不会起作用
标签: python jupyter-notebook cuda tensorflow2.0 numba