{
  "id": "llm/huggingface-tokenizer-padding-side-mismatch",
  "signature": "ValueError: You have to provide either 'max_length' or 'padding' and 'truncation' to use padding side 'left' with batch size > 1",
  "signature_zh": "ValueError: 当批次大小大于1且使用左侧填充时，必须提供'max_length'或'padding'和'truncation'参数",
  "regex": "ValueError: You have to provide either 'max_length' or 'padding' and 'truncation' to use padding side 'left' with batch size > 1",
  "domain": "llm",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "Hugging Face tokenizer requires explicit padding and truncation configuration when using left padding (common for decoder-only LLMs) in batched inference, otherwise it cannot determine the sequence length alignment.",
  "root_cause_type": "generic",
  "root_cause_zh": "Hugging Face分词器在使用左侧填充（解码器专用LLM常见设置）进行批量推理时，需要显式配置填充和截断参数，否则无法确定序列长度对齐方式。",
  "versions": [
    {
      "version": "transformers 4.30.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "transformers 4.31.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "transformers 4.35.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Python 3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Python 3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Python 3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Decoder-only models (e.g., LLaMA, GPT) expect left padding for causal attention masking; right padding causes misalignment and incorrect generation.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Bypasses the error but prevents batching, leading to poor throughput and increased latency in production.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Configure tokenizer with explicit padding=True, truncation=True, and max_length=model_max_length before tokenizing batched inputs:\n\ntokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-hf')\ntokenizer.pad_token = tokenizer.eos_token\ntokenizer.padding_side = 'left'\nencoded = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors='pt')",
      "success_rate": 0.95,
      "how": "Configure tokenizer with explicit padding=True, truncation=True, and max_length=model_max_length before tokenizing batched inputs:\n\ntokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-hf')\ntokenizer.pad_token = tokenizer.eos_token\ntokenizer.padding_side = 'left'\nencoded = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors='pt')",
      "condition": "",
      "sources": []
    },
    {
      "action": "Set tokenizer.pad_token_id explicitly and use DataCollatorWithPadding from transformers for dynamic batching:\n\nfrom transformers import DataCollatorWithPadding\ndata_collator = DataCollatorWithPadding(tokenizer, padding='max_length', max_length=512)",
      "success_rate": 0.9,
      "how": "Set tokenizer.pad_token_id explicitly and use DataCollatorWithPadding from transformers for dynamic batching:\n\nfrom transformers import DataCollatorWithPadding\ndata_collator = DataCollatorWithPadding(tokenizer, padding='max_length', max_length=512)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在分词前显式配置padding=True, truncation=True和max_length=model_max_length：\n\ntokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-hf')\ntokenizer.pad_token = tokenizer.eos_token\ntokenizer.padding_side = 'left'\nencoded = tokenizer(texts, padding=True, truncation=True, max_length=512, return_tensors='pt')",
    "显式设置tokenizer.pad_token_id，并使用transformers的DataCollatorWithPadding进行动态批处理：\n\nfrom transformers import DataCollatorWithPadding\ndata_collator = DataCollatorWithPadding(tokenizer, padding='max_length', max_length=512)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/transformers/main/en/main_classes/tokenizer#transformers.PreTrainedTokenizer.__call__.padding",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-08-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}