handwired README

The project's README from github.com/chenglou/handwired. As one markdown file: README.md.

Handwired

Turn your regular JavaScript code into a neural net; no training, exact, yet also fully differentiable!

How It Works

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:

Play With It

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)

HandwiredOS

handwired (chenglou/handwired)

Searching Database…

What is this?Handwired is a research tool that converts ordinary JavaScript code (if-else, loops, state) into an equivalent neural network made only of matrix multiplication and ReLU, so the same logic can run as a differentiable, GPU-friendly program instead of a normal single-threaded script.
Is this a ready-made library or more of an experimental technique?It is closer to an experimental transformation technique with demos: it compiles your program's logic into matmul+ReLU weights by hand (no training needed to be correct), and ships a lab/ folder described as old experiments plus docs/, src/, and a template/ to build on.
How do I run the demos?Clone the repo, run `bun install`, then `bun start` to launch the live demos locally, and check docs/, src/, and template/ to get started.
Do I need Bun installed first?Yes, the project is run with Bun (bun install, bun start), not plain npm or node.
Is this stable enough to drop into a real project?No, it reads as a research playground: the lab/ directory is explicitly old experiments you can 'check or ignore', suggesting the project is exploratory rather than a polished, stable library.
Why turn code into a neural net if the weights aren't trained?Because once your program is just matmul + ReLU, you get a fully differentiable version of ordinary JS logic, which you can later unfreeze and train, attach to another neural net for end-to-end training, or run with GPU-level parallelism even though it was written single-threaded.
Does it actually run on the GPU?Yes, after collecting the additions and ReLUs into one large matmul, the result can be executed either in plain JS or on WebGPU for experimentation.
What is the JSON search demo?It is one of the included demos showing the GPU-parallelism benefit: a program written in normal single-threaded JS style searches over 100k JSON documents in milliseconds once converted to the matmul form.
Can I feed it my own JS program?The repo includes a template/ folder intended as a starting point for wiring up your own code, alongside docs/ and src/ for the transformation itself.
Creator
chenglou
Size
15.1 MB
Stars
1.7K
Updated
CommitOct 17
Age
3d
Commits · 90 Days