[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release GPU Memory(VRAM) after tf.keras.backend.clear_session() #39535

Closed
Arktius opened this issue May 14, 2020 · 5 comments
Closed

Release GPU Memory(VRAM) after tf.keras.backend.clear_session() #39535

Arktius opened this issue May 14, 2020 · 5 comments
Assignees
Labels
comp:gpu GPU related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:performance Performance Issue

Comments

@Arktius
Copy link
Arktius commented May 14, 2020

System information

  • Windows 10 Microsoft Windows [Version 10.0.18362.418]
  • TensorFlow 2.0 installed from Conda:
  • Python version: 3.6.10
  • CUDA/cuDNN version: NVIDIA-SMI 445.75 Driver Version: 445.75 CUDA Version: 11.0
  • GPU model and memory: NVIDIA 2060S

Describe the current behavior
There's no command which frees the previously used VRAM. Even deleting the model and the data had no effect on the VRAM.

Describe the expected behavior
Any of these commands should release the VRAM.

Standalone code to reproduce the issue

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras.models import Sequential
from tensorflow import math, dtypes
from tensorflow import float32 as f32 
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Input
import random
import numpy as np # linear algebra
import gc

rseed=10
np.random.seed(rseed)
random.seed(rseed)
tf.compat.v1.set_random_seed(rseed)
    
def MMSE( preds,targets, mask_value=0.0):
    tf.print('\npred',preds)
    tf.print('target',targets)
    mask = dtypes.cast(tf.not_equal(targets,0),f32) 
    num_rating = math.reduce_sum(mask) #count ratings
    loss = math.reduce_sum(math.square(mask*(preds - targets))) / num_rating 
    return loss


input_dim = Input(shape = (3, ))
model = Sequential()
model.add(Dense(3,input_dim=3))
model.add(Dense(3))
model.compile(optimizer = Adam(lr=0.01),loss=[MMSE]) 
            
data  = tf.math.round(tf.random.normal(shape=[5,3]))
history = model.fit(data,data, epochs = 1, batch_size = 5,verbose=0, shuffle=False) 

del input_dim,model,data,history
tf.compat.v1.reset_default_graph()
tf.keras.backend.clear_session()
gc.collect()

I've used nvidia-smi to check the memory-usage.

@byronyi
Copy link
Contributor
byronyi commented May 15, 2020

It is currently not possible without exiting the Python process due to the fact that many TF internal objects, e.g. GPU memory pool, device streams, do not support clean shutdown.

@ravikyram ravikyram added comp:gpu GPU related issues TF 2.0 Issues relating to TensorFlow 2.0 type:performance Performance Issue and removed type:bug Bug labels May 15, 2020
@ravikyram ravikyram assigned gowthamkpr and unassigned ravikyram May 15, 2020
@Arktius
Copy link
Author
Arktius commented May 15, 2020

What a pity. Can I somehow estimate it by using the number of trainable parameters and the batch size?

@Arktius
Copy link
Author
Arktius commented May 15, 2020

One could check the model size or the trainable parameters, but this is somehow not satisfying. Source

@gowthamkpr
Copy link

@Arktius As this issue has been answered, can you please create a new issue and close this one. Thanks!

@gowthamkpr gowthamkpr added the stat:awaiting response Status - Awaiting response from author label May 16, 2020
@Arktius Arktius closed this as completed May 16, 2020
@ELind77
Copy link
ELind77 commented Aug 14, 2020

It is currently not possible without exiting the Python process due to the fact that many TF internal objects, e.g. GPU memory pool, device streams, do not support clean shutdown.

This seems like a core deficiency in the library. Is there a followup ticket for a line of work to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:gpu GPU related issues stat:awaiting response Status - Awaiting response from author TF 2.0 Issues relating to TensorFlow 2.0 type:performance Performance Issue
Projects
None yet
Development

No branches or pull requests

5 participants