{
  "id": "huggingface/quantization-config-dtype-mismatch",
  "signature": "ValueError: You passed `quantization_config` with `bnb_4bit_compute_dtype=torch.float16` but the model weights are loaded in torch.float32. This may cause numerical instability.",
  "signature_zh": "ValueError: 您传递的 `quantization_config` 中 `bnb_4bit_compute_dtype=torch.float16`，但模型权重以 torch.float32 加载。这可能导致数值不稳定。",
  "regex": "ValueError: You passed `quantization_config` with `bnb_4bit_compute_dtype=torch\\.(float16|bfloat16)` but the model weights are loaded in torch\\.(float32|float16)\\. This may cause numerical instability\\.",
  "domain": "huggingface",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "Mismatch between the compute dtype specified in quantization config and the actual model weight dtype leads to potential numerical issues.",
  "root_cause_type": "generic",
  "root_cause_zh": "量化配置中指定的计算数据类型与实际模型权重数据类型不匹配，可能导致数值问题。",
  "versions": [
    {
      "version": "transformers>=4.30.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "bitsandbytes>=0.41.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Setting `bnb_4bit_compute_dtype` to `torch.float32` without changing model weights",
      "why_fails": "This only silences the warning but does not fix the underlying dtype mismatch if the model is in float16.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring the warning and proceeding with training",
      "why_fails": "Numerical instability can lead to NaN loss or diverging gradients, especially in mixed-precision training.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Set `bnb_4bit_compute_dtype` to match the model weight dtype: `quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype)`",
      "success_rate": 0.95,
      "how": "Set `bnb_4bit_compute_dtype` to match the model weight dtype: `quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype)`",
      "condition": "",
      "sources": []
    },
    {
      "action": "Load the model with the correct torch_dtype: `model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=quantization_config)`",
      "success_rate": 0.9,
      "how": "Load the model with the correct torch_dtype: `model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=quantization_config)`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Set `bnb_4bit_compute_dtype` to match the model weight dtype: `quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype)`",
    "Load the model with the correct torch_dtype: `model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=quantization_config)`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/transformers/main/en/quantization/bitsandbytes",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2024-04-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}