{
  "id": "huggingface/text-generation-pipeline-max-new-tokens-exceeded",
  "signature": "ValueError: The sum of `max_new_tokens` (1000) and input length (512) exceeds the model's maximum position embeddings (2048). Reduce `max_new_tokens` or truncate input.",
  "signature_zh": "值错误：`max_new_tokens` (1000) 与输入长度 (512) 之和超过了模型的最大位置嵌入 (2048)。请减少 `max_new_tokens` 或截断输入。",
  "regex": "ValueError: The sum of `max_new_tokens` \\(\\d+\\) and input length \\(\\d+\\) exceeds the model's maximum position embeddings \\(\\d+\\).",
  "domain": "huggingface",
  "category": "config_error",
  "subcategory": null,
  "root_cause": "The total sequence length (input tokens + new tokens to generate) exceeds the model's maximum position embedding size, which is a hard limit for positional encodings.",
  "root_cause_type": "generic",
  "root_cause_zh": "总序列长度（输入标记数 + 要生成的新标记数）超过了模型的最大位置嵌入大小，这是位置编码的硬性限制。",
  "versions": [
    {
      "version": "transformers>=4.38.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torch>=2.0.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "Python>=3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Setting max_new_tokens to a very large value (e.g., 10000) expecting the model to handle it, which causes the same error.",
      "fail_rate": 1.0,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Using max_length instead of max_new_tokens may truncate input silently and produce incomplete outputs.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Reduce max_new_tokens so that input_length + max_new_tokens <= model_max_length:\nfrom transformers import pipeline\ngenerator = pipeline('text-generation', model='gpt2')\nmodel_max_length = generator.model.config.max_position_embeddings  # 1024 for gpt2\ninput_length = len(generator.tokenizer(prompt)['input_ids'])\nmax_new_tokens = min(500, model_max_length - input_length - 10)  # safety margin\nresult = generator(prompt, max_new_tokens=max_new_tokens)",
      "success_rate": 0.93,
      "how": "Reduce max_new_tokens so that input_length + max_new_tokens <= model_max_length:\nfrom transformers import pipeline\ngenerator = pipeline('text-generation', model='gpt2')\nmodel_max_length = generator.model.config.max_position_embeddings  # 1024 for gpt2\ninput_length = len(generator.tokenizer(prompt)['input_ids'])\nmax_new_tokens = min(500, model_max_length - input_length - 10)  # safety margin\nresult = generator(prompt, max_new_tokens=max_new_tokens)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Truncate the input to allow more generation tokens:\ntruncated_prompt = generator.tokenizer.decode(generator.tokenizer(prompt, truncation=True, max_length=512)['input_ids'])\nresult = generator(truncated_prompt, max_new_tokens=1000)",
      "success_rate": 0.85,
      "how": "Truncate the input to allow more generation tokens:\ntruncated_prompt = generator.tokenizer.decode(generator.tokenizer(prompt, truncation=True, max_length=512)['input_ids'])\nresult = generator(truncated_prompt, max_new_tokens=1000)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Reduce max_new_tokens so that input_length + max_new_tokens <= model_max_length:\nfrom transformers import pipeline\ngenerator = pipeline('text-generation', model='gpt2')\nmodel_max_length = generator.model.config.max_position_embeddings  # 1024 for gpt2\ninput_length = len(generator.tokenizer(prompt)['input_ids'])\nmax_new_tokens = min(500, model_max_length - input_length - 10)  # safety margin\nresult = generator(prompt, max_new_tokens=max_new_tokens)",
    "Truncate the input to allow more generation tokens:\ntruncated_prompt = generator.tokenizer.decode(generator.tokenizer(prompt, truncation=True, max_length=512)['input_ids'])\nresult = generator(truncated_prompt, max_new_tokens=1000)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://huggingface.co/docs/transformers/en/main_classes/text_generation#transformers.GenerationConfig.max_new_tokens",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.89,
  "fix_success_rate": 0.93,
  "resolvable": "true",
  "first_seen": "2023-10-01",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}