Learning to Compile Programs to Neural Networks

Logan Weber, Jesse Michel, Alex Renda, Michael Carbin

Abstract

Neural surrogates—small, efficient neural networks that mimic complex software—are vital for resource-constrained environments like robotics and scientific simulation. However, traditional methods for creating these surrogates, such as training from scratch, are data-hungry and computationally expensive. This paper introduces Neural Surrogate Compilation via COMPNET, a hypernetwork that "compiles" C source code directly into the initial weights of a neural network. By treating program text as a structural prior, COMPNET provides a "warm start" that achieves high accuracy with up to 9.5x less data and 7x faster training than standard random initialization.

Introduction: The "Missing Middle" of Surrogates

In modern engineering, researchers often replace slow, high-fidelity simulations with neural surrogates to enable real-time execution. Currently, developers face a binary choice: train a small network from scratch using thousands of input-output pairs, or use a massive Large Language Model (LLM) to "execute" the code. The former is data-intensive, while the latter is too slow for embedded systems.

The authors identify a "missing middle": the need for a small, standalone surrogate that doesn't require massive training overhead for every new program. COMPNET addresses this by using a hypernetwork to amortize the cost of learning, allowing for the near-instantaneous generation of high-quality surrogate initializations.

Methodology: The COMPNET Pipeline

COMPNET functions as a neural compiler. Instead of learning a specific function, it learns the mapping between program logic and neural network parameters. The process is divided into five distinct phases:

  1. Tokenization: The C source code is decomposed into a sequence of tokens.
  2. Embedding: A pre-trained BERT-Tiny encoder processes these tokens to extract a high-dimensional representation of the program's logic.
  3. Regressing: A linear "parameter head" maps the code embedding to a vector of neural network weights.
  4. Parameterizing: These weights are loaded into a Covering Architecture—a fixed multilayer perceptron (MLP) designed with enough capacity to represent a wide variety of program signatures through padding and cloning techniques.
  5. Finetuning: The "compiled" network undergoes brief training on a small, program-specific dataset to reach peak performance.
The COMPNET architecture pipeline: from C source code to an executable neural surrogate.

Experimental Setup and Datasets

To evaluate COMPNET, the researchers developed EXESTACK, a massive dataset of 69,083 executable C programs scraped from GitHub. A refined subset, EXESTACKCPN (37,772 programs), was used to train the hypernetwork. For benchmarking, the authors utilized PARROTBENCHCPN, which includes complex kernels such as Fast Fourier Transform (FFT), K-Means clustering, and Sobel edge detection.

Key Results and Insights

COMPNET demonstrates that program text contains rich structural information that can be translated into weight patterns, such as ReLU gates mimicking conditional logic.

Data and Training Efficiency

Compared to random initialization and meta-learning baselines like MAML, COMPNET-initialized surrogates reached target error rates using 1.9x to 9.5x less training data. Furthermore, the convergence was significantly faster, requiring 4.3x to 7.3x fewer epochs. In "zero-shot" scenarios (before any finetuning), COMPNET provided an 84x improvement in initial loss over random baselines.

Perceptual Quality

In practical applications like color quantization (K-Means), COMPNET's advantage is visually apparent. When training data is extremely scarce (0.1%), COMPNET surrogates produce outputs that are significantly more faithful to the ground truth than those initialized randomly or via other meta-learning techniques.

Comparison of surrogate performance on a color quantization task (K-Means) at 0% and 0.1% dataset sizes.

Limitations and Future Work

While a significant step forward, COMPNET currently has several constraints:

  • Language Scope: It is limited to numeric, pointer-free C functions and cannot yet handle recursion or dynamic memory.
  • Fixed Topology: The use of a single "covering architecture" limits the complexity of programs that can be accurately represented.
  • Input Handling: The 512-token limit of the BERT encoder prevents the compilation of very large codebases.

Future research will focus on dynamic network topologies and scaling the encoder to handle more complex, multi-file programs.

Summary updated

BibTeX
@inproceedings{weber2024learning,
  author = {Logan Weber and Jesse Michel and Alex Renda and Michael Carbin},
  title = {{Learning to Compile Programs to Neural Networks}},
  booktitle = {International Conference on Machine Learning},
  year = {2024},
  url = {https://openreview.net/forum?id=rJti61Uere},
}
Figure