{
  "id": "huggingface/quantization-bnb-4bit-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` but the model weights are loaded in `torch\\.float32`\\.",
  "domain": "huggingface",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "When using 4-bit quantization via bitsandbytes, the compute dtype for matrix multiplications (bnb_4bit_compute_dtype) must match the dtype of the model weights. A mismatch between float16 compute and float32 weights can cause incorrect gradients or NaN losses.",
  "root_cause_type": "generic",
  "root_cause_zh": "通过 bitsandbytes 使用 4 位量化时，矩阵乘法的计算数据类型（bnb_4bit_compute_dtype）必须与模型权重的数据类型匹配。float16 计算与 float32 权重之间的不匹配可能导致梯度错误或 NaN 损失。",
  "versions": [
    {
      "version": "bitsandbytes>=0.41.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.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Changing `bnb_4bit_compute_dtype` to `torch.float32` without also adjusting the model's `torch_dtype` may still leave the model in float16 if it was originally loaded that way.",
      "fail_rate": 0.4,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Using `model.half()` after loading to force float16 can break the quantized modules and cause CUDA errors.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Set both `bnb_4bit_compute_dtype` and `torch_dtype` to the same value (e.g., `torch.float16`) when loading the model. Use `from_pretrained(..., torch_dtype=torch.float16, quantization_config=quant_config)`.",
      "success_rate": 0.9,
      "how": "Set both `bnb_4bit_compute_dtype` and `torch_dtype` to the same value (e.g., `torch.float16`) when loading the model. Use `from_pretrained(..., torch_dtype=torch.float16, quantization_config=quant_config)`.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Alternatively, set `bnb_4bit_compute_dtype=torch.float32` and load the model with `torch_dtype=torch.float32` (default) to avoid precision mismatch.",
      "success_rate": 0.85,
      "how": "Alternatively, set `bnb_4bit_compute_dtype=torch.float32` and load the model with `torch_dtype=torch.float32` (default) to avoid precision mismatch.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Set both `bnb_4bit_compute_dtype` and `torch_dtype` to the same value (e.g., `torch.float16`) when loading the model. Use `from_pretrained(..., torch_dtype=torch.float16, quantization_config=quant_config)`.",
    "Alternatively, set `bnb_4bit_compute_dtype=torch.float32` and load the model with `torch_dtype=torch.float32` (default) to avoid precision mismatch."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/transformers/main_classes/quantization#4-bit-quantization",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-12-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}