Abstract
Large Language Models (LLMs) have become dominant in the Natural Language Processing (NLP) field, causing a huge surge in progress. However, their limitations are still a mystery and have primarily been explored through tailored datasets to analyze specific human-level skills. In this paper, we introduce perplexed, a library for exploring where a particular language model is perplexed. To show the flexibility and types of insights that can be gained by perplexed, we conducted a case study focused on LLMs for code generation using an additional tool we built called codetokenizers. Specifically, we explore success and failure cases at the token level of code LLMs under different scenarios pertaining to the type of coding structure the model is predicting. From this analysis, we found that our studied code LLMs had their worst performance on coding structures where the code was not syntactically correct. Additionally, we found the models to generally perform worse at predicting internal method invocations than external ones.
Introduction
While LLMs have revolutionized NLP, their limitations often remain a "black box." Most evaluations rely on high-level benchmarks or "Bertology" (probing internal layers), which can be difficult to interpret. We know models fail, but we don't always know if they fail because of a specific word type, a complex grammatical structure, or a lack of context.
In the domain of code generation, this is particularly acute. A model might have a low average error but fail catastrophically on a specific type of logic. The authors address this by providing tools to perform granular, per-token failure analysis, allowing researchers to see exactly which coding constructs (like variable names vs. operators) trigger the highest "perplexity" (uncertainty) in a model.
Methodology: Mapping Perplexity to Structure
The authors developed two primary tools to move beyond aggregate accuracy scores:
- PERPLEXED: A Python library that integrates with HuggingFace to calculate cross-entropy loss on a per-token basis. Instead of looking at the average loss of a whole document, it identifies the specific "perplexing" tokens where the model’s probability distribution is most spread out.
- CODETOKENIZERS: A specialized tool for code analysis. It aligns standard Byte Pair Encoding (BPE) tokens with Abstract Syntax Tree (AST) nodes using the
tree-sitterlibrary.
By combining these, the researchers can ask: "Is the model more confused by FunctionDeclarations or StringLiterals?" This "semantic token" approach allows for a much more nuanced understanding of model behavior than simple text-based metrics.
Key Insights and Results
For their case study, the authors evaluated the SantaCoder model using a subset of the codeparrot/github-code dataset. The analysis revealed several critical findings:
- The "Syntax Error" Wall: The most surprising finding was that the top 10 "worst-performing" AST nodes were almost exclusively
ERRORnodes. This suggests that LLMs are highly sensitive to syntactically incorrect code; when the code doesn't "make sense" structurally, the model's predictive capabilities collapse. - The Context Gap (Internal vs. External): Models are significantly better at predicting calls to popular external libraries (like
numpyorpandas) than calls to functions defined within the same local project. Internal method invocations had an average cross-entropy of 0.777, while external ones were lower (better) at 0.636. - Frequency and Role: There is a moderate negative correlation ($\\approx -0.319$) between how often a token appears in the dataset and how perplexed the model is by it. Common tokens are easier to predict, but the type of token (its role in the code) is a major factor in performance.
Conclusion
The introduction of PERPLEXED and CODETOKENIZERS provides the research community with the means to perform deep-dive analyses into LLM behavior. The struggle with internal method calls highlights a major pitfall: current LLMs often lack the "project-wide" context needed to understand how different files in a software system interact. Future work will focus on handling more complex tokenization schemes and exploring whether training on "improperly written code" can improve model robustness against syntax errors.
Summary updated