Forge
What is it
Forge is a runtime for training and running neural networks, aiming to be the efficient and portable one: a single Rust crate and one set of WGSL kernels, with no CUDA toolchain and no Python interpreter anywhere in the loop.
Why
Running a transformer usually means Python, a CUDA toolchain, and a dependency stack tied to one vendor's hardware. Forge is a single Rust crate that trains and infers on any GPU wgpu reaches — Vulkan, Metal, D3D12, and WebGPU in a browser tab — with no CUDA, no Python interpreter, and no server round trip. That's not just a portability trick. Portable and efficient is where this is going: one runtime on every device you own, down to the edge, and eventually across them — the floor a vendor-independent runtime has to stand on before it can carry real research, and eventually local, safety-focused AI work, on hardware you control end to end.
Demo
Two pages run the runtime in a browser tab on your own GPU, built from this repository and served locally — the deployed site still describes 0.2.0:
https://github.com/user-attachments/assets/d3487d8f-40a1-4c84-9f98-d4a7c5ce1550
Run it
# Generate — the 43 MB char-level Shakespeare model ships in the repo
# Real GPT-2 124M weights — the parity run from tests/gpt2_e2e.rs
# Train from scratch
# Terminal model browser + run dashboard (tools/forge-top)
# A council of four small models, deciding one character together (tools/council)
# The two browser pages, built and served locally (tools/)
# Tests
As a library:
# The crate publishes as `forge-ml` (the name `forge` was taken on crates.io in
# 2017); the library it builds is still `forge`, so imports read `use forge::…`.
[]
= "0.3"
use ;
// assets/shakespeare_char ships in the repo; swap in models/gpt2 after
// running download_gpt2.sh — AnyTokenizer picks char or BPE by what it finds.
let device = wgpu?; // or Device::Cpu
let config = from_json?;
let model = from_safetensors?;
let tok = from_dir?;
let text = model.generate?;
The crate, and what is built on it
forge-ml is the runtime and nothing else: tensors, kernels, GPT-2, tokenizers,
serialization, and the browser bindings. One optional feature, off by default:
| Feature | What it adds | Why it's optional |
|---|---|---|
train |
reverse-mode autograd, AdamW, the nine backward kernels, and Gpt2::loss / loss_grads. Needed by examples/train_shakespeare.rs and make train. |
Forge is an inference runtime that also happens to train. cargo add forge-ml should not compile a tape you never record, and src/wasm.rs exports no training at all. Construction and serialization — Gpt2::init_random, params, save_safetensors — are not gated: they are not training, and the inference tests use them. |
= { = "0.3", = ["train"] }
Everything else lives in tools/, downstream of the runtime — a crate
or a page that depends on forge and adds no kernel, no dtype and no device
work of its own:
| Tool | What it is |
|---|---|
tools/council |
Four small GPT-2s run in parallel and merged in hidden space by an entropy router, plus the page that draws the vectors they exchange |
tools/forge-top |
A terminal model browser and run dashboard |
tools/surprise |
A page that tints text by how surprised the model was to read it |
Through 0.1.0 the first two were council and tui features of this crate.
They are separate crates from 0.3.0 because the test of what belongs in the
runtime is whether a third-party crate could have written it against the public
API — and all three could. The primitives they stand on stayed:
Gpt2::hidden_step, logits_from_hidden, wte_host, surprisal_async,
Sampler, top_probs.
On Linux, wgpu needs a Vulkan ICD — install mesa-vulkan-drivers for a
software fallback, or run scripts/setup_nvidia_vulkan.sh
for NVIDIA inside a container. Check with cargo run --release --example wgpu_probe.
Acknowledgement
Supported by the RACE Merit Allocation Scheme (RMAS), RMIT University.
License
GNU Affero General Public License v3.0 — see LICENSE.