{
  "id": "pytorch/torchscript-unsupported-dynamic-control-flow",
  "signature": "RuntimeError: TorchScript supports tracing only for tensors. This function cannot be traced because it uses dynamic control flow (if/else or loops) that depends on tensor data.",
  "signature_zh": "运行时错误：TorchScript 仅支持对张量进行追踪。此函数无法追踪，因为它使用了依赖于张量数据的动态控制流（if/else 或循环）。",
  "regex": "RuntimeError: TorchScript supports tracing only for tensors. This function cannot be traced because it uses dynamic control flow",
  "domain": "pytorch",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "The model contains control flow (e.g., if x.sum() > 0) that depends on tensor values, which is not supported by torch.jit.trace; torch.jit.script is required instead.",
  "root_cause_type": "generic",
  "root_cause_zh": "模型包含依赖于张量值的控制流（如 if x.sum() > 0），torch.jit.trace 不支持；需要使用 torch.jit.script。",
  "versions": [
    {
      "version": "pytorch>=1.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Tracing captures the operations executed for given inputs; it cannot handle branches not taken.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Ignoring the function means it won't be compiled, leading to missing operations in the exported graph.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Removing control flow changes model logic; it may work but is error-prone and not always feasible.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Replace torch.jit.trace with torch.jit.script for the entire model or the specific module with control flow. Script supports dynamic control flow.",
      "success_rate": 0.95,
      "how": "Replace torch.jit.trace with torch.jit.script for the entire model or the specific module with control flow. Script supports dynamic control flow.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If you must use tracing, refactor the control flow into a separate function and use torch.jit.script for that function, then call it from the traced module.",
      "success_rate": 0.85,
      "how": "If you must use tracing, refactor the control flow into a separate function and use torch.jit.script for that function, then call it from the traced module.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "将 torch.jit.trace 替换为 torch.jit.script 用于整个模型或包含控制流的特定模块。Script 支持动态控制流。",
    "如果必须使用 tracing，将控制流重构到单独的函数中，并使用 torch.jit.script 处理该函数，然后从 traced 模块中调用它。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/jit.html#mixing-tracing-and-scripting",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2024-04-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}