You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Have I written custom code (as opposed to using a stock example script provided in TensorFlow): No
OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Windows 10 (2 different machines)
TensorFlow installed from (source or binary): Binary
TensorFlow version (use command below): gpu 2.0.0-dev20190214, gpu 2.0.0-dev20190224
Python version: 3.6.6
CUDA/cuDNN version: 10
GPU model and memory: 7gb K5200, 4gb GTX 970
Describe the current behavior
Compile leaks memory. Describe the expected behavior
Compile clears the memory when overwritten Code to reproduce the issue
Provide a reproducible test case that is the bare minimum necessary to generate the problem.
import tensorflow as tf
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
for i in range(100000):
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Memory slowly increases about 20mb at a time.
I discovered this whilst working on a genetic algorithm which generates models which are then compiled and tested, but it fills my memory and I have narrowed it down to this simple code.
@worthy7 Can you try this gist and let me know if the issue still persists. I don't see any issue if I add tf.keras.backend.clear_session() to the end of your code.
import tensorflow as tf
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
for i in range(100000):
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
tf.keras.backend.clear_session()
That seems to work in that notebook on the nightly build.
In my original 2.0a the clear_session() doesn't fix it.
So I assume you fixed a bug somewhere in clear_session() which seems to work.
I think that clear_session shouldn't be necessary really... don't you?
I feed there should be no residue from a previous compile when running another compile on the same model.
Given that compile does add ops to the graph (gradient ops, metric variables, etc.), I think this probably reasonable since compile is an infrequent call. (And yes, there was a bug with clear_session in the 2.0 alpha. It should be fixed in the beta.) See #28844 for an explanation of why there is some unnecessarily persisted state for the time being.
That seems to work in that notebook on the nightly build.
In my original 2.0a the clear_session() doesn't fix it.
So I assume you fixed a bug somewhere in clear_session() which seems to work.
I think that clear_session shouldn't be necessary really... don't you?
I feed there should be no residue from a previous compile when running another compile on the same model.
I agree with you @worthy7 . A similar situation, if I train the model in the main thread and load the model in another thread AT THE SAME TIME. All things is in a loop. However, if I use clear_session() method in one thread, the code in another thread won't work!!! I test pytorch and mxnet , and there is no any memory leak in a loop. why??? amazing tensorflow!!! I think that clear_session shouldn't be necessary.
Activity
worthy7 commentedon Feb 26, 2019
Changing the parameters of
compileseems to makes no difference. And the size of the network also doesn't seem to make a difference.kotritrona commentedon Apr 21, 2019
Me too... Hope this can get solved soon, for now I have to use an old version
wangyexiang commentedon May 5, 2019
Same issue... And K.clear_session() doesn't work.
jvishnuvardhan commentedon Jun 6, 2019
@worthy7 Can you try this gist and let me know if the issue still persists. I don't see any issue if I add
tf.keras.backend.clear_session()to the end of your code.worthy7 commentedon Jun 10, 2019
That seems to work in that notebook on the nightly build.
In my original 2.0a the clear_session() doesn't fix it.
So I assume you fixed a bug somewhere in clear_session() which seems to work.
I think that clear_session shouldn't be necessary really... don't you?
I feed there should be no residue from a previous compile when running another compile on the same model.
robieta commentedon Jun 10, 2019
I'm not able to replicate your 20 mb figure; I see an increase of just under 0.5 mb / compile:
https://colab.sandbox.google.com/gist/robieta/9820655313efc01f15bd864110e98a45/sequential_leak_test.ipynb
Given that compile does add ops to the graph (gradient ops, metric variables, etc.), I think this probably reasonable since compile is an infrequent call. (And yes, there was a bug with clear_session in the 2.0 alpha. It should be fixed in the beta.) See #28844 for an explanation of why there is some unnecessarily persisted state for the time being.
tensorflow-bot commentedon Jun 10, 2019
Are you satisfied with the resolution of your issue?
Yes
No
worthy7 commentedon Jun 11, 2019
@robieta I said "at a time", I didn't mean "per compile" :) I was just watching the task manager window, probably per second.
Thanks for sorting it!
wangyexiang commentedon Jul 3, 2019
I agree with you @worthy7 . A similar situation, if I train the model in the main thread and load the model in another thread AT THE SAME TIME. All things is in a loop. However, if I use clear_session() method in one thread, the code in another thread won't work!!! I test pytorch and mxnet , and there is no any memory leak in a loop. why??? amazing tensorflow!!! I think that clear_session shouldn't be necessary.