Abstract
As Large Language Models (LLMs) become integral to software engineering, understanding the distribution of their stochastic outputs is critical. This paper investigates the structural "shape" of program distributions by treating LLM-generated Python code as points in a metric space. By utilizing Abstract Syntax Trees (ASTs), Tree-Edit Distance (TED), and Topological Data Analysis (TDA), we compare the consistency and explorative tendencies of ChatGPT-4 and TinyLlama. Our findings reveal that ChatGPT-4 produces highly clustered, consistent results, while TinyLlama exhibits significantly higher dispersion that scales with model temperature.
Introduction
The relationship between a prompt (stochastic input) and the resulting distribution of programs is a fundamental question in AI-supported programming. Traditional code comparison methods often rely on string-based metrics like Hamming distance, which fail to account for the underlying syntax and logic of programming languages. To address this, we propose a framework that respects the structural essence of code, allowing us to characterize the geometry and topology of the "program space" without relying on lossy low-dimensional embeddings.
Methodology
To analyze programs as a metric space $(M', d)$, we employ a multi-step pipeline:
- Abstract Syntax Trees (ASTs): Programs are converted into ASTs using Python’s
astmodule. This captures structural logic while ignoring trivial differences such as comments or whitespace. - Tree-Edit Distance (TED): The distance $d$ between two programs is defined as the minimum number of operations (insert, delete, relabel) required to transform one AST into another.
- Geometric Statistics: We utilize Geometric Medians to identify central programs and Ripley’s K-function to determine if programs tend to cluster or repel.
- Topological Data Analysis (TDA): Persistent Homology is applied to identify cycles in the program space. For instance, interchanging variable names can create a 1-cycle (a loop) within the TED metric space.
Experimental Setup
The study utilized two primary models: ChatGPT-4 (proprietary) and TinyLlama (1.1B parameters, open-source). TinyLlama was tested at three temperature settings: 0.5, 0.7, and 0.9. The models were tasked with seven variations of a prompt related to image thresholding (e.g., "Write a function which segments an image using Otsu's threshold"). Each prompt was repeated 100 times, generating a dataset of 700 programs per model configuration.
Key Results
The quantitative analysis highlights a stark contrast in how different models navigate the program space. ChatGPT-4 acts as a "focused" assistant, with outputs clustering tightly around a preferred implementation style. Conversely, TinyLlama is more "explorative," rarely generating identical syntax twice and showing a measurable "repulsion tendency" at small scales.
As shown in the statistical data, ChatGPT-4 maintained an average dispersion of 65.0. In comparison, TinyLlama at $t=0.5$ showed more than double the dispersion at 148.3, which climbed to 207.7 at $t=0.9$. This confirms that increasing the temperature parameter directly correlates with an increase in the geometric dispersion of the resulting programs.
Discussion and Limitations
While the AST-TED approach provides a more robust metric than string-based methods, it is not without challenges. The computational cost of calculating distance matrices is significant; generating the TinyLlama matrices required approximately 70 hours on a MacBook Pro due to the $O(n^2)$ nature of the calculations. Furthermore, while ASTs capture structure, they do not perfectly capture "semantic identity" (e.g., the functional equivalence of a for loop and a while loop).
Future work will explore "unfolding" code into canonical forms before AST generation to better capture semantic similarity and refining TDA techniques to be less sensitive to noise. These developments could eventually allow for automated, deep insights into the inherent structure of programming languages and the models that generate them.
Summary updated