tensorflow/python/autograph/g3doc/reference/generated_code.md
For each converted function, AutoGraph creates a new function. The
loading mechanism is an implementation detail and may change, but the
generated function is generally a regular
Python function.
This function is typically executed internally by @tf.function to construct a
TensorFlow graph.
The generated code is a transformation of the input code. The transformations are listed below. Any other elements are left unchanged.
Summary of transformations:
foo(args) -> ag__.converted_call(foo, args)if, while and for statements are replaced with function calls:
if -> ag__.if_stmtwhile -> ag__.while_stmtfor -> ag__.for_stmtbreak, return, and continue statements are replaced with equivalent
if statements.and, or and not operators are replaced with function calls:
and -> ag__.and_or -> ag__.or_not -> ag__.not_The functions replacing control flow statements are very similar in form with the corresponding control flow ops in TensorFlow.
You can interact normally with the generated code. For example, you can use
the inspect.getsourcefile and inspect.getsource:
def f(a):
...
converted_f = tf.autograph.to_graph(f)
print(inspect.getsourcefile(converted_f))
/tmp/tmpm562wlj7.py
When using @tf.function, you can repeat the same steps using the function's
python_function attribute:
@tf.function
def f(a):
...
converted_f = tf.autograph.to_graph(f.python_function)
print(inspect.getsourcefile(converted_f))
/tmp/tmpm562wlj7.py
tf.autograph.to_code(f) is a shortcut to obtain the generated code, and it's
equivalent with calling inspect.getsource(tf.autograph.to_graph(f)).
tf.autograph.set_verbosityAutoGraph can log additional debug information. This is mostly used for filing bugs, but can also be used to get an indication of whether a function is converted successfully or not.
You can enable logging by calling tf.autograph.set_verbosity(level). The
level argument varies from 0 to 10:
Caution: The information being logged includes source code as well as data. Before sharing AutoGraph logs, make sure they don't contain any sensitive information.
Alternatively, you can control the verbosity level using the environment
variable AUTOGRAPH_VERBOSITY.