{
  "id": "huggingface/transformers-tokenizer-padding-side-mismatch",
  "signature": "UserWarning: You are using a decoder-only model with padding_side='right'. This may produce incorrect results. Consider setting `tokenizer.padding_side = 'left'`.",
  "signature_zh": "UserWarning: 您正在对解码器专用模型使用 padding_side='right'。这可能会产生不正确的结果。建议设置 `tokenizer.padding_side = 'left'`。",
  "regex": "You are using a decoder-only model with padding_side='right'",
  "domain": "huggingface",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "Decoder-only models (e.g., GPT, LLaMA) expect padding on the left side for batched generation; using right padding causes the model to attend to padding tokens at the end of the sequence.",
  "root_cause_type": "generic",
  "root_cause_zh": "解码器专用模型（如 GPT、LLaMA）在批量生成时期望左侧填充；使用右侧填充会导致模型关注序列末尾的填充标记。",
  "versions": [
    {
      "version": "transformers>=4.30.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torch>=1.12.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "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.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "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.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "This only sets the pad token ID; it does not change the padding direction, so the warning and incorrect behavior persist.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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')`",
      "success_rate": 0.95,
      "how": "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')`",
      "condition": "",
      "sources": []
    },
    {
      "action": "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.",
      "success_rate": 0.9,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "If using Trainer, set `tokenizer.padding_side = 'left'` in the data collator or before creating the dataset to ensure all batches are left-padded.",
      "success_rate": 0.85,
      "how": "If using Trainer, set `tokenizer.padding_side = 'left'` in the data collator or before creating the dataset to ensure all batches are left-padded.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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')`",
    "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.",
    "If using Trainer, set `tokenizer.padding_side = 'left'` in the data collator or before creating the dataset to ensure all batches are left-padded."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/transformers/en/pad_truncation#padding-and-truncation",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.95,
  "resolvable": "true",
  "first_seen": "2023-04-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}