The error you're encountering, AssertionError: Padding_idx must be within num_embeddings, occurs because the padding_idx used in the embedding layer of the GLM model is not valid for the model's vocabulary size. This typically happens when the tokenizer and model are mismatched or the padding token is not properly configured. Here's how to fix it:
Root Cause
The error arises because the padding_idx (the ID of the padding token in the tokenizer) exceeds the number of embeddings (num_embeddings) in the model's embed_tokens layer. This mismatch occurs when:
- The tokenizer's padding token ID is not within the model's vocabulary size.
- The model and tokenizer are not compatible (e.g., using a tokenizer from a different model version).
Steps to Fix
1. Verify Tokenizer and Model Compatibility
Ensure you're using the correct tokenizer for the model. For CogView4, use the tokenizer from the same model checkpoint:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("your-cogview4-model-name")
2. Check Padding Token ID
Print the tokenizer's padding token ID and the model's vocabulary size:
print("Tokenizer padding token ID:", tokenizer.pad_token_id)
print("Model vocabulary size:", model.config.vocab_size)
- If
tokenizer.pad_token_id >= model.config.vocab_size, the padding token is invalid.
3. Set a Valid Padding Token
If the tokenizer doesn't have a padding token or the ID is invalid:
# Add a padding token if missing
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model.resize_token_embeddings(len(tokenizer)) # Update model embeddings
This ensures the padding token is within the vocabulary size.
4. Use the Correct Model Configuration
If you're using a custom model, ensure the pad_token_id is set in the model's configuration:
model.config.pad_token_id = tokenizer.pad_token_id
5. Validate Input IDs
Ensure your input IDs do not contain invalid tokens. For example:
text_input_ids = tokenizer("Your prompt here", return_tensors="pt").input_ids
print("Input IDs:", text_input_ids)
Check that all IDs are within 0 to model.config.vocab_size - 1.
Example Fix
Here’s a complete example to resolve the issue:
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained("your-cogview4-model-name")
model = AutoModelForCausalLM.from_pretrained("your-cogview4-model-name")
# Ensure padding token is set
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model.resize_token_embeddings(len(tokenizer))
# Process input
text = "Your prompt here"
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True).to("cuda")
outputs = model(**inputs)
Key Notes
- Always use the same model and tokenizer pair.
- Avoid manually setting
padding_idx unless you're certain of the vocabulary size.
- If using a custom model, ensure the tokenizer's vocabulary matches the model's embedding layer.
By aligning the tokenizer and model configurations, you should resolve the Padding_idx error. Let me know if you need further clarification!