timlawrenz's picture
Upload paper.md with huggingface_hub
196b28f verified
|
raw
history blame
16.1 kB

Graph Neural Networks for Ruby Code Complexity Prediction and Generation: A Systematic Architecture Study

Authors: Tim Lawrenz, with autonomous experimentation by Ratiocinator

Abstract. We present a systematic study of Graph Neural Network (GNN) architectures for two tasks on Ruby Abstract Syntax Trees (ASTs): cyclomatic complexity prediction and code generation via graph autoencoders. Using a dataset of 22,452 Ruby methods, we evaluate five GNN architectures (GCN, GraphSAGE, GAT, GIN, GraphConv) across 35 GPU experiments on RTX 4090 instances. For complexity prediction, a 5-layer GraphSAGE achieves MAE 4.018 (R² = 0.709), a 16% improvement over the 3-layer baseline. GIN ranks second among equal-depth models. For code generation, we find a stark negative result: standard graph autoencoders produce 0% syntactically valid Ruby across all tested architectures, loss functions, and hidden dimensions. Only a tree-aware decoder with teacher-forced GIN edges achieves non-zero validity (7%), suggesting that explicit structural supervision is necessary but insufficient for GNN-based code generation. Total compute cost: under $5 USD.


1. Introduction

Graph Neural Networks have emerged as a natural representation for source code, where Abstract Syntax Trees provide a structured graph encoding of program syntax and semantics. Prior work has applied GNNs to tasks including vulnerability detection (Devign; Zhou et al., 2019), code clone detection (ASTNN; Zhang et al., 2019), and type inference. However, two questions remain underexplored:

  1. Which GNN architecture best predicts code complexity from ASTs? Existing studies typically evaluate one or two architectures without controlled comparisons.
  2. Can GNN autoencoders generate syntactically valid code? While graph variational autoencoders have shown promise in molecular generation, their application to code synthesis is largely uncharted.

We address both questions through a systematic study on Ruby, a dynamically-typed language whose relatively uniform syntax makes AST analysis tractable. Our contributions are:

  • A controlled 5-way architecture comparison for complexity prediction, showing that network depth matters more than width or architecture choice (Section 4.1).
  • A comprehensive negative result demonstrating that GNN autoencoders cannot generate valid Ruby code under standard training regimes (Section 4.2).
  • Evidence that teacher-forced tree-aware decoding with GIN is the only configuration that produces any valid output (7%), pointing toward necessary conditions for GNN-based code generation (Section 4.3).
  • All experiments were orchestrated by Ratiocinator, an autonomous research pipeline, demonstrating reproducible AI-driven experimentation at under $5 total compute cost (Section 3).

2. Related Work

GNNs for Code Understanding. Allamanis et al. (2018) introduced GNNs for variable misuse detection. CodeBERT (Feng et al., 2020) and GraphCodeBERT (Guo et al., 2021) combine transformer architectures with code structure. Our work differs by focusing on classical GNN variants (GCN, SAGE, GAT, GIN, GraphConv) in a controlled comparison rather than proposing a new architecture.

Graph Autoencoders. Kipf and Welling (2016) proposed variational graph autoencoders for link prediction. Junction Tree VAE (Jin et al., 2018) generates molecular graphs with validity guarantees by decomposing graphs into tree structures. Our tree-aware decoder draws inspiration from this approach but operates on ASTs rather than molecular substructures.

Code Complexity Prediction. McCabe's cyclomatic complexity (1976) is a standard software metric. ML approaches using hand-crafted features (Gill and Kemerer, 1991) have been supplemented by deep learning methods, but few use graph representations of the AST directly.

Automated Research. Our experimental infrastructure, Ratiocinator, belongs to the emerging class of AI-driven scientific discovery tools alongside systems like The AI Scientist (Lu et al., 2024). We demonstrate that such systems can conduct meaningful ablation studies at minimal cost.

3. Experimental Setup

3.1 Dataset

We use 22,452 Ruby methods extracted from open-source repositories, each parsed into an AST with:

  • 74-dimensional node features encoding node type (one-hot), depth, sibling index, and subtree statistics.
  • Edge attributes (3D): edge type encoding, relative depth, and child index.
  • Positional encodings (2D): tree depth and sibling position.
  • Labels: McCabe cyclomatic complexity (integer, range 1–200+).

The dataset is split 85/15 into training (19,084) and validation (3,368) sets.

3.2 Models

Complexity Prediction. RubyComplexityGNN applies $L$ message-passing layers followed by global mean pooling and an MLP regressor. We evaluate five convolution operators:

  • GCN (Kipf and Welling, 2017): Symmetric normalized adjacency.
  • GraphSAGE (Hamilton et al., 2017): Mean aggregation with concatenation.
  • GAT (Veličković et al., 2018): Multi-head attention (4 heads).
  • GIN (Xu et al., 2019): Sum aggregation with learnable epsilon, designed for maximal expressiveness under the WL test.
  • GraphConv (Morris et al., 2019): General message-passing with separate self/neighbor transforms.

Code Generation. ASTAutoencoder encodes the AST into a fixed-size latent vector, then decodes to reconstruct node types and edge structure. The decoder supports three edge construction modes:

  • Chain: Nodes connected sequentially (baseline, no structural information).
  • Teacher-forced: Ground-truth AST edges provided during training.
  • Iterative: Decoder predicts edges from node embeddings (no ground truth at inference).

3.3 Infrastructure

All experiments run on single NVIDIA RTX 4090 GPUs (24 GB VRAM) provisioned via Vast.ai. Training uses Adam optimizer (lr=0.001), batch size 32, and 50 epochs (complexity) or 30 epochs (generation). The Ratiocinator pipeline handles instance provisioning, code deployment via Git, dependency installation, training execution, metric collection, and instance cleanup.

3.4 Experiment Design

Track Task Arms Varied Parameters
1 Complexity prediction 8 Architecture, depth, width
2 Generation (chain decoder) 7 Architecture, loss function, width
4 Generation (tree-aware decoder) 6 Edge mode, architecture, loss
Baseline Complexity (autonomous) 18 Random seed variance

Additionally, 18 runs from the autonomous Ratiocinator coordinator (3 iterations × ~6 successful arms) provide baseline variance data under identical SAGE/64/3 configuration, yielding MAE μ = 4.745, σ = 0.073.

4. Results

4.1 Track 1: Architecture Comparison for Complexity Prediction

Architecture Hidden Dim Layers Val MAE ↓ Val MSE
SAGE 64 5 4.018 54.37 0.709
GIN 64 3 4.589 69.28 0.629
SAGE (baseline) 64 3 4.782 68.07 0.635
GraphConv 64 3 4.804 68.14 0.635
SAGE (wide) 128 3 4.863 68.15 0.635
GAT 64 3 4.952 73.19 0.608
GCN 64 3 5.321 81.61 0.563

Key findings:

  1. Depth dominates. The 5-layer SAGE achieves MAE 4.018, a 16.0% relative improvement over the 3-layer baseline (4.782). This is far outside the baseline variance band (σ = 0.073 from 18 replicate runs).

  2. Width does not help. Doubling the hidden dimension from 64 to 128 (SAGE-wide) produces no improvement (4.863 vs 4.782), suggesting the representational bottleneck is in message-passing depth, not per-layer capacity.

  3. Architecture ranking (at 3 layers): GIN > SAGE ≈ GraphConv > GAT > GCN. GIN's theoretical advantage under the Weisfeiler-Leman framework translates to a practical 4% improvement over SAGE. GCN, lacking learnable aggregation weights, performs worst with 11% higher MAE.

  4. GAT underperforms expectations. Despite attention being theoretically more expressive, GAT ranks fifth. We hypothesize that the relatively uniform AST structure (most nodes have 2–4 children) does not benefit from attention-based neighbor weighting. GAT also ran slowest, with the gat-wide arm timing out at the 1200s budget.

4.2 Track 2: Generation Failure Analysis

Decoder Conv Hidden Dim Loss Function Syntactic Validity
GAT 256 improved 0%
GAT 512 improved 0%
SAGE 256 improved 0%
GIN 256 improved 0%
GCN 256 improved 0%

Every configuration achieves 0% syntactic validity. Validation loss converges to approximately 7.7 across all variants, indicating the model learns non-trivial representations but cannot reconstruct valid ASTs.

We hypothesize three contributing factors:

  1. Token vocabulary size. Ruby ASTs use ~200 node types. With 74-dimensional features, the decoder must recover discrete types from continuous embeddings — a fundamentally lossy reconstruction.

  2. Structural coherence. Valid Ruby requires nested structures (begin/end blocks, do/end, def/end) that span arbitrary distances in the graph. The chain decoder connects nodes sequentially, destroying the tree topology.

  3. Loss function mismatch. Cross-entropy on node types does not penalize structural invalidity. A syntactically-aware loss would need to evaluate the generated AST against Ruby's grammar, which is combinatorially expensive.

4.3 Track 4: Tree-Aware Decoder Topology

Edge Mode Conv Validity Val Loss
chain GAT 0% 7.715
teacher_forced GAT 0% 7.706
iterative GAT 0% 7.765
teacher_forced SAGE 0% 7.799
teacher_forced GIN 7% 8.384

The teacher-forced GIN configuration is the only one that produces any valid Ruby code (7 out of 100 samples). This result is notable for several reasons:

  1. Teacher forcing is necessary. The chain and iterative modes produce 0% validity even with the same architecture. Ground-truth AST edges during training provide the structural scaffold the decoder needs.

  2. GIN is the only viable architecture. Despite GAT and SAGE also receiving teacher-forced edges, only GIN produces valid output. This aligns with GIN's superior WL-test expressiveness — it can distinguish subtree structures that other architectures conflate.

  3. Higher val_loss paradox. The teacher-forced GIN has the highest validation loss (8.384 vs ~7.7), yet is the only model that generates valid code. This suggests the standard reconstruction loss is a poor proxy for code validity — the model may be learning a sharper but riskier distribution.

  4. 7% is still insufficient. Even the best configuration produces only 7 valid samples out of 100, far below practical utility. This suggests that GNN autoencoders, even with structural supervision, face fundamental limitations for code generation.

5. Discussion

The Representation Gap

Our results reveal a representation gap between understanding and generation in GNN-based code models. For complexity prediction (a graph-level regression task), GNNs perform well — a 5-layer SAGE explains 71% of variance in cyclomatic complexity. But for generation (requiring node-level reconstruction of discrete types and valid tree structure), the same representations fail catastrophically.

This asymmetry mirrors findings in NLP, where masked language models excel at classification but require autoregressive decoding for generation. GNNs face an analogous challenge: message-passing aggregates information for global understanding but loses the fine-grained sequential and hierarchical structure needed for reconstruction.

Depth vs. Width

The stark depth-over-width result (5-layer SAGE improves 16%; 128-dim SAGE improves 0%) has practical implications. For code ASTs with depths of 10–30, a 3-layer GNN can only aggregate information from a 3-hop neighborhood. Deeper networks capture cross-branch dependencies (e.g., a variable defined in one branch and used in another) that directly relate to complexity.

Why GIN for Generation?

GIN's success as the sole architecture achieving non-zero validity likely stems from its injective aggregation function. The sum aggregation with learnable epsilon preserves multiset information about node neighborhoods, which is critical for distinguishing structurally similar but semantically different AST subtrees (e.g., if/else vs case/when). Other architectures, by averaging or attending, lose this fine-grained structural signal.

Limitations

  • Single language. Ruby results may not transfer to languages with different AST structures.
  • Fixed hyperparameters. We did not tune learning rate, dropout, or batch size per architecture.
  • No cross-validation. Results are from a single train/val split.
  • Small generation evaluation. Validity is measured on 100 samples; a larger evaluation would reduce variance.

6. Conclusion

We conducted a systematic study of five GNN architectures for Ruby code complexity prediction and generation across 35 GPU experiments. Our findings are:

  1. For complexity prediction, go deeper: A 5-layer GraphSAGE (MAE 4.018, R² 0.709) outperforms all 3-layer variants by 16%, while doubling width provides no benefit.
  2. GNN autoencoders cannot generate valid code: Zero syntactic validity across 10+ configurations with standard chain decoders.
  3. Structural supervision is necessary but insufficient: Teacher-forced GIN decoding achieves 7% validity — the sole non-zero result — but remains impractical.
  4. Architecture expressiveness matters for generation: GIN, the most expressive architecture under the WL framework, is the only one that crosses the 0% validity barrier.

These results suggest that future work on GNN-based code generation should focus on (a) hybrid architectures combining GNN encoding with autoregressive or grammar-constrained decoding, (b) hierarchical generation strategies inspired by Junction Tree VAE, and (c) validity-aware training objectives that go beyond token-level reconstruction loss.

Reproducibility

All code is available at github.com/timlawrenz/jubilant-palm-tree (branch: experiment/ratiocinator-gnn-study). Experiments were orchestrated by Ratiocinator (github.com/timlawrenz/ratiocinator) using declarative YAML specifications. Total compute cost: approximately $5 USD on Vast.ai RTX 4090 instances.


Appendix A: Baseline Variance Analysis

The autonomous Ratiocinator coordinator ran 3 iterations of 8 arms each under identical SAGE/64/3 configuration (due to an environment variable propagation bug that was subsequently fixed). This unintentional ablation provides 18 independent replicates:

Statistic Value
Successful runs 18
MAE mean 4.745
MAE std 0.073
MAE range 4.622 – 4.962
R² mean 0.635
R² range 0.627 – 0.638

The 5-layer SAGE result (MAE 4.018) is 9.9 standard deviations below this baseline mean, confirming its statistical significance.

Appendix B: Compute Cost Breakdown

Experiment Arms Successful Approx. Cost
Autonomous research (3 iter) 24 18 $1.50
Architecture comparison 8 7 $1.20
Generation analysis 7 5 $0.80
Decoder topology 6 5 $0.70
Total 45 35 ~$4.20

Appendix C: Failed Arms

Arm Failure Mode Cause
gat-wide (Track 1) Timeout (exit 124) GAT with 128-dim exceeded 1200s training budget
simple-loss-gat (Track 2) SSH timeout Instance never became SSH-ready
comprehensive-loss-gat (Track 2) Exit 1 Training script error (likely OOM or arg parsing)
teacher-forced-gat-comprehensive (Track 4) Exit 1 Training script error

All failures are infrastructure-related or timeout-related; no experiments produced anomalous metrics.