← all repositories
chenglou/handwired

Your if-else is secretly a neural net (and it never needs training)

Handwired compiles ordinary JavaScript into matmuls and ReLUs — exact, differentiable, and untrained by design.

★1.1k stars TypeScript ML Frameworks
handwired
Collecting fresh signals — velocity needs a few days of history.
collecting data…
star history

What it does

Handwired takes regular TypeScript/JavaScript — branches, loops, state — and rewrites it as matrix multiplication plus ReLU over floats. The trick is that comparisons become arithmetic: a > 4 turns into a difference of two clamped terms that yields exactly 1 or 0, and that indicator then blends both branches of a conditional instead of picking one. Everything collapses into one giant matmul that runs on plain JS or WebGPU.

The interesting bit

Because the entire program is built from just +, *, and ReLU, it’s fully differentiable — yet the weights are hand-written, not trained. The result is a “neural net” that computes your program exactly. The README then cheerfully suggests the obvious mischief: unfreeze the weights and train them anyway, or bolt a frozen program onto a real model like nanoGPT and train end-to-end.

Key highlights

  • Exact program semantics with zero training — weights are derived, not learned.
  • Fully differentiable, so you can attach it to a trainable network.
  • Runs on JS or WebGPU; the minimal primitive set is friendly to fusion, sparsity, and reordering.
  • A JSON search demo reportedly scans 100k documents in milliseconds, thanks to GPU-style parallelism from single-threaded-looking code.
  • Live demos are hosted on the project page.

Caveats

  • The README is a proof-of-concept in spirit: the “funny use-cases” (training the weights, nanoGPT integration) are posed as open questions, not shipped features.
  • The comparison-to-arithmetic encoding shown works for integers; how it generalizes to all real-world JS isn’t spelled out.
  • A lab/ directory of old experiments hints at plenty of churn behind the current design.

Verdict

A delightful piece of program-compiler art from chenglou — worth an hour of poking if you care about the boundary between code and models. Not a library you’d ship to production tomorrow.

Frequently asked

What is chenglou/handwired?
Handwired compiles ordinary JavaScript into matmuls and ReLUs — exact, differentiable, and untrained by design.
Is handwired open source?
Yes — chenglou/handwired is open source, released under the MIT license.
What language is handwired written in?
chenglou/handwired is primarily written in TypeScript.
How popular is handwired?
chenglou/handwired has 1.1k stars on GitHub.
Where can I find handwired?
chenglou/handwired is on GitHub at https://github.com/chenglou/handwired.

heatdrop uses Google Analytics to see which pages get read — nothing else. Your call. How we handle data.