{
  "id": "huggingface/quantization-dtype-mismatch",
  "signature": "ValueError: You passed `quantization_config` with `bnb_4bit_compute_dtype=torch.float16` but the model weights are loaded in torch.float32. Set `bnb_4bit_compute_dtype` to match the model weights or convert the model.",
  "signature_zh": "ValueError：您传递了`bnb_4bit_compute_dtype=torch.float16`的`quantization_config`，但模型权重以torch.float32加载。将`bnb_4bit_compute_dtype`设置为与模型权重匹配或转换模型。",
  "regex": "ValueError: You passed `quantization_config` with `bnb_4bit_compute_dtype=torch\\.\\w+` but the model weights are loaded in torch\\.\\w+",
  "domain": "huggingface",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "When using bitsandbytes 4-bit quantization, the compute dtype specified in the quantization config does not match the model's weight dtype, causing a type mismatch in mixed-precision operations.",
  "root_cause_type": "generic",
  "root_cause_zh": "使用bitsandbytes 4位量化时，量化配置中指定的计算数据类型与模型权重数据类型不匹配，导致混合精度操作中的类型不匹配。",
  "versions": [
    {
      "version": "transformers>=4.32.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"
    },
    {
      "version": "accelerate>=0.24.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": "If the model was loaded with torch_dtype=torch.float16, the compute dtype float32 may cause performance degradation or OOM, but the error might be suppressed at the cost of incorrect computation.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "Ignoring the error and continuing with default dtype",
      "why_fails": "The error is raised as a ValueError; the code will stop execution unless caught, so ignoring is not possible.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Set quantization config to match model weights: from transformers import BitsAndBytesConfig; bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype) and then model = AutoModelForCausalLM.from_pretrained('model-name', quantization_config=bnb_config).",
      "success_rate": 0.9,
      "how": "Set quantization config to match model weights: from transformers import BitsAndBytesConfig; bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype) and then model = AutoModelForCausalLM.from_pretrained('model-name', quantization_config=bnb_config).",
      "condition": "",
      "sources": []
    },
    {
      "action": "Load model with torch_dtype matching the compute dtype: model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=bnb_config).",
      "success_rate": 0.85,
      "how": "Load model with torch_dtype matching the compute dtype: model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=bnb_config).",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Set quantization config to match model weights: from transformers import BitsAndBytesConfig; bnb_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype) and then model = AutoModelForCausalLM.from_pretrained('model-name', quantization_config=bnb_config).",
    "Load model with torch_dtype matching the compute dtype: model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=bnb_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-08",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}