Skip to main content
AI tutorials

Handwired: Turn JavaScript Programs into Differentiable Neural Networks

This tutorial explains Handwired’s approach to transforming conditionals, loops, and program state into matrix multiplication and ReLU-style operations. You will set up the project with Bun, explore its demos, understand the arithmetic transformation, and learn where its differentiable programs can be useful.

Handwired: Turn JavaScript Programs into Differentiable Neural Networks

What Is Handwired?

Handwired turns regular JavaScript code into a neural-network-like computation without requiring training. Instead of representing a program as ordinary control flow, it transforms logic such as conditionals, loops, and state into matrix multiplication plus reglu operations over floating-point values.

The resulting program remains fully differentiable, while its weights are written manually rather than learned. Handwired can execute these computations in JavaScript or with WebGPU.

HandwiredOS demo

Why Convert Programs into Neural Computation?

Handwired reduces program behavior to a small set of mathematical primitives, mainly addition, multiplication, and ReLU-style operations. This approach provides several interesting properties:

  • The full program remains differentiable.
  • Weights do not need to be trained because they are written manually.
  • GPU optimizations such as fusion, sparsity, and operation reordering can work with the simplified computation.
  • Traditional JavaScript code can potentially achieve GPU-level parallelism when represented in this form.

The project also creates experimental possibilities. You can unfreeze manually defined weights and investigate what training does, or attach a frozen program to a generic neural network and train the combined system end to end.

Set Up the Project

Handwired includes live demos and the source needed to explore its implementation locally. You will need Bun installed on your system.

  1. Clone the Handwired repository.
  2. Change into the project directory.
  3. Install dependencies with bun install.
  4. Start the local demos with bun start.
git clone <handwired-repository-url>
cd handwired
bun install
bun start

After starting the project, use the local address printed by Bun to inspect the demos. You can also try the hosted demos at chenglou.me/handwired.

Understand the Core Transformation

Consider a simple JavaScript conditional:

const a = 5
const result = (a > 4) ? 10 : 12

Handwired expresses the condition with arithmetic. For an integer value, the following difference of ReLU operations produces a selector that is 1 when a > 4 and 0 when a <= 4:

const a = 5
const above4 = Math.max(0, a - 4) - Math.max(0, a - 5)
const result = above4 * 10 + (1 - above4) * 12

Both branches are computed, but above4 selects the appropriate result. With a = 5, the selector is 1, so the final value is 10, matching the original conditional expression.

How the Mathematical Representation Works

The key idea is to replace branching with continuous arithmetic primitives. A conditional becomes a combination of:

HandwiredOS

  • ReLU operations to construct a selector from the input.
  • Multiplication to apply the selector to each branch.
  • Addition to combine the branch results.

Once these operations are collected, Handwired can represent the computation as a large matrix multiplication with the relevant activation operations. The same program can then be evaluated using JavaScript or WebGPU.

Both branches compute, and the selector chooses which result contributes to the output.

Explore the Repository

When working through the project, begin with the directories highlighted in the README:

  • docs/ contains documentation and material for understanding the project.
  • src/ contains the implementation.
  • template/ provides a starting point for getting familiar with the project.
  • lab/ contains older experiments that you can explore or ignore.

A practical workflow is to run the demos first, then compare their behavior with the relevant source files. This helps connect the visible examples to the underlying transformation into matrix operations.

Try the Demos

The hosted demo is the fastest way to see Handwired in action. The project description specifically highlights a JSON search demo that processes 100,000 documents in milliseconds. It demonstrates the project’s goal of bringing substantial parallelism to code that was originally written in a traditional, single-threaded style.

Use the demos to observe the distinction between the original program structure and the generated mathematical representation. They are also a useful starting point before modifying the local source.

Advanced Ideas

Experiment with Trainable Weights

Handwired normally uses manually written weights, so training is not required to make the program work. One experiment suggested by the project is to unfreeze those weights and see what the optimization process produces.

Combine Frozen Programs with Neural Networks

Another possibility is to attach a specific frozen Handwired program to a general neural network and train the combined system end to end. This could allow a model to retain a precisely defined program while learning other behaviors.

Think in Terms of Primitive Operations

When designing experiments, focus on how control flow can be expressed using addition, multiplication, and ReLU-style operations. The simpler the resulting computation, the more directly it can benefit from techniques such as fusion, sparsity, and reordering.

Conclusion

Handwired demonstrates how ordinary JavaScript logic can be rewritten as differentiable mathematical computation. By replacing control flow with arithmetic selectors and reducing the result to matrix multiplication plus activation operations, it makes traditional programs compatible with neural-network techniques and JavaScript or WebGPU execution. Install it with Bun, explore the demos, and then inspect docs/, src/, and template/ to begin experimenting.