The project's README from github.com/chenglou/handwired. As one markdown file: README.md.
Turn your regular JavaScript code into a neural net; no training, exact, yet also fully differentiable!
A program's made of if-else, loops, and state. Handwired transforms them into matrix multiplication + reglu over floats. For example:
const a = 5
const result = (a > 4) ? 10 : 12
The above can be expressed as basic arithmetic:
const a = 5
// Math.max(0, x) is ReLU. For an integer a, above4 is 1 if a > 4, and 0 otherwise
const above4 = Math.max(0, a - 4) - Math.max(0, a - 5)
// Both branches compute, and above4 picks one
const result = above4 * 10 + (1 - above4) * 12
You can calculate that by hand and see that both arrive at the same result.
We then collect the + and * and relu into a giant matmul, and either run it on JS or WebGPU, for fun.
Because we basically only use these 2 primitives:
This unlocks lots of funny use-cases, like:
Try the demos live: https://chenglou.me/handwired/
git clone && bun install, then bun start to run them locally. Check docs/, src/ and template/ to get started! (lab/ is a bunch of old experiments you can check or ignore)
