# ValueError: 目标模块 ['q_proj', 'v_proj'] 在基础模型中未找到。可用模块为: ['query', 'value', 'key', 'output']

- **ID:** `huggingface/peft-lora-target-modules-mismatch`
- **领域:** huggingface
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

LoRA 的 target_modules 参数指定了错误的模块名称，与基础模型实际的层命名约定不匹配。

## 版本兼容性

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

## 解决方案

1. ```
   Print the model's module names to identify correct target modules: `for name, _ in model.named_modules(): print(name)` then select only linear layers (e.g., 'q_proj', 'v_proj' for LLaMA, 'query', 'value' for BERT).
   ```
2. ```
   Use peft.get_peft_model() with target_modules='all-linear' if using peft>=0.7.0 and the model is transformer-based, which automatically selects all linear layers except the output projection.
   ```
3. ```
   Check the model configuration via model.config.model_type and refer to the PEFT documentation for the correct module names for that architecture.
   ```

## 无效尝试

- **** — The model may not have those exact names; e.g., LLaMA uses 'self_attn.q_proj' while GPT-2 uses 'attn.c_attn'. This can still fail if names are wrong. (50% 失败率)
- **** — Includes non-linear layers (e.g., activation functions) which are invalid for LoRA and cause shape errors during forward pass. (30% 失败率)
- **** — 'all-linear' was introduced in peft>=0.7.0; older versions raise AttributeError or silently ignore it. (40% 失败率)
