Skip to content

Qwen3-0.6B inference (12 KB), bare-metal bootable OS (22 KB), and from-scratch Mini-GPT trainer with live Braille TUI (32 KB) in 100% pure x86-64 NASM assembly. Zero C, zero libc, zero dependencies.

Exit with an error on CPUs without AVX2, FMA or F16C

Jordan Ritterf7689b9b6498
diff --git a/01-qwen3-inference/README.md b/01-qwen3-inference/README.md
index 8b4225a..2f49fd7 100644
--- a/01-qwen3-inference/README.md
+++ b/01-qwen3-inference/README.md
@@ -231,7 +231,7 @@ python3 scripts/test_math.py build/test_math # after make test-math AVX1=1
- `test_math.py` covers the matmul with K = 160 (one 128-byte 4-block body and one 32-byte tail) over rows 0–5 and 5–8 (the `FAST_Q8=0` 4-row body and 1-row tail), for v1 and v2 scales, in all four build modes.
- Results do not depend on the thread count. They are not bit-exact against the AVX2 build: without FMA a multiply and an add round twice. With `FAST_Q8=0` that stays at fp32 rounding; with `FAST_Q8=1` activations are re-quantized to int8, so a rounding difference can move a value to the next integer and greedy text can part from the AVX2 build's after some tokens.
- `make train AVX1=1` stops with an error: Phase 3 keeps its AVX2 + FMA kernels.
-- Verified under `qemu-x86_64 -cpu SandyBridge` (full Linux ELF forward pass) and `qemu-system-x86_64 -accel tcg -cpu SandyBridge -smp 2` (OS boots, all APs online, chat answers); the stock build stops there with SIGILL / `[FATAL] CPU lacks AVX2+FMA+XSAVE`.
+- Verified under `qemu-x86_64 -cpu SandyBridge` (full Linux ELF forward pass) and `qemu-system-x86_64 -accel tcg -cpu SandyBridge -smp 2` (OS boots, all APs online, chat answers); the stock build stops there with `error: CPU lacks AVX2, FMA or F16C support` (exit 4) / `[FATAL] CPU lacks AVX2+FMA+XSAVE`. `python3 scripts/test_cpu_check.py` runs the Linux ELF's startup check of either build under `qemu-x86_64` CPU models.
## Live Generation Samples (`build/asm-llm`, `-t 32`)
diff --git a/01-qwen3-inference/main.asm b/01-qwen3-inference/main.asm
index 0f0ffe7..e618a9f 100644
--- a/01-qwen3-inference/main.asm
+++ b/01-qwen3-inference/main.asm
@@ -35,6 +35,13 @@ extern model_init
extern model_free
extern transformer_forward
+; CPUID.1:ECX bits the build needs; OSXSAVE(27) and AVX(28) in both builds
+%if AVX1
+CPU_NEED_ECX equ 0x18080200 ; SSSE3(9) | SSE4.1(19) | OSXSAVE(27) | AVX(28)
+%else
+CPU_NEED_ECX equ 0x38001000 ; FMA(12) | OSXSAVE(27) | AVX(28) | F16C(29)
+%endif
+
section .text
global _start
@@ -45,6 +52,35 @@ _start:
lea r13, [rsp + 8] ; argv
and rsp, -16 ; 16-byte align stack for all calls
+ ; Require the build's instruction sets with YMM state enabled by the OS
+ ; default: AVX2 + FMA + F16C AVX1=1: AVX + SSSE3 + SSE4.1
+ xor eax, eax
+ cpuid
+%if AVX1
+ test eax, eax ; leaf 1 needed
+ jz .err_cpu
+%else
+ cmp eax, 7 ; leaf 7 needed for AVX2
+ jb .err_cpu
+%endif
+ mov eax, 1
+ cpuid
+ and ecx, CPU_NEED_ECX
+ cmp ecx, CPU_NEED_ECX
+ jne .err_cpu
+ xor ecx, ecx
+ xgetbv ; XCR0: SSE(1) | AVX(2) state
+ and eax, 6
+ cmp eax, 6
+ jne .err_cpu
+%if AVX1 == 0
+ mov eax, 7
+ xor ecx, ecx
+ cpuid
+ test ebx, 0x20 ; AVX2(5)
+ jz .err_cpu
+%endif
+
; Initialize default CLI options
mov qword [rel opt_model], 0
lea rax, [rel def_prompt]
@@ -526,6 +562,13 @@ _start:
mov edi, 3
call sys_exit_group
+.err_cpu:
+ mov edi, 2
+ lea rsi, [rel msg_err_cpu]
+ call write_cstr
+ mov edi, 4
+ call sys_exit_group
+
; ============================================================================
; void run_tokens(uint64 n_prompt in rdi)
; Runs prefill (from g_chat_pos) + streaming decode + optional KV-cache close
@@ -990,6 +1033,11 @@ msg_think_on: db "[mode] think=1", 10, 0
msg_think_off: db "[mode] think=0", 10, 0
msg_err_model: db "error: failed to load .asmbin model", 10, 0
msg_err_prompt: db "error: empty prompt", 10, 0
+%if AVX1
+msg_err_cpu: db "error: CPU lacks AVX, SSSE3 or SSE4.1 support", 10, 0
+%else
+msg_err_cpu: db "error: CPU lacks AVX2, FMA or F16C support", 10, 0
+%endif
msg_usage:
%if AVX1
db "asm-llm (pure x86-64 AVX1 Qwen3-0.6B Q8_0, zero libc)", 10
diff --git a/scripts/test_cpu_check.py b/scripts/test_cpu_check.py
new file mode 100644
index 0000000..ae64798
--- /dev/null
+++ b/scripts/test_cpu_check.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+"""Check the Phase 1 startup CPU feature test under qemu-user CPU models.
+
+ make inference [AVX1=1]
+ python3 scripts/test_cpu_check.py [build/asm-llm]
+
+Runs `asm-llm -h` under `qemu-x86_64 -cpu <model>`: usage (exit 1) when the
+CPU has what the build needs, the CPU error (exit 4) when it does not. The
+build mode (default or AVX1=1) is read from the usage banner.
+"""
+
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import unittest
+
+
+BINARY = Path(__file__).resolve().parents[1] / "build/asm-llm"
+QEMU = shutil.which("qemu-x86_64") or shutil.which("qemu-x86_64-static")
+
+MSG_AVX2 = b"error: CPU lacks AVX2, FMA or F16C support\n"
+MSG_AVX1 = b"error: CPU lacks AVX, SSSE3 or SSE4.1 support\n"
+
+# cpu model -> (runs the default build, runs the AVX1=1 build)
+CPUS = {
+ "Haswell": (True, True),
+ "Skylake-Client": (True, True),
+ "SandyBridge": (False, True),
+ "IvyBridge": (False, True),
+ "Nehalem": (False, False),
+ "Westmere": (False, False),
+ "Haswell,-avx2": (False, True),
+ "Haswell,-fma": (False, True),
+ "Haswell,-f16c": (False, True),
+ "Haswell,-xsave": (False, False), # no OSXSAVE: YMM state not enabled
+ "SandyBridge,-ssse3": (False, False),
+ "SandyBridge,-sse4.1": (False, False),
+ "SandyBridge,-xsave": (False, False),
+}
+
+
+def run(cpu):
+ return subprocess.run([QEMU, "-cpu", cpu, BINARY, "-h"],
+ capture_output=True, timeout=30)
+
+
[email protected](QEMU, "qemu-x86_64 (qemu-user) not found")
+class CpuCheckTests(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.avx1 = b"asm-llm (pure x86-64 AVX1 " in run("Haswell").stderr
+
+ def test_cpu_models(self):
+ message = MSG_AVX1 if self.avx1 else MSG_AVX2
+ for cpu, runs in CPUS.items():
+ with self.subTest(cpu=cpu, avx1=self.avx1):
+ result = run(cpu)
+ if runs[self.avx1]:
+ self.assertEqual(result.returncode, 1, result.stderr)
+ self.assertIn(b"Usage: asm-llm", result.stderr)
+ else:
+ self.assertEqual(result.returncode, 4, result.stderr)
+ self.assertTrue(result.stderr.endswith(message), result.stderr)
+ self.assertEqual(result.stdout, b"")
+
+
+if __name__ == "__main__":
+ if len(sys.argv) > 1 and not sys.argv[1].startswith("-"):
+ BINARY = Path(sys.argv.pop(1))
+ unittest.main()