huggingface config_error ai_generated true

UserWarning: You are using a decoder-only model with padding_side='right'. This may produce incorrect results. Consider setting padding_side='left' before tokenizing.

ID: huggingface/decoder-only-padding-side-warning

Also available as: JSON · Markdown · 中文
92%Fix Rate
88%Confidence
1Evidence
2023-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
transformers>=4.30.0 active
torch>=2.0.0 active
Python>=3.8 active

Root Cause

Decoder-only models (e.g., GPT, LLaMA) expect padding on the left side to maintain causal masking; right padding causes attention to attend to padding tokens in the middle of the sequence.

generic

中文

仅解码器模型(如 GPT、LLaMA)期望在左侧进行填充以保持因果掩码;右侧填充会导致注意力机制在序列中间关注填充标记。

Official Documentation

https://huggingface.co/docs/transformers/en/pad_truncation#padding-and-truncation-for-decoder-only-models

Workarounds

  1. 92% success Set padding_side='left' and pad_token to eos_token before tokenizing: from transformers import AutoTokenizer tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf') tokenizer.padding_side = 'left' tokenizer.pad_token = tokenizer.eos_token inputs = tokenizer(texts, padding=True, return_tensors='pt')
    Set padding_side='left' and pad_token to eos_token before tokenizing:
    from transformers import AutoTokenizer
    tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf')
    tokenizer.padding_side = 'left'
    tokenizer.pad_token = tokenizer.eos_token
    inputs = tokenizer(texts, padding=True, return_tensors='pt')
  2. 85% success Use a data collator that handles left padding automatically, such as DataCollatorForSeq2Seq with padding_side='left': from transformers import DataCollatorForSeq2Seq data_collator = DataCollatorForSeq2Seq(tokenizer, padding=True, pad_to_multiple_of=8) tokenizer.padding_side = 'left'
    Use a data collator that handles left padding automatically, such as DataCollatorForSeq2Seq with padding_side='left':
    from transformers import DataCollatorForSeq2Seq
    data_collator = DataCollatorForSeq2Seq(tokenizer, padding=True, pad_to_multiple_of=8)
    tokenizer.padding_side = 'left'

中文步骤

  1. Set padding_side='left' and pad_token to eos_token before tokenizing:
    from transformers import AutoTokenizer
    tokenizer = AutoTokenizer.from_pretrained('meta-llama/Llama-2-7b-chat-hf')
    tokenizer.padding_side = 'left'
    tokenizer.pad_token = tokenizer.eos_token
    inputs = tokenizer(texts, padding=True, return_tensors='pt')
  2. Use a data collator that handles left padding automatically, such as DataCollatorForSeq2Seq with padding_side='left':
    from transformers import DataCollatorForSeq2Seq
    data_collator = DataCollatorForSeq2Seq(tokenizer, padding=True, pad_to_multiple_of=8)
    tokenizer.padding_side = 'left'

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Setting padding_side='right' and adding an attention_mask is a common wrong fix; the model still sees padding in the middle of the sequence, breaking causal masking.

  2. 90% fail

    Disabling padding entirely (padding=False) often causes batch processing to fail due to uneven sequence lengths, leading to a different error.