{
  "id": "pytorch/jit-trace-dynamic-control-flow",
  "signature": "RuntimeError: TracerWarning: Using a tensor as a Python bool might cause the trace to be incorrect. We can't record the control flow of this loop.",
  "signature_zh": "运行时错误：跟踪器警告：将张量用作Python布尔值可能导致跟踪不正确。我们无法记录此循环的控制流。",
  "regex": "RuntimeError: TracerWarning: Using a tensor as a Python bool might cause the trace to be incorrect\\. We can't record the control flow of this loop\\.",
  "domain": "pytorch",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "TorchScript tracing encountered a data-dependent control flow (e.g., if tensor > 0), which cannot be captured statically; tracing converts it to a constant.",
  "root_cause_type": "generic",
  "root_cause_zh": "TorchScript跟踪遇到了数据相关的控制流（例如，if tensor > 0），无法静态捕获；跟踪将其转换为常量。",
  "versions": [
    {
      "version": "torch>=1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torch>=1.8 for improved warnings",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "@torch.jit.script",
      "why_fails": "Using @torch.jit.script instead of tracing may still fail if the function uses dynamic shapes; scripting requires full type annotations.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "torch.jit._state.disable_tracing()",
      "why_fails": "Suppressing the warning with torch.jit._state.disable_tracing() hides the issue but the exported model will produce wrong results for different inputs.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Replace dynamic control flow with torch.where or masked operations: instead of 'if x > 0: y = a else: y = b', use 'y = torch.where(x > 0, a, b)'. This keeps the graph static.",
      "success_rate": 0.85,
      "how": "Replace dynamic control flow with torch.where or masked operations: instead of 'if x > 0: y = a else: y = b', use 'y = torch.where(x > 0, a, b)'. This keeps the graph static.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use torch.jit.script with type annotations: @torch.jit.script; def forward(self, x: torch.Tensor) -> torch.Tensor: ... This supports dynamic control flow.",
      "success_rate": 0.8,
      "how": "Use torch.jit.script with type annotations: @torch.jit.script; def forward(self, x: torch.Tensor) -> torch.Tensor: ... This supports dynamic control flow.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "用torch.where或掩码操作替换动态控制流：不用'if x > 0: y = a else: y = b'，而用'y = torch.where(x > 0, a, b)'。这保持图静态。",
    "使用带类型注解的torch.jit.script：@torch.jit.script; def forward(self, x: torch.Tensor) -> torch.Tensor: ... 这支持动态控制流。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/jit.html#tracing-vs-scripting",
  "official_doc_section": null,
  "error_code": "TORCH_SCRIPT_TRACE_DYNAMIC_CF",
  "verification_tier": "ai_generated",
  "confidence": 0.83,
  "fix_success_rate": 0.75,
  "resolvable": "partial",
  "first_seen": "2023-05-01",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}