Проблемы с управлением памятью графического процессора при использовании TensorFlow

| Процессы: Память GPU | | GPU PID Тип Имя процесса Использование
| 0 6944 C python3 11585MiB | | 1 6944 C python3 11587MiB | | 2 6944 C python3 10621MiB |

nvidia-smi память не освобождается после остановки тензорного потока в середине.

Пытался использовать это

config = tf.ConfigProto()
config.gpu_options.allocator_type = 'BFC'
config.gpu_options.per_process_gpu_memory_fraction = 0.90
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)

Также

with tf.device('/gpu:0'):
with tf.Graph().as_default():

Пробовал сбрасывать графический процессорsudo nvidia-smi --gpu-reset -i 0

Память не может быть освобождена вообще.

1 ответ

Решение

Решение было получено из набора Tensorflow CUDA_VISIBLE_DEVICES в рамках компании Jupyter благодаря Ярославу.

Большая часть информации была получена из документации Tensorflow Stackru. Мне не разрешено не разрешено размещать это не знаю почему.

Вставьте это в начале вашего кода.

from tensorflow.python.client import device_lib

# Set the environment variables
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

# Double check that you have the correct devices visible to TF
print("{0}\nThe available CPU/GPU devices on your system\n{0}".format('=' * 100))
print(device_lib.list_local_devices())

Different options to start with GPU or CPU. I am using the CPU. Can be changed from the below options
with tf.device('/cpu:0'):
# with tf.device('/gpu:0'):
# with tf.Graph().as_default():

Используйте следующие строки в сеансе:

config = tf.ConfigProto(device_count={'GPU': 1}, log_device_placement=False,
                        allow_soft_placement=True)
# allocate only as much GPU memory based on runtime allocations
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
# Session needs to be closed
sess.close()

Следующая строка исправит проблему ресурсов, заблокированных python

with tf.Session(config=config) as sess:

Еще одна полезная статья, чтобы понять важность 'with'. Пожалуйста, проверьте официальный tf.Session() из tenorflow.

Объяснение параметра

    To find out which devices your operations and tensors are assigned to, create the session with 
    log_device_placement configuration option set to True.

    TensorFlow to automatically choose an existing and supported device to run the operations in case the specified 
    one doesn't exist, you can set allow_soft_placement=True in the configuration option when creating the session.
Другие вопросы по тегам