{
  "id": "tensorflow/graph-execution-function-retracing",
  "signature": "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.",
  "signature_zh": "WARNING:tensorflow：最近 5 次调用 <function train_step> 中的 5 次触发了 tf.function 重跟踪。跟踪开销很大，过多的跟踪可能是由于：(1) 在循环中重复创建 @tf.function，(2) 传递不同形状的张量，(3) 传递 Python 对象而非张量。对于 (1)，请将 @tf.function 定义在循环外部。对于 (2)，@tf.function 具有 reduce_retracing=True 选项，可以避免不必要的重跟踪。对于 (3)，请参阅 https://www.tensorflow.org/guide/function#controlling_retracing 和 https://www.tensorflow.org/api_docs/python/tf/function 了解更多详情。",
  "regex": "5 out of the last 5 calls to .* triggered tf\\.function retracing",
  "domain": "tensorflow",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Repeated retracing of a tf.function due to dynamic inputs like varying tensor shapes or Python arguments, degrading performance.",
  "root_cause_type": "generic",
  "root_cause_zh": "",
  "versions": [
    {
      "version": "tensorflow 2.15",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tensorflow 2.16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tensorflow 2.17",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Adding `@tf.function` inside a loop repeatedly creates new functions, but users often move it outside incorrectly without fixing the input types.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Setting `experimental_compile=True` may suppress the warning but cause compilation errors if shapes are dynamic.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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.",
      "success_rate": 0.85,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If shapes vary, pad inputs to a fixed shape or use `tf.ensure_shape` to constrain tensor shapes before passing to the function.",
      "success_rate": 0.75,
      "how": "If shapes vary, pad inputs to a fixed shape or use `tf.ensure_shape` to constrain tensor shapes before passing to the function.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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.",
    "If shapes vary, pad inputs to a fixed shape or use `tf.ensure_shape` to constrain tensor shapes before passing to the function."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.tensorflow.org/guide/function#controlling_retracing",
  "official_doc_section": null,
  "error_code": "RET",
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2024-03-01",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}