Pure x86-64 Assembly LLM Runs Qwen3 at 126 Tokens per Second

Original title: Bare Metal Qwen
78AI Score

The llm.asm project delivers three complete LLM implementations written entirely in x86-64 NASM assembly, with no C, C++, Rust, libc, or runtime dependencies. Its Qwen3-0.6B inference binary is just 14,712 bytes and reaches 126.8 tok/s decode at 40 threads, claimed to be 18% to 36% faster than llama.cpp CPU Q8_0 across 1 to 40 threads while matching PyTorch FP32 on 64/64 greedy tokens. The codebase also hand-writes math kernels, an AVX2+FMA+F16C GEMM engine, a BPE tokenizer, a futex thread pool, and a bare-metal SMP kernel.

Full Report

Contents6 sections

llm.asm

Three complete LLM projects written in 100% pure x86-64 NASM assembly. Zero C, zero C++, zero Rust, zero libc, zero libm, zero runtime dependencies.

Assembled directly with nasm and linked with ld. Every math kernel (expf, sincosf, RMSNorm, RoPE, SiLU, Softmax), AVX2+FMA+F16C GEMM/GEMV engine, BPE tokenizer, clone()/futex() thread pool, bare-metal SMP kernel, analytical backward pass, AdamW optimizer, and live Unicode Braille TUI is hand-written x86-64 assembly (17,252 lines total across all three projects).

The Trilogy

ProjectBinarySizeDepsKey Numbers
01-qwen3-inference/build/asm-llm14,712 B (14,008 B FAST_Q8=0)0Static Linux ELF running Qwen/Qwen3-0.6B (Q8_0, built-in BPE tokenizer, GQA + QK-Norm + RoPE + SwiGLU, single-shot & --chat multi-turn KV-cache REPL). FAST_Q8=1 (default): 126.8 tok/s decode at 40T (20.7 tok/s at 1T), +18% to +36% faster than llama.cpp CPU (Q8_0) across 1..40T, 0.99965 cosine & 64/64 exact greedy tokens vs PyTorch FP32.
02-baremetal-os/build/asm-llm-os.elf24,252 B (23,612 B FAST_Q8=0)0Bootable bare-metal Qwen3-0.6B OS with no Linux or Windows. Multiboot 1 -> Long Mode -> AVX2/FMA/F16C -> ACPI/APIC SMP -> VGA + PS/2 + COM1 multi-turn KV-cache chat (/clear, /think, /bench). 74–94 ms boot, 134.9 tok/s KVM EPYC (23.7 tok/s 1 vCPU, 136.6 tok/s peak), 55.4 tok/s live on Proxmox Xeon E5-2670 v3 with 0 GB disk.
03-minigpt-train/build/asm-gpt-train32,944 B0From-scratch FP32 Mini-GPT trainer and sampler. Register-blocked 6x16 AVX2+FMA SGEMM, full analytical backward pass (1.0000000000 cosine across all 21 gradient tensors vs PyTorch CPU), decoupled AdamW, 27,662 train tok/s, live 80x24 Braille TUI, val_loss 4.19 -> 1.51 on TinyShakespeare in 4 min.

Head-to-Head CPU Decode Benchmark: llm.asm vs llama.cpp (Qwen3-0.6B Q8_0)

Measured on the same machine (AMD EPYC 7642, 47C online, 256 MiB L3, DDR4, CPU-only -ngl 0), same model (Qwen3-0.6B Q8_0: 613.66 MiB .asmbin v2 FP16-scale vs 604.15 MiB GGUF Q8_0), 34 prompt tokens / 64 generated tokens, 3-run average:

Threadsllm.asm (FAST_Q8=0, FP32 act)llama.cpp (Q8_0, -ngl 0)llm.asm (FAST_Q8=1, 14.7 KB)vs llama.cppBare-Metal OS KVM (FAST_Q8=1)
17.18 tok/s16.63 tok/s20.70 tok/s1.24x (+24.5%)23.67 tok/s
428.12 tok/s51.08 tok/s69.24 tok/s1.36x (+35.6%)78.89 tok/s
853.09 tok/s80.79 tok/s103.64 tok/s1.28x (+28.3%)118.08 tok/s
1677.50 tok/s103.03 tok/s121.86 tok/s1.18x (+18.3%)134.94 tok/s
2491.88 tok/s104.11 tok/s123.72 tok/s1.19x (+18.8%)132.90 tok/s
3296.63 tok/s103.82 tok/s122.51 tok/s1.18x (+18.0%)133.75 tok/s
4098.50 tok/s105.08 tok/s125.86 tok/s (peak 126.81)1.20x (+19.8%)—

(Note: llama.cpp uses batched GEMM for prompt prefill pp34, whereas llm.asm runs token-by-token GEMV. For autoregressive decode tg64, llm.asm (FAST_Q8=1) combines INT8xINT8 AVX2 vpmaddubsw/vpmaddwd, .asmbin v2 FP16 block scales (F16C vcvtph2ps), monotonic next-row prefetch, contiguous head-major KV-cache, and parallel GQA attention — beating llama.cpp by +18%..+36% across 1..40 threads while producing bit-exact deterministic outputs across all thread counts. The bare-metal OS gains another ~10% from 2 MiB huge-page identity mapping.)


Live Output & Visual Proof

Phase 2: Booting Qwen3-0.6B Directly on Bare Metal (QEMU KVM, 16 vCPUs)

Bare-Metal Qwen3-0.6B OS VGA Console

Phase 2: Running Live on a Proxmox VE VM (0 GB Disk, Intel Xeon E5-2670 v3, 12 Cores)

Running on Real Proxmox VE VM (0 GB Disk, Intel Xeon KVM)

Phase 3: Pure-Assembly Live 80x24 UTF-8 Braille TUI (build/asm-gpt-train)

TEXT
╭──────────────────────────────────────────────────────────────────────────────╮
│ asm-gpt-train │ 100% Pure x86-64 Assembly (Zero libc, AVX2+FMA)              │
│ L=6 D=192 H=6 FF=512 V=65 │ params=2681664 │ B=16 T=128 │ threads=20         │
├──────────────────────────────────────────────────────────────────────────────┤
│ Loss Curve (0..400)    ⣿⣿⣿ train   ◆ val    best_val=1.8399                  │
│ 4.34 ┤◆   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·  │
│      │⡇                                                                      │
│ 3.61 ┤⢱   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·  │
│      │ ⢱                                                                     │
│ 2.87 ┤· ⠣⣀·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·  │
│      │    ⠑⠢⠤⣀◆                                                              │
│ 2.14 ┤·   ·   ·⠉⠉⠑⠒⠒⠢⠤⠤◆⠤⣀⣀⣀⡀⣀⣀◆  ·   · ◆ ·   ·   ·   ·   ·   ·   ·   ·   ·  │
│      │                      ⠈  ⠉⠉⠉⠊⠉⠊⠒⠊⠒⠊⠒⠔⠒⠢⠤⠤⠔⠢◆⠤⠤⠤⢄⣀⣀⢄◆⣀⣀⣀⣀⡀⣀⣀⢀◆⡀ ⡀⢀   ⣀◆ │
│ 1.40 ┤·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ·   ⠈  ⠁⠁⠈⠉⠈⠁⠉⠉⠉·⠈ │
│      └step 0 ───────────────────────── 200 ────────────────────────── 400    │
├──────────────────────────────────────────────────────────────────────────────┤
│ Step 400/400 [████████████████████████████] 100% │ elapsed 35.2s             │
│ loss=1.6604  val=1.8399  best=1.8399  gnorm=0.53  lr=0.000200  25470 tok/s   │
├─ Live Sample (step 400) ─────────────────────────────────────────────────────┤
│ ROMEO:                                                                       │
│ Nay, to lond, in if as my kears, some with death.                            │
│                                                                              │
│ DUKE VINCENTIO:                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)