# 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.

- **ID:** `huggingface/quantization-dtype-mismatch`
- **Domain:** huggingface
- **Category:** config_error
- **Verification:** ai_generated
- **Fix Rate:** 88%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| transformers>=4.32.0 | active | — | — |
| bitsandbytes>=0.41.0 | active | — | — |
| accelerate>=0.24.0 | active | — | — |

## Workarounds

1. **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).** (90% success)
   ```
   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).
   ```
2. **Load model with torch_dtype matching the compute dtype: model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=bnb_config).** (85% success)
   ```
   Load model with torch_dtype matching the compute dtype: model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=bnb_config).
   ```

## Dead Ends

- **Setting bnb_4bit_compute_dtype to torch.float32 without changing model weights** — 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. (50% fail)
- **Ignoring the error and continuing with default dtype** — The error is raised as a ValueError; the code will stop execution unless caught, so ignoring is not possible. (100% fail)
