huggingface config_error ai_generated true

ValueError:您传递了 `bnb_4bit_compute_dtype=torch.float16` 的 `quantization_config`,但模型权重以 torch.float32 加载。这可能导致意外行为。

ValueError: You passed `quantization_config` with `bnb_4bit_compute_dtype=torch.float16` but the model weights are loaded in torch.float32. This may cause unexpected behavior.

ID: huggingface/quantization-config-bnb-compute-dtype-mismatch

其他格式: JSON · Markdown 中文 · English
85%修复率
83%置信度
1证据数
2024-03-12首次发现

版本兼容性

版本状态引入弃用备注
transformers>=4.36.0 active
bitsandbytes>=0.41.0 active
torch>=2.0.0 active

根因分析

量化配置中指定的计算 dtype 与实际加载的模型权重的 dtype 不匹配,导致精度不一致。

English

Mismatch between the compute dtype specified in quantization config and the actual dtype of the loaded model weights, causing precision inconsistencies.

generic

官方文档

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

解决方案

  1. 在 from_pretrained 中设置 torch_dtype='auto' 以匹配量化配置:model = AutoModel.from_pretrained('model', quantization_config=quant_config, torch_dtype='auto')
  2. 明确设置 bnb_4bit_compute_dtype 以匹配模型的 dtype:quant_config = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.float32)

无效尝试

常见但无效的做法:

  1. 70% 失败

    If quantization config specifies float16 but model weights are float32, the error persists.

  2. 90% 失败

    The mismatch causes incorrect computations, especially in mixed-precision training.