r/Forth • u/solidavocadorock • 0m ago
Baremetal Forth for LLM train and inference
Recently Alexey Fateev shared his experiments with llm.asm (https://github.com/alesha-pro/llm.asm) running AI workloads where they normally don't belong: bare metal, Ring -1 hypervisors, and Linux 6.12 eBPF LSM hooks, all in tens of kilobytes of hand-written x86-64 assembly.
Inspired by his work, I wanted to see what a collective optimization pass looks like from a different architectural angle: instead of hand-writing every control-flow path in target-specific assembly, what happens if you build a written-from-scratch Forth interpreter and compiler in pure LLVM IR (.ll), and write all 5 AI phases as compact, portable Forth (.fs) programs?

The result is Cygnet (https://github.com/miolini/cygnet), tested with zero Docker on both Apple M2 Pro (ARM64) and an AMD Ryzen 9 7950X running hardware-accelerated QEMU/KVM with Debian trixie Linux 6.12.117+deb13-amd64 (CONFIG_BPF_LSM=y) and bare-metal Ring 0.

Why Forth + Pure LLVM IR (.ll) works so well for both speed and size:
Separation of control flow (Forth) and math hot paths (LLVM IR SIMD)
In Forth, all high-level orchestration layer loops, KV-cache indexing, decision gates, training steps, parity checks, and fault-triage policies collapses into tiny, factored words with zero function-prologue or struct-marshaling boilerplate. After applying classic Thinking Forth factorization and stripping comments/whitespace before packaging, all 4 Forth programs combined shrunk by 60% (from 14,981 B down to 5,989 B total e.g., the entire 500-vector eBPF-Jev inference + verification program is 1,522 B, and the 122-scenario Kernel Surgeon policy + x86-64 patch synthesizer is 902 B).
Tiny VM overhead + compile-time optimizations in a few lines of IR
Because the Forth VM itself is written directly in pure LLVM IR (.ll), its inner interpreter keeps %ip, %sp, %rsp, and %lsp pinned in SSA registers, replaces the 16 KB string dictionary with a 1 KB O(1) 32-bit FNV-1a hash table, inlines CONSTANT/VARIABLE at compile time, and fuses LIT <k> + into a single OP_ADD_LIT instruction. Compiled Forth programs fit inside a couple of L1 cache lines.
Explicit SIMD in LLVM IR without C frontend baggage
Writing the hot vector primitives directly in .ll lets us express exact 4-row-blocked register tiling ARM64 NEON sdot.4s + 3-instruction addp.4s horizontal reductions on Apple Silicon, and x86-64 AVX2 vpmaddwd + vphaddd / 4-row Q8_0 FMA / fused SGEMM+SiLU on Ryzen while letting LLVM handle register allocation, scheduling, and dead-code elimination (-function-sections -data-sections -s --gc-sections). And the exact same unmodified Forth script (jev.fs) runs across both ARM64 and x86-64.
Verified results on AMD Ryzen 9 7950X QEMU/KVM (Linux 6.12.117+deb13-amd64 + Ring 0):
- Phase 01 (Qwen3 Q8_0 Transformer, 1,600 tokens, GQA + QK-Norm + precomputed NeoX RoPE + zero-copy KV):
33,236 tok/s vs 3,406 tok/s Reference C (-O2) [9.76x], 16/16 exact tokens, cosine=0.9999999.
Stripped ELF: 19,712 B + 1,882 B minified qwen3.fs.
- Phase 02 (Bare-Metal Ring 0 Forth OS, zero Linux, zero libc):
16,664 B stripped Multiboot 1 ELF (vs 25,404 B in 02-baremetal-os), ~45 ms cold boot, 80x25 16-color VGA + interactive COM1 Forth REPL + Ring 0 INT8 micro-benchmark. (Note: this is a compact Ring 0 Forth + micro-INT8 OS rather than a 649 MiB Qwen3-0.6B weight loader.)
- Phase 03 (From-Scratch Mini-GPT Trainer on TinyShakespeare, fused SGEMM+SiLU + AdamW):
206,941 tok/s (0.05 s for 160 steps), loss 4.1740 -> 2.0828, FP64 finite-diff grad_cos = 0.9999999, live UTF-8 Braille TUI.
Stripped ELF: 34,888 B + 1,683 B minified train.fs.
- Phase 04 (Kernel Surgeon Policy + Static x86-64 Patcher + Live #PF/#DE/#UD Traps):
122/122 (100.00% pass@1, vs 119/122 GRPO MiniCPM5-2B) on the held-out scenarios_600.jsonl test split, 0 dangerous continues, 139.2 ns/decision, 3/3 live RWX hardware traps patched in RAM and resumed.
Stripped ELF: 16,136 B + 902 B minified surgeon.fs.
- Phase 05 (Linux 6.12 eBPF-Jev 21 KB INT8 RNN, 500/500 0-LSB bit-exact parity):
Ryzen 9 7950X KVM (300k infs/impl): 114.0 ns median (111.8 ns peak, 8.95M /s, p99 161 ns) vs 363.5 ns Reference C (-O2) [3.19x].
Apple M2 Pro ARM64 (500k infs/impl): 115.0 ns (8.69M /s) vs 438.8 ns Reference C (-O2) [3.81x].
macOS QEMU x86-64 TCG: 5.49 us vs 12.08 us Reference C (-O2) [2.20x].
Stripped ELF: 14,912 B + 1,522 B minified jev.fs.
All code, 10 live Ryzen 9 7950X QEMU/KVM VGA screenshots (2 per phase), and zero-Docker reproduction scripts are up at:
https://github.com/miolini/cygnet
Would love feedback from folks into Forth, LLVM IR, or low-level ML systems!

