Paper 1 of 3 · The Neural Compiler
arXiv:2605.22498 · 2026

The Neural Compiler

Program-to-Network Translation for Hybrid Scientific Machine Learning

Write the physics as a Scheme program; compile it into a frozen, exact, differentiable PyTorch module; train only the constants.

Lucas Sheneman Institute for Interdisciplinary Data Sciences, University of Idaho

Abstract

Scientific machine learning frequently requires integrating known physics with unknown components: the equation form is known, but parameters or correction terms must be learned from data. Existing approaches either discard the known structure (neural networks), encode it as a soft penalty the model may violate (physics-informed neural networks), or require hand-implementation for each new equation. The Neural Compiler translates programs in a first-order expression language with Scheme syntax into frozen, differentiable PyTorch modules. Compiled modules compute exactly: they produce the same output as the source program to floating-point precision, with exact gradients via autograd, and contribute zero approximation error on their safe domain, including outside the training distribution. Across six experiments, compiled models recover physical constants to under 1% error with 1 to 4 trainable parameters where PINNs with 8,500+ parameters show 7 to 93% error, and compiled modules compose with zero error at arbitrary depth while neural approximations accumulate errors up to 5.9 × 109. The system supports 51 primitive operations, including vector and matrix algebra.

This is the first paper of the Neural Compiler → DMCI → NDVM program. Its successor, DMCI, compiles the interpreter instead of the program.

1 · How it works

Structure stays exact; constants train

The compiler takes the model as text, an S-expression in a 51-operation first-order language, and emits a frozen PyTorch module that reproduces the program to floating-point precision. Named constants become trainable parameters; everything else is fixed.

From equation to module

A scientist who knows the form of a law but not its constants writes it directly as a program. The damped pendulum θ″ = −(g/L) sin θ − b θ′ becomes the two-line Scheme expression on the right. A single call, compile_scheme(source, inputs={...}), turns it into a frozen nn.Module in under 150 µs. The constants g_L and b are exposed as nn.Parameter entries, so ordinary gradient descent fits them against data while the compiled structure, including the transcendental sin, computes exactly everywhere on its safe domain.

Because the input is a string, modules are systematically generable: changing "(sin x)" to "(exp (sin x))" yields a new correct module instantly, and libraries of compiled equations can be chained or recombined without writing new PyTorch code. The language covers scalar arithmetic (24 ops), vector operations (9), matrix operations (11), and control flow (7), enough for ODE right-hand sides and PDE discretizations.

pendulum.scmscheme
; damped pendulum acceleration
;   theta'' = -(g/L) sin(theta) - b theta'
; g_L and b compile to trainable parameters
(- (* (- 0 g_L) (sin theta))
   (* b theta_dot))

The whole model. Two trainable scalars, one transcendental, no weights to initialize, nothing to approximate.

program text P(θ) compile, <150 μs frozen DirectModule structure exact · θ trainable forward loss  L ∇θ, exact gradients reach only the exposed constants
Figure 1. Compilation freezes the program's structure and exposes its named constants θ as trainable parameters. Autograd through the compiled module yields the true derivative of the program, so gradient descent adjusts the constants while the physics remains exact, in and out of distribution.
1

Parse

Recursive-descent parsing of S-expressions into an AST. Bracket syntax [1 2 3] desugars to (vec 1 2 3).

2

A-Normal Form

Compound subexpressions are let-bound to fresh temporaries, giving a one-to-one map from bindings to graph nodes.

3

Tail-call optimization

Tail-recursive time-stepping and fixed-point loops are rewritten as iteration, so scientific loops execute efficiently.

4

Graph → DirectModule

Each binding becomes a node in a dataflow DAG, compiled to a PyTorch nn.Module that evaluates in topological order and broadcasts over batches.

Figure 2. The compiler pipeline, live in your browser. Type a program and step through parse, A-Normal Form, and graph construction; the highlighting links every stage to the next. This is a faithful reimplementation of the paper's front end, running with no server.
2 · Results

Hard constraints against four baseline families

Six experiments: 15 Feynman physics laws, two ODE systems (Lotka-Volterra and a damped pendulum), a 1D heat equation, compositional generalization, and 3D vector mechanics, each compared against hand-coded PyTorch, PINN, Neural ODE, and pure MLP baselines.

2 vs 8,706
trainable parameters: compiled pendulum model vs the MLP baseline
731×
better in-distribution trajectory MSE for the compiled pendulum model
3,000×
better trajectory MSE at 5× extrapolation beyond the training horizon
0.00% vs 92.7%
diffusivity recovery error, compiled model vs PINN on the heat equation

Damped pendulum: 2 parameters against 8,706

The pendulum's transcendental sin term is exactly what MLPs approximate poorly outside their training range. The full-structure compiled model, just the two constants g_L and b, recovers g/L to 0.08% error and achieves 731× better in-distribution trajectory MSE than the 8,706-parameter MLP, and 3,000× better at 5× extrapolation (5.0×10−6 vs 1.5×10−2). The PINN baseline (8,580 parameters) recovers g/L with 41.1% error: the soft physics penalty is insufficient for the nonlinear dynamics.

Six-panel damped pendulum figure: in-distribution angle and angular-velocity fits, 5x extrapolation trajectories, trajectory MSE comparison, test-loss curves, and parameter recovery for the hybrid model.
Figure 3. The hybrid variant of the experiment: compiled gravity plus a learned damping correction, against MLP and Neural ODE baselines. The hybrid reaches test MSE near 10−4 while both 8,706-parameter baselines remain near 10−2, and it extrapolates more stably beyond the training horizon. The bottom-right panel is an honest caveat: with overlapping functional forms, the learned g/L absorbs part of the gravity term (a credit-assignment limitation of additive hybrids), which is why the headline numbers above come from the full-structure two-parameter model.

Heat-equation inverse problem: hard vs soft constraints

The heat equation ut = α∇²u is discretized on 10 grid points with the compiler's matrix operations, leaving a single trainable parameter, the thermal diffusivity α. The compiled and hand-coded models recover α with 0.00% error (test MSE near 10−15). The PINN, with 8,578 parameters, shows 92.7% diffusivity error: the soft physics penalty lets the network fit the data without learning the constant. This is the starkest hard-vs-soft constraint gap in the paper.

Four-panel heat equation figure: diffusivity recovery converging to the true value, test-loss curves reaching machine precision for compiled and hand-coded models, interpolation versus extrapolation bars, and the diffusion-plus-source hybrid comparison.
Figure 4. Compiled and hand-coded models (1 parameter) converge to the true α = 0.01 within hundreds of epochs and drive test loss to machine precision, while the PINN stalls near 10−1 and the MLP near 10−2; the hard-constrained models show no degradation under extrapolation, and the diffusion-plus-learned-source hybrid beats a pure MLP by 100×. The panel titles report the single run shown; the paper's tabulated PINN recovery error is 92.7%.

Composition without error accumulation

Chains of depth 2 to 6 built from eight compiled operation modules compose with exactly zero error, in-distribution and at 4× extrapolation. Chains of the corresponding trained MLP approximations accumulate errors up to 5.9 × 109: per-module approximation error is amplified by the Lipschitz constants of downstream modules.

Six-panel compositional generalization figure: composition and extrapolation error versus chain depth, worst-case error bars, per-module neural approximation error, error amplification factors, and an example composed function.
Figure 5. Exact composition vs neural approximation. Compiled and hand-coded chains sit at machine epsilon at every depth; neural chains reach errors of 109 and amplification factors above 1025 under extrapolation.

Scope, honestly stated

These are six experiments with hand-coded PyTorch, PINN, Neural ODE, and pure-MLP baselines, not a survey. For any single fixed equation the compiled module is numerically identical to a careful hand-coded implementation; the compiler's contribution is systematic generation and composability, not accuracy over hand-coding. Two of the 15 Feynman equations fail to recover constants (the harmonic oscillator's periodic loss landscape and the Lorentz factor's singularity), optimization failures rather than compilation failures. And compiled hybrid training costs 2.5 to 3.8× more wall-clock time than a pure MLP.

3 · Where it stopped

One graph per program

The Neural Compiler established that exact, differentiable structure beats soft constraints. Its own design also marked the boundary the next paper had to cross.

Limitation First-order only

The source language excludes closures, higher-order functions, and general data structures, and supports recursion only in restricted forms (tail calls compile to loops; other recursion runs under a bounded-depth dispatch). Full Scheme programs are out of reach.

Limitation Programs are the compiled artifact

Every new equation is staged into its own graph. The program stops being data the moment it is compiled, so exploring many candidate structures means one compilation per candidate, and nothing at runtime can propose or rewrite programs.

The observation that became Paper 2

The compiler is expressive enough to compile something more useful than any single model: a Scheme interpreter written in Scheme. Compile that interpreter once and every program it runs becomes data, inheriting differentiability with no recompilation and no per-program reimplementation. That is DMCI, Paper 2 of this program; the full arc is traced in the program lineage.

Resources

Read it, run it, cite it

The paper, the open-source compiler, and the next chapter of the program.

Cite this paper

@article{sheneman2026neuralcompiler,
  title         = {The Neural Compiler: Program-to-Network Translation
                   for Hybrid Scientific Machine Learning},
  author        = {Sheneman, Lucas},
  year          = {2026},
  journal       = {arXiv preprint arXiv:2605.22498},
  eprint        = {2605.22498},
  archivePrefix = {arXiv}
}
Brainstorm