GitHub - chenglou/handwired: Turn your regular JavaScript code into a neural net.
Short summary
Handwired converts regular JavaScript control flow (if-else, loops, state) into matrix multiplication and ReLU operations without any training required. The resulting code stays fully differentiable and runs on GPU, enabling use cases like training frozen programs end-to-end or parallelizing sequential JS code.
- Transforms conditional logic into arithmetic using ReLU tricks (e.g., if-else becomes a weighted sum of both branches selected by a binary mask), keeping everything differentiable without training weights
- Collapses all operations into matmul and ReLU primitives, which GPUs can heavily optimize through fusion, sparsity, and reordering techniques
- Enables hybrid workflows: freeze a handwired program and train it alongside a generic neural net, or run sequential JavaScript at GPU-level parallelism (demo processes 100k JSON documents in milliseconds)
Full summary
Handwired is a tool that converts regular JavaScript into differentiable neural network operations without requiring any training. The core trick is expressing control flow—if-else statements and loops—as pure arithmetic using ReLU activations. Take a simple conditional: `a > 4 ? 10 : 12`. Instead of branching, you compute both paths and use a learned gate (derived from ReLU operations on `a - 4` and `a - 5`) to select which result passes through. This transforms arbitrary program logic into matrix multiplications and ReLU operations, which can then run on either JavaScript or WebGPU. The weights stay frozen by default since they're derived directly from your original code's logic.
The payoff is that your program becomes fully differentiable while staying mathematically exact—you can verify the results by hand. Because everything reduces to just two primitives (matrix multiplication and ReLU), GPUs can optimize aggressively through fusion, sparsity, and reordering. This opens unusual possibilities: you can unfreeze those weights and train them to see what happens, or attach a frozen program block to a trainable neural network and train the whole thing end-to-end. The demos show practical speed gains too—a JSON search over 100k documents runs in milliseconds by achieving GPU-level parallelism from single-threaded JavaScript code.
The project includes live demos at chenglou.me/handwired with local setup via `git clone`, `bun install`, and `bun start`. The repository structure separates documentation and source code in `docs/` and `src/`, with a `template/` for getting started and a `lab/` folder for older experiments. This is fundamentally about collapsing the gap between traditional imperative programming and neural computation—letting you write code the normal way, then automatically unlock differentiability and GPU acceleration.