huggingface config_error ai_generated true

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.

ID: huggingface/quantization-config-dtype-mismatch

Also available as: JSON · Markdown · 中文
88%Fix Rate
86%Confidence
1Evidence
2024-04-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
transformers>=4.30.0 active
bitsandbytes>=0.41.0 active

Root Cause

Mismatch between the compute dtype specified in quantization config and the actual model weight dtype leads to potential numerical issues.

generic

中文

量化配置中指定的计算数据类型与实际模型权重数据类型不匹配,可能导致数值问题。

Official Documentation

https://huggingface.co/docs/transformers/main/en/quantization/bitsandbytes

Workarounds

  1. 95% success Set `bnb_4bit_compute_dtype` to match the model weight dtype: `quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype)`
    Set `bnb_4bit_compute_dtype` to match the model weight dtype: `quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype)`
  2. 90% success Load the model with the correct torch_dtype: `model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=quantization_config)`
    Load the model with the correct torch_dtype: `model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=quantization_config)`

中文步骤

  1. Set `bnb_4bit_compute_dtype` to match the model weight dtype: `quantization_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=model.dtype)`
  2. Load the model with the correct torch_dtype: `model = AutoModelForCausalLM.from_pretrained('model-name', torch_dtype=torch.float16, quantization_config=quantization_config)`

Dead Ends

Common approaches that don't work:

  1. Setting `bnb_4bit_compute_dtype` to `torch.float32` without changing model weights 70% fail

    This only silences the warning but does not fix the underlying dtype mismatch if the model is in float16.

  2. Ignoring the warning and proceeding with training 60% fail

    Numerical instability can lead to NaN loss or diverging gradients, especially in mixed-precision training.