Monday 25 October 2021

Freeing and Reusing GPU in Tensorflow

I would like to free and Reuse the GPU while using Tensorflow in a jupyter notebook.

I imagen a workflow like this:

  1. Make a TF calculation.
  2. Free the GPU
  3. Wait a while
  4. Step 1. again.

This is the code i use right no. Steps 1 to 3 are working step 4 is not:

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()

If i run this code in Jupyter Notebooks my kernel just dies. Is there a alternetiv to cuda.close() and cuda.close() that frees the GPU while not breaking TF?



from Freeing and Reusing GPU in Tensorflow

No comments:

Post a Comment