pytorch runtime_error ai_generated true

IndexError: index out of range in Embedding

ID: pytorch/embedding-index-error

Also available as: JSON · Markdown
85%Fix Rate
88%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2 active

Root Cause

Token ID exceeds embedding vocabulary size.

generic

Workarounds

  1. 92% success Set num_embeddings to match tokenizer vocab size: len(tokenizer)
  2. 88% success Use padding_idx for out-of-vocabulary handling

Dead Ends

Common approaches that don't work:

  1. Increase embedding size blindly 72% fail

    Wastes memory for sparse IDs

  2. Clip all IDs to max 78% fail

    Wrong embeddings for OOV tokens