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.

2.4 KB
Raw Blame
#!/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)


@unittest.skipUnless(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()