{
  "id": "huggingface/peft-adapter-torch-dtype-mismatch",
  "signature": "RuntimeError: The base model dtype is torch.float32 but the LoRA adapter was trained with torch.bfloat16. This may cause numerical instability.",
  "signature_zh": "运行时错误：基础模型数据类型为 torch.float32，但 LoRA 适配器使用 torch.bfloat16 训练。这可能导致数值不稳定。",
  "regex": "RuntimeError: The base model dtype is torch\\.\\w+ but the LoRA adapter was trained with torch\\.\\w+\\. This may cause numerical instability\\.",
  "domain": "huggingface",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "PEFT adapter weights are loaded onto a base model with a different torch dtype, causing a dtype mismatch that can lead to silent accuracy degradation or NaN outputs.",
  "root_cause_type": "generic",
  "root_cause_zh": "PEFT 适配器权重加载到具有不同 torch 数据类型的基础模型上，导致数据类型不匹配，可能引发精度下降或 NaN 输出。",
  "versions": [
    {
      "version": "peft>=0.7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "transformers>=4.36.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torch>=2.1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Forcing model.to(torch.bfloat16) after loading the adapter does not change the adapter's internal dtype and may cause a separate device mismatch error.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Setting torch_dtype='auto' in from_pretrained may load the base model in float16, which still mismatches if the adapter expects bfloat16.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Load the base model with the same dtype as the adapter (bfloat16 in this case):\nfrom transformers import AutoModel\nimport torch\nmodel = AutoModel.from_pretrained('base-model', torch_dtype=torch.bfloat16)\nmodel.load_adapter('adapter-path')\n# Verify dtype\nprint(model.dtype)  # Should be torch.bfloat16",
      "success_rate": 0.85,
      "how": "Load the base model with the same dtype as the adapter (bfloat16 in this case):\nfrom transformers import AutoModel\nimport torch\nmodel = AutoModel.from_pretrained('base-model', torch_dtype=torch.bfloat16)\nmodel.load_adapter('adapter-path')\n# Verify dtype\nprint(model.dtype)  # Should be torch.bfloat16",
      "condition": "",
      "sources": []
    },
    {
      "action": "Convert the adapter weights to the base model's dtype after loading using PeftModel.from_pretrained with dtype argument:\nfrom peft import PeftModel\nbase_model = AutoModel.from_pretrained('base-model', torch_dtype=torch.float32)\npeft_model = PeftModel.from_pretrained(base_model, 'adapter-path', torch_dtype=torch.float32)",
      "success_rate": 0.8,
      "how": "Convert the adapter weights to the base model's dtype after loading using PeftModel.from_pretrained with dtype argument:\nfrom peft import PeftModel\nbase_model = AutoModel.from_pretrained('base-model', torch_dtype=torch.float32)\npeft_model = PeftModel.from_pretrained(base_model, 'adapter-path', torch_dtype=torch.float32)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Load the base model with the same dtype as the adapter (bfloat16 in this case):\nfrom transformers import AutoModel\nimport torch\nmodel = AutoModel.from_pretrained('base-model', torch_dtype=torch.bfloat16)\nmodel.load_adapter('adapter-path')\n# Verify dtype\nprint(model.dtype)  # Should be torch.bfloat16",
    "Convert the adapter weights to the base model's dtype after loading using PeftModel.from_pretrained with dtype argument:\nfrom peft import PeftModel\nbase_model = AutoModel.from_pretrained('base-model', torch_dtype=torch.float32)\npeft_model = PeftModel.from_pretrained(base_model, 'adapter-path', torch_dtype=torch.float32)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/peft/en/developer_guides/quantization",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2024-01-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}