Table of Contents
Fetching ...

Dynamic Stability of LLM-Generated Code

Prateek Rajput, Abdoul Aziz Bonkoungou, Yewei Song, Abdoul Kader Kabore, Iyiola E. Olatunji, Jacques Klein, Tegewende Bissyande

TL;DR

This work tackles the gap between functional correctness and runtime stability in LLM-generated code by introducing a principled stability framework that leverages opcode distributions. It defines Static Canonical Trace Divergence (SCTD) and Dynamic Canonical Trace Divergence (DCTD), with Behavioral Expression Factor (BEF) as their ratio to diagnose stability vs. redundancy. Through CodeContests and BigOBench benchmarks across 11 diverse LLMs, the study shows nontrivial algorithmic and runtime divergence even among functionally correct solutions, and uncovers a 'penalty of instability' where higher temperature improves pass@1 at the cost of stability. The findings argue for stability-aware objectives in model training and benchmarking, supported by asymptotic test cases to ensure robust, real-world code generation performance. Math-driven opcode analyses reveal that stability metrics capture dimensions of code quality that are largely orthogonal to traditional syntax-based metrics, advocating a shift toward dynamic stability as a primary evaluation criterion.

Abstract

Current evaluations of LLMs for code generation emphasize functional correctness, overlooking the fact that functionally correct solutions can differ significantly in algorithmic complexity. For instance, an $(O(n^2))$ versus $(O(n \log n))$ sorting algorithm may yield similar output but incur vastly different performance costs in production. This discrepancy reveals a critical limitation in current evaluation methods: they fail to capture the behavioral and performance diversity among correct solutions. To address this, we introduce a principled framework for evaluating the dynamic stability of generated code. We propose two metrics derived from opcode distributions: Static Canonical Trace Divergence (SCTD), which captures algorithmic structure diversity across generated solutions, and Dynamic Canonical Trace Divergence (DCTD), which quantifies runtime behavioral variance. Their ratio, the Behavioral Expression Factor (BEF), serves as a diagnostic signal: it indicates critical runtime instability when BEF $\ll$ 1 and functional redundancy when BEF $\gg$ 1. Empirical results on BigOBench and CodeContests show that state-of-the-art LLMs exhibit significant algorithmic variance even among functionally correct outputs. Notably, increasing sampling temperature improves pass@1 rates but degrades stability, revealing an unrecognized trade-off: searching for correct solutions in diverse output spaces introduces a "penalty of instability" between correctness and behavioral consistency. Our findings call for stability-aware objectives in code generation and new benchmarks with asymptotic test cases for robust, real-world LLM evaluation.

Dynamic Stability of LLM-Generated Code

TL;DR

This work tackles the gap between functional correctness and runtime stability in LLM-generated code by introducing a principled stability framework that leverages opcode distributions. It defines Static Canonical Trace Divergence (SCTD) and Dynamic Canonical Trace Divergence (DCTD), with Behavioral Expression Factor (BEF) as their ratio to diagnose stability vs. redundancy. Through CodeContests and BigOBench benchmarks across 11 diverse LLMs, the study shows nontrivial algorithmic and runtime divergence even among functionally correct solutions, and uncovers a 'penalty of instability' where higher temperature improves pass@1 at the cost of stability. The findings argue for stability-aware objectives in model training and benchmarking, supported by asymptotic test cases to ensure robust, real-world code generation performance. Math-driven opcode analyses reveal that stability metrics capture dimensions of code quality that are largely orthogonal to traditional syntax-based metrics, advocating a shift toward dynamic stability as a primary evaluation criterion.

Abstract

Current evaluations of LLMs for code generation emphasize functional correctness, overlooking the fact that functionally correct solutions can differ significantly in algorithmic complexity. For instance, an versus sorting algorithm may yield similar output but incur vastly different performance costs in production. This discrepancy reveals a critical limitation in current evaluation methods: they fail to capture the behavioral and performance diversity among correct solutions. To address this, we introduce a principled framework for evaluating the dynamic stability of generated code. We propose two metrics derived from opcode distributions: Static Canonical Trace Divergence (SCTD), which captures algorithmic structure diversity across generated solutions, and Dynamic Canonical Trace Divergence (DCTD), which quantifies runtime behavioral variance. Their ratio, the Behavioral Expression Factor (BEF), serves as a diagnostic signal: it indicates critical runtime instability when BEF 1 and functional redundancy when BEF 1. Empirical results on BigOBench and CodeContests show that state-of-the-art LLMs exhibit significant algorithmic variance even among functionally correct outputs. Notably, increasing sampling temperature improves pass@1 rates but degrades stability, revealing an unrecognized trade-off: searching for correct solutions in diverse output spaces introduces a "penalty of instability" between correctness and behavioral consistency. Our findings call for stability-aware objectives in code generation and new benchmarks with asymptotic test cases for robust, real-world LLM evaluation.

Paper Structure

This paper contains 35 sections, 2 theorems, 8 equations, 8 figures, 3 tables.

Key Result

theorem 1

Given that $\mathrm{JSD}(x,y) \in [0, 1]$ for any PMFs $x,y$, both SCTD_JSD and DCTD_JSD are bounded in $[0,1]$.

Figures (8)

  • Figure 1: Two functionally correct algorithms for deduplication, but with vastly different runtime characteristics. Algorithm A scales efficiently, while Algorithm B suffers from quadratic-time degradation.
  • Figure 2: Pipeline illustrating the end-to-end process for computing SCTD, DCTD, and BEF. Static stability is assessed through analysis of Python bytecode using the dis module (SCTD), while dynamic stability is evaluated through runtime traces (DCTD).
  • Figure 3: Pipeline for evaluating the impact of prompt control on dynamic stability. Code is generated from a dataset using an LLM under varying prompt conditions, and the resulting outputs are assessed using dynamic stability metrics including DCTD, SCTD, and BEF.
  • Figure 4: SCTD_JSD for BigObench at different temperatures
  • Figure 5: DCTD_JSD for BigOBench at different temperatures
  • ...and 3 more figures

Theorems & Definitions (2)

  • theorem 1: JSD-Based Metrics are Bounded
  • theorem 2: Sharp Upper Bound for $\tau$ Normalization