{
  "id": "pytorch/torchscript-recursive-trace",
  "signature": "RuntimeError: Tracing failed: Recursive trace of function 'forward' is not allowed",
  "signature_zh": "运行时错误：跟踪失败：不允许递归跟踪函数 'forward'",
  "regex": "Tracing failed: Recursive trace of function 'forward' is not allowed",
  "domain": "pytorch",
  "category": "module_error",
  "subcategory": null,
  "root_cause": "TorchScript tracing detected a recursive call to the traced function, which is not supported in graph-based tracing; the model uses dynamic control flow or self-referential layers.",
  "root_cause_type": "generic",
  "root_cause_zh": "TorchScript 跟踪检测到对已跟踪函数的递归调用，这在基于图的跟踪中不受支持；模型使用了动态控制流或自引用层。",
  "versions": [
    {
      "version": "pytorch>=2.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Suppressing the error does not fix the underlying issue; the traced model may produce incorrect results.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Tracing unrolls loops but still cannot handle recursive calls within the traced function.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Script may fail with the same recursion error if the model is not scriptable; it requires explicit type annotations.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Refactor the model to avoid recursion: replace recursive calls with iterative loops using `torch.jit.script` and explicit type annotations. Example: change a recursive RNN to use `torch.nn.RNN` or a loop over time steps.",
      "success_rate": 0.9,
      "how": "Refactor the model to avoid recursion: replace recursive calls with iterative loops using `torch.jit.script` and explicit type annotations. Example: change a recursive RNN to use `torch.nn.RNN` or a loop over time steps.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `torch.jit.script` with `@torch.jit.script` decorator and annotate all tensor types. If recursion is unavoidable, use `torch.jit.ignore` on the recursive helper function.",
      "success_rate": 0.8,
      "how": "Use `torch.jit.script` with `@torch.jit.script` decorator and annotate all tensor types. If recursion is unavoidable, use `torch.jit.ignore` on the recursive helper function.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Export the model using ONNX instead of TorchScript, as ONNX may handle certain recursive patterns via explicit unrolling.",
      "success_rate": 0.75,
      "how": "Export the model using ONNX instead of TorchScript, as ONNX may handle certain recursive patterns via explicit unrolling.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "重构模型以避免递归：使用 `torch.jit.script` 和显式类型注释，用迭代循环替换递归调用。例如：将递归 RNN 改为使用 `torch.nn.RNN` 或时间步循环。",
    "使用带有 `@torch.jit.script` 装饰器的 `torch.jit.script` 并注释所有张量类型。如果递归不可避免，则在递归辅助函数上使用 `torch.jit.ignore`。",
    "使用 ONNX 而不是 TorchScript 导出模型，因为 ONNX 可能通过显式展开处理某些递归模式。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/jit.html#tracing",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-11-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}