# 运行时错误：PEFT 适配器权重形状不匹配：期望 [4096, 4096] 但得到 [4096, 2048]

- **ID:** `huggingface/peft-adapter-shape-mismatch`
- **领域:** huggingface
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

PEFT 适配器是在不同隐藏维度的模型上训练的（例如较小的变体），并被加载到维度不兼容的模型上。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| peft>=0.5.0 | active | — | — |
| transformers>=4.30.0 | active | — | — |
| torch>=1.13.0 | active | — | — |

## 解决方案

1. ```
   验证适配器训练使用的基础模型：加载具有匹配隐藏大小的正确基础模型：from transformers import AutoModel; model = AutoModel.from_pretrained('original-base-model'); model.load_adapter('./adapter_path')
   ```
2. ```
   检查适配器配置元数据：print(PeftConfig.from_pretrained('./adapter_path').base_model_name_or_path) 以识别正确的基础模型。
   ```

## 无效尝试

- **Force load the adapter with `strict=False` to ignore mismatched layers** — The model will silently drop or partially load weights, leading to undefined behavior and poor performance. (90% 失败率)
- **Manually resize the adapter weights using interpolation** — Adapters are not spatially structured; interpolation can break the learned patterns and cause numerical instability. (85% 失败率)
- **Set `torch.set_default_dtype(torch.float16)` to avoid shape errors** — Dtype does not affect tensor shape; shape mismatch is a structural issue, not a precision issue. (100% 失败率)
