Cheng Lou's Handwired Compiles JavaScript Directly Into Neural Networks Without Training
A compiler converts if-else and loops into frozen matrix weights, producing exact, differentiable neural nets that run JavaScript programs in a browser.
Takeaways
- Handwired compiles JavaScript if-else and loops into matmul plus ReLU, producing an exact neural net with no training.
- Live demos include a mini OS, a trainable photo gallery, and 100k JSON search on WebGPU.
- Compiled programs stay fully differentiable, so you can unfreeze weights and fine-tune them with normal backprop.
- HandwiredOS runs on roughly 9,909 weights across 130 layers, rendering to regular DOM elements.
- Open-source under MIT on GitHub, built in TypeScript, run locally with bun.
- Positioned as a toy project, but a sharp demonstration that programs and neural nets form one continuum.
Handwired is an open-source experiment that lowers supported JavaScript programs into neural networks with fixed, compiler-assigned weights. The generated network reproduces the program’s logic through tensor operations and ReLU activations, runs through plain JavaScript or WebGPU, and requires no training loop or GPU.
Engineer Cheng Lou, formerly a member of the React core team, presents the project as a toy compiler for exploring the overlap between conventional programs and differentiable computation. Its output remains deterministic for supported inputs, while its tensor representation can use neural-network tooling such as automatic differentiation, kernel fusion, sparsity, and GPU scheduling.
Branches become ReLU gates
Handwired converts control flow into numerical data flow by computing possible outcomes and selecting among them with arithmetic gates. The README demonstrates the technique with a conditional expression equivalent to a > 4 ? 10 : 12:
const a = 5
// ReLU(x) is Math.max(0, x)
const isAbove4 =
Math.max(0, a - 4) - Math.max(0, a - 5)
// Compute both outcomes, then select one
const result =
isAbove4 * 10 + (1 - isAbove4) * 12
For integer inputs, isAbove4 equals 0 when a is at most 4 and 1 when a is at least 5. Both branches are evaluated, and the gate selects the required result. Inputs between 4 and 5 produce fractional values, so the identity’s exactness depends on the integer domain stated in the example.
The compiler extends this pattern across supported branches, bounded loops, and explicit state. It emits a sequence of linear operations and ReLU activations arranged as network layers, with program state carried through the graph. The JavaScript backend evaluates those layers directly, while the WebGPU path packages suitable work into GPU kernels.