# WARNING:tensorflow:5 out of the last 5 calls to <function train_step> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has reduce_retracing=True option that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.

- **ID:** `tensorflow/graph-execution-function-retracing`
- **Domain:** tensorflow
- **Category:** runtime_error
- **Error Code:** `RET`
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

Repeated retracing of a tf.function due to dynamic inputs like varying tensor shapes or Python arguments, degrading performance.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| tensorflow 2.15 | active | — | — |
| tensorflow 2.16 | active | — | — |
| tensorflow 2.17 | active | — | — |

## Workarounds

1. **Define `@tf.function` outside the training loop and ensure inputs are tensors with consistent shapes. For example: `@tf.function(reduce_retracing=True) def train_step(x, y): ...`. Then call `train_step(batch_x, batch_y)` inside the loop.** (85% success)
   ```
   Define `@tf.function` outside the training loop and ensure inputs are tensors with consistent shapes. For example: `@tf.function(reduce_retracing=True) def train_step(x, y): ...`. Then call `train_step(batch_x, batch_y)` inside the loop.
   ```
2. **If shapes vary, pad inputs to a fixed shape or use `tf.ensure_shape` to constrain tensor shapes before passing to the function.** (75% success)
   ```
   If shapes vary, pad inputs to a fixed shape or use `tf.ensure_shape` to constrain tensor shapes before passing to the function.
   ```

## Dead Ends

- **** — Adding `@tf.function` inside a loop repeatedly creates new functions, but users often move it outside incorrectly without fixing the input types. (60% fail)
- **** — Setting `experimental_compile=True` may suppress the warning but cause compilation errors if shapes are dynamic. (70% fail)
