spyder-ide / spyder-kernels

Jupyter Kernels for the Spyder console
MIT License
38 stars 39 forks source link

@tf.function works the first time running the code but fails afterwards on Spyder IDE. #241

Open eliphatfs opened 4 years ago

eliphatfs commented 4 years ago

I turned here from a TensorFlow issue related with Spyder. I copy my contents there, together with an analysis.

import tensorflow as tf

@tf.function
def mean(a):
    return tf.reduce_mean(tf.abs(a))

Output:

runfile('D:/tfbug.py', wdir='D:/')

mean([1, 2])
Out[2]: <tf.Tensor: id=9, shape=(), dtype=int32, numpy=1>

mean([1, 2])
Out[3]: <tf.Tensor: id=10, shape=(), dtype=int32, numpy=1>

runfile('D:/tfbug.py', wdir='D:/')
Reloaded modules: tmpv1iph1dn

mean([1, 2])
WARNING: Logging before flag parsing goes to stderr.
W0718 20:43:06.847428 10664 ag_logging.py:146] Entity <function mean at 0x000000001DB28B70> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: 
WARNING: Entity <function mean at 0x000000001DB28B70> could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: 
Out[5]: <tf.Tensor: id=19, shape=(), dtype=int32, numpy=1>

Appending the following code, the warning disappears:

try:
    import os
    import sys
    from spyder_kernels.customize import spydercustomize as spc
    if spc.__umr__ is None:
        namelist = os.environ.get("SPY_UMR_NAMELIST", None)
        if namelist is not None:
            namelist = namelist.split(',')
        spc.__umr__ = spc.UserModuleReloader(namelist=namelist)
    spc.__umr__.namelist += \
        list(filter(lambda x: x.startswith("tmp"), sys.modules.keys()))
except ImportError:
    pass

In umr.py where the User Module Reloader is defined, there is a blocklist which may cause issues with the reloader and won't be reloaded. Here tf.function temporary modules are in a name starting with tmp, which may be a characteristic to match with.

My suggestions: Either

Or

ccordoba12 commented 4 years ago

Hey @strongrex2001, I'm very sorry for big delay in commenting about your suggestion (I totally missed this issue). Is it ok if for now we simply add this line

spc.__umr__.namelist += \
    list(filter(lambda x: x.startswith("tmp"), sys.modules.keys()))

and later consider how to implement the ability to exclude modules from our UMR using regexp's?

eliphatfs commented 4 years ago

Sorry for my delay in reply, and.. no, since we only have the modules generated after tensorflow is run, and when umr is initialized there is no such tmp module in sys.modules.