Can LLMs Implicitly Learn Numeric Parameter Constraints in Data Science APIs?

Yinlin Deng, Chunqiu Steven Xia, Zhezhen Cao, Meiziniu Li, Lingming ZhangNeurIPS 2024

Introduction

While Large Language Models (LLMs) are surprisingly adept at generating common data science code, they often rely on memorization rather than a true understanding of the underlying mathematical rules. Data Science (DS) libraries such as PyTorch and NumPy are the bedrock of modern AI, but their APIs come with strict "numeric parameter constraints"—mathematical relationships that must be satisfied for the code to execute successfully.

Examples of implicit numeric constraints in PyTorch APIs

For example, in a 2D convolution (Conv2d), the number of groups must evenly divide both the in_channels and out_channels. As we increasingly rely on LLMs to act as coding assistants, we operate under the assumption that these models have implicitly "learned" these rules from their training data. A new study and benchmark, DSEVAL, investigates whether that assumption holds true: Do LLMs actually understand the logic, or are they just repeating patterns they've seen a thousand times?

Methods: Categorizing Constraints and Evaluating Models

To rigorously test this, researchers conducted an empirical study focusing on 28 widely used APIs. They categorized the mathematical constraints into four distinct types:

Four categories of numeric constraints evaluated in the study
  1. Equality: Values must match exactly (e.g., copying a specific dimension).
  2. Inequality: Values must stay within a valid range (e.g., a dimension index must be less than the tensor's rank).
  3. Arithmetic: Complex relationships involving division, modulus, or products (e.g., the Fold operation).
  4. Set-related: Requirements for uniqueness or completeness across parameters.

The models were evaluated across three levels of granularity: generating a Full Program from scratch, filling in All Parameters for a given input, and predicting an Individual Parameter to satisfy a specific constraint. To verify the results efficiently without the overhead of executing heavy deep-learning tensors, the researchers employed Satisfiability Modulo Theories (SMT) solvers (specifically Z3) to check the mathematical validity of the LLM-generated parameters instantly.

The DSEVAL Benchmark

The authors introduced DSEVAL, a first-of-its-kind public benchmark designed to systematically evaluate LLM comprehension of DS API constraints. It features 19,600 unique problems covering 12 representative APIs from PyTorch and NumPy. Problems are grouped into "Easy," "Medium," and "Hard" categories based on the complexity of the inputs and the rarity of the required parameter values.

Key Insights and Results

The study evaluated eight state-of-the-art models, revealing a vast performance gap between proprietary and open-source models, as well as fundamental flaws in how LLMs handle math:

  • The Memorization Trap: LLMs are excellent at generating "simple" programs that mirror common training data (like the standard 3x224x224 ImageNet tensor shape). However, when input shapes become unusual, performance craters.
  • The "-1" Crutch: In operations like reshape, GPT-4-Turbo frequently uses the -1 placeholder, which tells the library to infer the dimension. When forced to provide the actual number, the model's accuracy drops, suggesting it prefers to avoid arithmetic entirely.
  • Arithmetic is the "Final Boss": While models handle equality and simple inequalities well, they struggle heavily with arithmetic constraints. For the Conv2d API, GPT-4-Turbo could only solve the hardest subset of problems about 24% of the time.
  • Scale Doesn't Solve Everything: GPT-4-Turbo led the pack with an average accuracy of 57.5% on "Hard" PyTorch constraints, while the best open-source model, CodeLlama-13b, achieved only 39.2%. Despite this lead, even the best models fail to show genuine comprehension of complex formulas.
  • Ranking Shifts: Models that excel at "Easy" tasks (like CodeQwen1.5) often see their rankings plummet as difficulty increases, while others (like StarCoder) prove more robust on harder problems.

Limitations and Future Work

Currently, the study is limited to numeric constraints across 28 APIs and does not cover other parameter types like strings or booleans. Future work aims to expand DSEVAL to include more libraries like Pandas and SciPy. Additionally, researchers plan to explore how Chain-of-Thought (CoT) or ReAct prompting can help models "reason through" the math before outputting code, and how integrating API documentation directly into the model's reasoning loop might reduce its reliance on memorized patterns.

Summary updated

BibTeX
@inproceedings{deng2024can,
  author = {Yinlin Deng and Chunqiu Steven Xia and Zhezhen Cao and Meiziniu Li and LINGMING ZHANG},
  title = {{Can LLMs Implicitly Learn Numeric Parameter Constraints in Data Science APIs?}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=LfC5rujSTk},
}
Figure