llm.asm Runs Qwen3 35% Faster Than llama.cpp in Pure Assembly
A single developer shipped Qwen3-0.6B inference, a bootable OS, and a GPT trainer in pure x86-64 assembly, beating llama.cpp by up to 36%.
Takeaways
- llm.asm ships three projects written in pure x86-64 NASM assembly with zero dependencies, under MIT license on GitHub.
- A 14 KB Linux ELF runs Qwen3-0.6B 18 to 36 percent faster than llama.cpp CPU inference.
- A 24 KB bare-metal OS boots in under 94 ms and hits 134.9 tok/s on EPYC KVM with no Linux underneath.
- A 32 KB from-scratch GPT trainer matches PyTorch gradients at cosine 1.0 across all 21 tensors.
- Outputs are bit-exact deterministic from 1 to 40 threads, with 64/64 greedy tokens matching PyTorch FP32.
- Needs an x86-64 CPU with AVX2, FMA, and F16C, plus nasm and binutils to build.
The GitHub project llm.asm implements Linux inference, a bootable model kernel, and GPT training in roughly 17,000 lines of hand-written x86-64 NASM assembly. Its repository reports that the Linux inference binary runs Qwen3-0.6B between 18.3% and 35.6% faster than llama.cpp in a CPU benchmark on an AMD EPYC 7642. The project provides a focused example of how model-specific kernels, memory layouts, and thread partitioning can outperform a portable runtime.
The programs contain no C or Rust code and link neither libc nor libm. Their small executable sizes cover code only: the quantized Qwen model remains a separate 614 MiB file, and the trainer also requires external data.
| Component | Size | Purpose |
|---|---|---|
| Linux inference | 14,712 bytes | Static ELF with byte-pair encoding, grouped-query attention, QK-Norm, RoPE, SwiGLU, a key-value cache, and a multi-turn chat REPL. |
| Bare-metal kernel | 24,252 bytes | Boots the same model through Multiboot 1 and provides VGA, PS/2 keyboard, and serial interfaces. |
| GPT trainer | 32,944 bytes | Trains a Mini-GPT from initialization with a hand-written backward pass, AdamW, and a terminal loss display. |
The Reported CPU Lead
The project compares both runtimes on an AMD EPYC 7642 using Qwen3-0.6B with Q8_0 quantization. Every reported thread count favors the assembly implementation.
| Threads | llama.cpp | llm.asm | Speedup |
|---|---|---|---|
| 1 | 16.63 tok/s | 20.70 tok/s | 24.5% |
| 4 | 51.08 tok/s | 69.24 tok/s | 35.6% |
| 8 | 80.79 tok/s | 103.64 tok/s | 28.3% |
| 16 | 103.03 tok/s | 121.86 tok/s | 18.3% |
| 40 | 105.08 tok/s | 125.86 tok/s | 19.8% |
The largest relative gain appears at four threads. Both implementations flatten between 16 and 40 threads, a pattern consistent with memory-bandwidth or synchronization limits on this workload.
The benchmark covers one CPU, model, quantization format, and decoding path. A rigorous reproduction also needs the exact llama.cpp revision, build flags, NUMA placement, CPU affinity, prompt length, generated token count, warm-up policy, and sampler configuration. The figures above are project-reported results for the stated setup.
How the Fast Path Works
Autoregressive decoding generates one token at a time and repeatedly moves model weights through matrix-vector operations. llm.asm keeps the transformer architecture conventional while specializing those operations for AVX2-era x86 processors:
- Integer dot products: INT8 values are processed with the AVX2
vpmaddubswandvpmaddwdinstructions, which multiply and accumulate several packed values per instruction. - Scale conversion: F16C
vcvtph2psexpands FP16 block scales into FP32 values for computation. - Predictable prefetching: The decoder prefetches the next weight row in monotonically increasing memory order.
- Cache locality: The key-value cache stores attention data in contiguous, head-major order.
- Parallel attention: Grouped-query attention work is divided across threads without changing the generated result.
The bare-metal build reports a further gain of roughly 10% from identity-mapped 2 MiB pages. Larger pages reduce translation lookaside buffer pressure, allowing the processor to cover more model memory with fewer address-translation entries.
A 24 KB Kernel Around a 614 MiB Model
The bare-metal kernel boots through Multiboot 1, enters x86-64 long mode, configures AVX2, FMA, and F16C support, and discovers additional processor cores through ACPI and APIC. It exposes a chat prompt through VGA and PS/2 hardware or a serial console.
The 614 MiB quantized model is supplied as a Multiboot module and mapped directly into physical memory. The kernel contains no filesystem, userspace, or general-purpose scheduler, and commands such as /clear, /think, and /bench are compiled into the executable. A virtual disk is therefore unnecessary.
The repository reports boot times between 74 and 94 ms, inference at 134.9 tokens per second on an EPYC-backed KVM environment, and 55.4 tokens per second on a Proxmox VM using a Xeon E5-2670 v3 with no virtual disk attached.
Backprop, Down to the Registers
The 32,944-byte training program includes a register-blocked 6-by-16 AVX2 and FMA SGEMM kernel, which performs single-precision matrix multiplication. It also implements the complete analytical backward pass and a decoupled AdamW optimizer without calling a machine-learning framework.
For validation, the project compares all 21 gradient tensors with a PyTorch CPU reference and reports cosine similarity of 1.0000000000 for each tensor. Cosine similarity measures directional agreement and can round to that displayed value despite element-level differences, so it should not be interpreted as proof of bit-for-bit equality.
The trainer reportedly processes 27,662 tokens per second and reduces TinyShakespeare validation loss from 4.19 to 1.51 in four minutes. An 80-by-24 terminal interface uses Braille characters to draw the loss curve during training. Throughput and convergence depend on the repository’s model configuration, processor, dataset preparation, and optimization settings.
Exact Tokens Across 40 Threads
The fast INT8 inference path reports cosine similarity of 0.9996517 against PyTorch FP32 output and matches all 64 greedy token choices in the test sequence. Runs from one through 40 threads produce bit-exact output, indicating that work partitioning does not alter reduction order or token selection.
The bare-metal and Linux builds also produce byte-for-byte identical output for the tested English, Russian UTF-8, arithmetic, and reasoning prompts. These checks cover selected regression cases and a 64-token greedy sequence; broader task-quality evaluations are outside the supplied results.
From Clone to First Token
Building the Linux inference program requires a GNU/Linux host, NASM, GNU binutils, make, and an x86-64 processor exposing AVX2, FMA, and F16C. Python 3 and the exporter’s package dependencies are required to convert the original model weights.
git clone https://github.com/alesha-pro/llm.asm.git
cd llm.asm
make all
# Export Qwen3-0.6B to the custom .asmbin format
python3 scripts/export_weights.py \
--model-dir models/qwen3-0.6b-bf16 \
--out models/qwen3-0.6b-q8_0.asmbin \
--scale-dtype fp16
./build/asm-llm \
-m models/qwen3-0.6b-q8_0.asmbin \
--chat \
-t 16
Python leaves the execution path after export. The inference binary then loads the custom .asmbin file directly, while the bare-metal build receives the same file as a Multiboot module. Booting that version requires a compatible bootloader or virtual machine configuration.
The repository code uses the MIT license. The Qwen weights retain their separate model license, and a deployment needs memory for the 614 MiB weight file, the key-value cache, working buffers, and the host or virtualized environment.
What Developers Can Reuse
- Specialization can recover measurable CPU throughput. For this fixed model and processor, hand-written kernels beat the reported portable-runtime baseline by 18.3% to 35.6%.
- Memory layout matters alongside arithmetic. Prefetch order, contiguous attention state, and block-scale decoding all contribute to the result.
- Thread determinism can be designed explicitly. Fixed partitioning preserves identical output across the tested thread counts.
- Executable size needs deployment context. The 14 KB and 24 KB artifacts still operate on a 614 MiB model.
- Bare metal provides a bounded comparison. The reported 10% gain quantifies huge-page and operating-system effects for this environment rather than establishing a universal hardware result.
Where the Specialization Ends
The implementation targets a narrow x86-64 CPU workload and one model family. It lacks a GPU path, request batching, broad model compatibility, and the operational features expected from a production serving system, including observability, fault isolation, security hardening, and rolling upgrades. For inference and kernel engineers, its main value is the concrete demonstration of how SIMD instruction selection, cache layout, prefetching, and deterministic threading affect small-model performance.