# UserWarning: 您正在对解码器专用模型使用 padding_side='right'。这可能会产生不正确的结果。建议设置 `tokenizer.padding_side = 'left'`。

- **ID:** `huggingface/transformers-tokenizer-padding-side-mismatch`
- **领域:** huggingface
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

解码器专用模型（如 GPT、LLaMA）在批量生成时期望左侧填充；使用右侧填充会导致模型关注序列末尾的填充标记。

## 版本兼容性

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

## 解决方案

1. ```
   Set padding_side before tokenizing: `tokenizer.padding_side = 'left'` then tokenize the batch again. Example: `tokenizer.padding_side = 'left'; inputs = tokenizer(texts, padding=True, truncation=True, return_tensors='pt')`
   ```
2. ```
   For generation, use a pipeline with `tokenizer.padding_side = 'left'` and set `pad_token_id=tokenizer.eos_token_id` if no pad token is defined.
   ```
3. ```
   If using Trainer, set `tokenizer.padding_side = 'left'` in the data collator or before creating the dataset to ensure all batches are left-padded.
   ```

## 无效尝试

- **** — The model will generate incorrect tokens because it attends to padding tokens at the end, especially for left-to-right generation tasks like text completion. (80% 失败率)
- **** — The tokenizer's padding_side only affects future calls to tokenizer(); if you already tokenized the input, the padding direction is already set and won't change. (60% 失败率)
- **** — This only sets the pad token ID; it does not change the padding direction, so the warning and incorrect behavior persist. (50% 失败率)
