• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# AutoGraph reference
2
3[Index](index.md)
4
5## Common AutoGraph errors
6
7### "WARNING: AutoGraph could not transform `<name>`"
8
9This warning is output when AutoGraph could not convert a function, for an
10unexpected reason. The error message contains the reason why the function could
11not be converted, as well as guidance on how to proceed next.
12
13The exact error message may vary from version to version but in general, the
14cause of the failure appears somewhere in the text, for example as
15"Cause: could not get source code" or "Original error: could not get source
16code".
17
18Note: AutoGraph does not always output a warning. For example, constructors
19are silently called without conversion.
20
21When this warning is printed, the code returned by AutoGraph still executes, but
22the functions indicated in the warning will be executed as they are, without
23conversion. If the functions contain pure Python or graph code (for example,
24they have no Tensor-dependent control flow), then the code is likely to still
25run without error. However, if it contains any constructs that are only
26supported in AutoGraph, expect subsequent exceptions.
27
28Note: the warning is output to the [abseil](https://github.com/abseil/abseil-py)
29logger, with `WARNING` severity. To direct these warnings to `stdout`, use
30`tf.autograph.set_verbosity(0, True)`.
31
32### "Unable to locate the source code" or "Source not found" errors
33
34Newer versions of AutoGraph raise a `ConversionError`. Older versions print a
35warning. In both cases, a similar message about finding the source code is
36included.
37
38These errors are raised when AutoGraph is unable to find the source code of
39functions it needs to transform. See [Limitations](limitations.md) for more
40details.
41
42### "WARNING: Large unrolled loop detected"
43
44This warning is output when AutoGraph detects a `for` or `while` loop that
45creates TensorFlow ops and which has a large number of iterations and creates.
46
47This usually indicates a loop that was intended to run as a `tf.while_loop`, but
48instead runs as a Python loop.
49
50For example, a training loop might mistakenly iterate over a Python `range`,
51instead of `tf.range`:
52
53```
54num_steps = 10000
55step = tf.constant(0)
56for i in range(num_steps):
57  step += 1
58  train_step(model)
59```
60
61Another example is when using custom generators which AutoGraph does not
62support, even if they wrap over supported iterators like Datasets:
63
64```
65def my_iterator(ds):
66  for data in ds:
67    yield data
68
69# Custom iterators always dispatch to a Python for loop.
70for x in my_iterator(tf.data.Dataset.range(10)):
71  tf.print(x)
72```
73
74Note: This verification is only performed when `__debug__` is `True`.
75
76Note: the warning is output to the [abseil](https://github.com/abseil/abseil-py)
77logger, with `WARNING` severity. To direct these warnings to `stdout`, use
78`tf.autograph.set_verbosity(0, True)`.
79
80### "OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool`"
81
82This exception is raised whenever a `tf.Tensor` is type-cast as a Python `bool`,
83in a context where eager execution is not active. The exception is only raised
84when graph execution is active, for example inside a `@tf.function` with
85AutoGraph turned off.
86
87**When AutoGraph is on**, it can be caused by:
88  * placing a Tensor-dependent `break`, `continue` or `return` inside a Python
89    loop (see example below)
90  * attempting to use a `tf.Tensor` in a list comprehension, by iterating over
91    it or using it in a condition)
92
93A typical example of mixing Python and TF control flow in an incompatible way
94is:
95
96```
97for i in range(3):  # Python loop
98  if i > tf.constant(0):  # TF conditional
99    break  # raises OperatorNotAllowedInGraphError
100```
101
102The way these errors are typically fixed is by ensuring all control flow is
103TF control flow:
104
105```
106for i in tf.range(3):  # TF loop
107  if i > tf.constant(0):  # TF conditional
108    break  # works
109```
110
111**When AutoGraph is off**, it can be caused by using a `tf.Tensor` value as:
112
113  * the condition of an `if` or `while` statement: `if <tensor>:`
114  * the argument in a logical expression: `tensor and another_tensor`
115  * the argument to the `bool` built-in: `bool(tensor)`
116
117Note: These operations are allowed when executing eagerly.
118
119When encountering this error, make sure that the function is either decorated
120with `@tf.function`, or called from another function decorated in this way. Also
121look at the console and logging output for conversion warnings (see the section
122above).
123
124### "OperatorNotAllowedInGraphError: iterating over `tf.Tensor`"
125
126This exception is raised whenever you try to iterate over a `tf.Tensor`,
127in a context where eager execution is not active. The exception is only raised
128when graph execution is active, for example inside a `@tf.function` with
129AutoGraph turned off. It can be caused by using a `tf.Tensor` value as:
130
131  * the iterated of a `for` statement: `for i in tensor:`
132  * the argument to the `iter` built-in: `iter(tensor)`
133
134Note: These operations are allowed when executing eagerly.
135
136This exception is similar to the previous example, and has similar causes and
137remedies.
138
139### "InaccessibleTensorError: The tensor `<name>` is defined in another function or code block"
140
141This exception is common to code which attempts to obtain values calculated
142within a `tf.cond`, `tf.while_loop`, or another `@tf.function` without using
143functional style or through mutable collections. See
144[Capturing External Symbolic Tensors](https://www.tensorflow.org/guide/function#all_outputs_of_a_tffunction_must_be_return_values)
145and [Limitations](limitations.md) for more details.
146
147### "StagingError: in converted code"
148
149This exception is used by AutoGraph to wrap exceptions with custom constructors
150that it cannot re-raise with the original type. See
151[Error handling](error_handling.md) for more details. If your code uses custom
152exceptions, expect them to be wrapped by this exception.
153
154### "Unable to identify source code of lambda function"
155
156This error usually appears in the context of a conversion warning. It indicates
157that a lambda function could not be parsed (see [Limitations](limitations.md)).
158
159This type of errors can usually be avoided by creating lambda functions in
160separate simple assignments, for example:
161
162```
163l = lambda <args>: <body>
164```
165