{
  "id": "pytorch/amp-gradscaler-dtype",
  "signature": "RuntimeError: Expected dtype float16 for half precision but got float32",
  "signature_zh": "运行时错误：半精度期望 float16 类型，但得到 float32",
  "regex": "RuntimeError: Expected dtype float16 for half precision but got float32",
  "domain": "pytorch",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A tensor with incorrect dtype (float32 instead of float16) was passed to an operation that expects half-precision input, such as within torch.cuda.amp.autocast with GradScaler.",
  "root_cause_type": "generic",
  "root_cause_zh": "将类型不正确的张量（float32 而非 float16）传递给了期望半精度输入的操作，例如在 torch.cuda.amp.autocast 和 GradScaler 中使用时。",
  "versions": [
    {
      "version": "torch>=2.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "CUDA>=11.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Disabling autocast entirely removes the error but defeats the purpose of mixed precision training.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Manually converting all tensors to float16 can cause numerical instability and does not fix the root cause of incorrect dtype propagation.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Explicitly cast inputs to half precision before the operation, or ensure the model uses half precision layers correctly with GradScaler.",
      "success_rate": 0.85,
      "how": "Explicitly cast inputs to half precision before the operation, or ensure the model uses half precision layers correctly with GradScaler.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Check for custom operations that do not support autocast and manually cast inputs to float16.",
      "success_rate": 0.75,
      "how": "Check for custom operations that do not support autocast and manually cast inputs to float16.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Explicitly cast inputs to half precision before the operation, or ensure the model uses half precision layers correctly with GradScaler.",
    "Check for custom operations that do not support autocast and manually cast inputs to float16."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/amp.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.78,
  "resolvable": "true",
  "first_seen": "2023-07-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}