GithubHelp home page GithubHelp logo

Comments (3)

cjmcgraw avatar cjmcgraw commented on June 1, 2024

One potential solution I've found is to manage the TrackableResources directly myself.

Consider the following code:

prev_model = loaded_model
loaded_model = tf.saved_model.load("/tmp/test-model")

if prev_model:
    lookups = filter(None, [
        layer._trackable_children().get('lookup_table') if hasattr(layer, '_trackable_children') else None
        for layer in prev_model._trackable_children().values()
    ])
    for lookup in lookups:
        tf.raw_ops.DestroyResourceOp(resource=lookup.resource_handle)

    del prev_model

Doing that after reloading appears to clear the resources as expected and not cause memory to grow indefinitely.

from tf-keras.

cjmcgraw avatar cjmcgraw commented on June 1, 2024

For any future readers. V2:

def wipeit(model):
    def _give_handles(m):
        if hasattr(m, 'resource_handle'):
            yield m.resource_handle
    
        children =  {}
        try:
            children = m._trackable_children()
        except:
            pass

        for child in children.values():
            yield from _give_handles(child)


    for handle in _give_handles(model):
        tf.raw_ops.DestroyResourceOp(resource=handle)


...

# during model load, after its loaded
prev_model = loaded_model
loaded_model = tf.saved_model.load(...)

if prev_model:
    wipeit(prev_model)
    del prev_model

from tf-keras.

divyashreepathihalli avatar divyashreepathihalli commented on June 1, 2024

@cjmcgraw thanks for filing the issue. This issues seems to be related to TF. This behavior can be reproduced without using any keras APIs. You can just use a lookup table and save and load the model -- the behavior would be the same.

from tf-keras.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.