Skip to main content
Back

Cyber Monday Deal: Get 20% off all AlphaSignal Pro subscriptions. Ends soon.

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.

Pro

Repo, Full Access
  • 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.

The resulting graph is piecewise differentiable. ReLU has a kink at zero, where automatic-differentiation systems use a chosen subgradient, but gradients remain available through the rest of the graph. The fixed weights need no optimization unless a developer deliberately unfreezes some of them for training.

Three demos test three paths

The live demos apply the same compiler design to user interfaces, browser training, and data-parallel search:

Demo What it does What it demonstrates
HandwiredOS Runs a small desktop interface with draggable windows, scrolling, text input, animation, and a photo grid. A network of roughly 9,909 weights across 130 layers drives regular DOM output through deterministic inference.
Learned gallery Lets users drag rectangles while selected compiled weights train in the browser. A known program can provide the initial graph, after which backpropagation can tune chosen parameters in real time.
JSON search Searches 100,000 documents using JavaScript implementations of matching, JSON parsing, and highlighting compiled for WebGPU. A regular batch workload can move from a serial loop into parallel GPU kernels, with reported execution times in milliseconds.

What the tensor form buys

  1. Trainable program parameters. Developers can begin with behavior encoded by a conventional program, freeze the weights that must remain stable, and optimize selected parameters with gradient descent. Unconstrained training can change the original behavior, so correctness-sensitive weights require freezing or explicit constraints.
  2. Composition with learned models. A compiled program can sit inside a larger differentiable graph, including a small language model such as nanoGPT. Gradients can then flow through compatible numerical interfaces while the frozen portion continues to encode fixed behavior.
  3. Access to data parallelism. Regular operations applied across many records can map well to WebGPU. The JSON demo benefits because thousands of documents follow the same computational structure, giving the GPU enough parallel work to offset dispatch and data-transfer costs.

Performance depends on workload shape, graph size, memory layout, compilation overhead, and hardware. Branch-heavy programs can create large graphs because multiple outcomes may need evaluation, while small or irregular jobs often remain better suited to a tuned CPU implementation.

The boundaries of exactness

  • Language coverage: The repository demonstrates a constrained, compiler-supported portion of JavaScript. Dynamic objects, unrestricted loops, external I/O, and other runtime behavior require explicit representations or remain outside the demonstrated scope.
  • Input domains: Exact equivalence relies on the assumptions used by each numerical encoding, such as integer inputs in the branch example.
  • Numerical behavior: Execution uses floating-point arithmetic, so precision limits and backend differences can matter for larger computations.
  • Graph growth: Control flow, state, and repeated operations can increase the number of layers and weights quickly.
  • Optimization: WebGPU exposes parallel hardware, but acceleration depends on batching, kernel efficiency, and the cost of moving data.
  • Project maturity: The author labels Handwired a toy. Production use would require broader language coverage, stable interfaces, extensive correctness tests, and benchmarks across browsers and devices.

Try the compiler locally

The MIT-licensed repository is written in TypeScript and uses Bun for installation and development. After cloning the project, the documented setup is:

git clone https://github.com/chenglou/handwired.git
cd handwired
bun install
bun start

The repository includes a tutorial under docs/ and a template/ directory for new programs. The JavaScript backend provides a CPU path, while the accelerated demos require a browser and device with suitable WebGPU support.

Handwired gives developers a concrete way to inspect one shared representation for deterministic code, GPU execution, and gradient-based optimization. Its current scope is experimental, but the compiler makes those connections testable in code rather than leaving them as an abstract equivalence.