Skip to content

2.0 Compile is leaking memory. #26108

Closed
Closed

Description

@worthy7

System information

  • 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.

Activity

  1. worthy7 commented on Feb 26, 2019

    @worthy7
    Author

    Changing the parameters of compile seems to makes no difference. And the size of the network also doesn't seem to make a difference.

  2. added
    TF 2.0
    comp:keras
    on Feb 26, 2019
  3. added
    stat:awaiting tensorflower
    on Feb 26, 2019
  4. kotritrona commented on Apr 21, 2019

    @kotritrona

    Me too... Hope this can get solved soon, for now I have to use an old version

  5. wangyexiang commented on May 5, 2019

    @wangyexiang

    Same issue... And K.clear_session() doesn't work.

  6. added
    stat:awaiting response
    stat:awaiting tensorflower
    and removed
    stat:awaiting tensorflower
    stat:awaiting response
    on May 31, 2019
  7. jvishnuvardhan commented on Jun 6, 2019

    @jvishnuvardhan
    Contributor

    @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()
    
  8. worthy7 commented on Jun 10, 2019

    @worthy7
    Author

    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.

  9. removed
    stat:awaiting response
    on Jun 10, 2019
  10. robieta commented on Jun 10, 2019

    @robieta

    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.

  11. tensorflow-bot commented on Jun 10, 2019

    @tensorflow-bot

    Are you satisfied with the resolution of your issue?
    Yes
    No

  12. worthy7 commented on Jun 11, 2019

    @worthy7
    Author

    @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!

  13. wangyexiang commented on Jul 3, 2019

    @wangyexiang

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

TF 2.0comp:kerastype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions