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…

这是什么?Handwired 是一个研究工具,它把普通的 JavaScript 代码(if-else、循环、状态)转换成一个等价的神经网络,这个网络只由矩阵乘法和 ReLU 组成,这样同样的逻辑就能以可微分、对 GPU 友好的程序形式运行,而不是普通的单线程脚本。
这是一个现成的库,还是更像一种实验性技术?它更接近一种带有演示的实验性转换技术:它把你的程序逻辑手工编译成矩阵乘法加 ReLU 的权重(不需要训练就能保证正确),并附带一个被描述为旧实验的 lab/ 文件夹,以及 docs/、src/ 和一个可供在其基础上开发的 template/。
我怎么运行这些演示?克隆仓库,运行 `bun install`,然后运行 `bun start` 在本地启动在线演示,再查看 docs/、src/ 和 template/ 来上手。
我需要先安装 Bun 吗?是的,这个项目要用 Bun 来运行(bun install、bun start),不能用普通的 npm 或 node。
它足够稳定,可以直接用到真实项目里吗?不能,它读起来更像一个研究试验场:lab/ 目录被明确标为可以'查看或忽略'的旧实验,说明这是一个探索性的项目,而不是一个打磨完善、稳定可靠的库。
如果权重没有经过训练,为什么还要把代码变成神经网络?因为一旦你的程序只是矩阵乘法加 ReLU,你就得到了普通 JS 逻辑的一个完全可微分的版本,之后你可以解冻它并进行训练,把它接到另一个神经网络上做端到端训练,或者用 GPU 级别的并行度来运行,即使它原本是按单线程方式写的。
它真的能在 GPU 上运行吗?可以,在把所有加法和 ReLU 汇总成一个大的矩阵乘法之后,结果既可以用纯 JS 执行,也可以在 WebGPU 上运行以便做实验。
JSON 搜索演示是什么?它是自带演示之一,展示了 GPU 并行的优势:一个用普通单线程 JS 风格编写的程序,被转换成矩阵乘法形式后,可以在几毫秒内搜索 10 万份 JSON 文档。
我可以把我自己的 JS 程序喂给它吗?仓库里有一个 template/ 文件夹,作为接入你自己代码的起点,另外还有 docs/ 和 src/ 介绍这个转换本身。
Creator
chenglou
Size
15.1 MB
Stars
1.9K
Updated
CommitOct 17
Age
6d
Commits · 90 Days