{
  "id": "pytorch/compile-recompilation-limit",
  "signature": "RuntimeError: torch.compile: Recompilation limit reached (100 recompilations) for function 'forward'. Consider using dynamic=True or reducing tensor shape variability.",
  "signature_zh": "运行时错误：torch.compile：函数 'forward' 的重新编译次数达到上限（100 次）。考虑使用 dynamic=True 或减少张量形状变化。",
  "regex": "RuntimeError: torch\\.compile: Recompilation limit reached \\(\\d+ recompilations\\) for function",
  "domain": "pytorch",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "torch.compile's guard mechanism triggers recompilation when tensor shapes change, and after 100 recompilations, it stops to prevent infinite loops or performance degradation.",
  "root_cause_type": "generic",
  "root_cause_zh": "torch.compile 的守卫机制在张量形状变化时触发重新编译，经过 100 次重新编译后停止，以防止无限循环或性能下降。",
  "versions": [
    {
      "version": "torch>=2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torch<=2.5.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Disabling torch.compile entirely to avoid recompilation",
      "why_fails": "This removes the performance benefits of compilation, which may be critical for large models.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting torch._dynamo.config.recompile_limit = 0 to disable the limit",
      "why_fails": "This can lead to infinite recompilation loops, causing severe performance degradation or crashes.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Use dynamic=True in torch.compile to handle dynamic shapes: torch.compile(model, dynamic=True). This allows the compiler to handle shape variations without recompiling.",
      "success_rate": 0.9,
      "how": "Use dynamic=True in torch.compile to handle dynamic shapes: torch.compile(model, dynamic=True). This allows the compiler to handle shape variations without recompiling.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Pad or resize tensors to a fixed set of shapes during training to reduce shape variability. For example, use torch.nn.utils.rnn.pad_sequence to pad sequences to a fixed length.",
      "success_rate": 0.85,
      "how": "Pad or resize tensors to a fixed set of shapes during training to reduce shape variability. For example, use torch.nn.utils.rnn.pad_sequence to pad sequences to a fixed length.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Use dynamic=True in torch.compile to handle dynamic shapes: torch.compile(model, dynamic=True). This allows the compiler to handle shape variations without recompiling.",
    "Pad or resize tensors to a fixed set of shapes during training to reduce shape variability. For example, use torch.nn.utils.rnn.pad_sequence to pad sequences to a fixed length."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/compile.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2024-04-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}