Bare Metal Qwen

Hacker News by 8 min read 48x views
Bare Metal Qwen

Share Post

A standalone bootable x86-64 functioning scheme that runs Qwen/Qwen3-0.6B immediately on hardware or KVM alongside no Linux, no Windows, no C, no Rust, and no libc.

  • 24,252 bytes stripped fixed Multiboot 1 kernel (FAST_Q8=1, default: .text = 17,143 B, .rodata = 2,772 B, .bss = 2.1 MiB NOBITS, zero energetic dependencies), or 23,612 bytes (FAST_Q8=0, bequest Q8_0xFP32).
  • 74–94 ms chilly boot to interactive asm-llm> multi-turn conversation alongside persistent KV-cache throughout turns, all SMP cores online, and the .asmbin v2 (614 MiB FP16 scales) or v1 (649 MiB FP32 scales) example border zero-copy from bodily RAM on 2 MiB huge pages.
  • 100% token-exact and byte-for-byte parity alongside Phase 1 throughout 1..32 vCPUs.
  • 134.9 tok/s decode on QEMU KVM (AMD EPYC 7642, 16 vCPUs; 23.7 tok/s at 1 vCPU, 118.1 tok/s at 8 vCPUs, 136.6 tok/s peak) and 55.4 tok/s live on a 0 GB disk Proxmox VE VM (Intel Xeon E5-2670 v3, 12 cores).

Bare-Metal Qwen3-0.6B OS VGA Console

Running live on a diskless (0 GB storage) Proxmox VE VM booted from the hybrid BIOS+UEFI ISO (Intel Xeon E5-2670 v3, 12 cores, 2 GiB RAM):

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


  • x86-64 CPU alongside AVX2 + FMA + F16C.
  • Core build & QEMU KVM:
    sudo apt instal nasm binutils qemu-system-x86
  • Optional, for construction a bootable BIOS+UEFI hybrid ISO (make os-iso):
    sudo apt instal grub-pc-bin grub-efi-amd64-bin grub-common mtools xorriso
  • An exported models/qwen3-0.6b-q8_0.asmbin document (--scale-dtype fp16 v2 recommended, or bequest v1 --scale-dtype fp32; see Phase 1 setup).

From the repository base (FAST_Q8=1 is enabled by default; continue FAST_Q8=0 for bequest FP32-activation mode):

make os FAST_Q8=1 make os-check

Expected output (FAST_Q8=1):

-rwxr-xr-x 1 person user 24252 build/asm-llm-os.elf build/asm-llm-os.elf: ELF 32-bit LSB executable, Intel 80386, type 1 (SYSV), statically linked, stripped There is no energetic division in this file. 

(The kernel is connected as 64-bit ELF and wrapped into ELF32 via objcopy -O elf32-i386 for norm Multiboot 1 loaders akin QEMU -kernel and GRUB2; boot.asm transitions immediately into 64-bit Long Mode.)


Option A: Instant QEMU KVM Boot (~85 ms)

Boot immediately in the terminal complete COM1 serial (-nographic):

qemu-system-x86_64 -enable-kvm -cpu presenter -m 2G -smp 16 \ -kernel build/asm-llm-os.elf \ -initrd models/qwen3-0.6b-q8_0.asmbin \ -nographic

Or use the Makefile shortcut:

make os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16

Run a one-shot lot immediate alongside spotless ACPI shutdown:

make os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16 \ CMDLINE='prompt="Explain how a CPU cache plant in 3 concise sentences." max_tokens=64 think=0 shutdown=1'

Open a live 80x25 VGA GUI window alongside PS/2 keyboard input:

qemu-system-x86_64 -enable-kvm -cpu presenter -m 2G -smp 16 \ -kernel build/asm-llm-os.elf \ -initrd models/qwen3-0.6b-q8_0.asmbin \ -vga std -serial stdio

Option B: Bootable Hybrid BIOS+UEFI ISO

Packages the 23 KB kernel, GRUB2 Multiboot 1 config, and the 649 MiB .asmbin example into a sole self-contained ~661 MiB bootable ISO (build/asm-llm-os.iso):

make os-iso MODEL=models/qwen3-0.6b-q8_0.asmbin

Boot the ISO in QEMU KVM (SeaBIOS or UEFI OVMF):

# Legacy BIOS (SeaBIOS) qemu-system-x86_64 -enable-kvm -cpu presenter -m 2G -smp 16 \ -cdrom build/asm-llm-os.iso -nographic # UEFI (OVMF) qemu-system-x86_64 -enable-kvm -cpu presenter -m 2G -smp 16 \ -bios /usr/share/ovmf/OVMF.fd \ -cdrom build/asm-llm-os.iso -nographic

Option C: Deploying on Proxmox VE (0 GB Disk VM)

Because the complete OS and example live inner the ISO and run out of RAM, the VM needs zero difficult disks.

  1. Upload build/asm-llm-os.iso to your Proxmox node's ISO retention (local -> ISO Images -> Upload, or scp into /var/lib/vz/template/iso/asm-llm-os.iso).
  2. Create and commencement a diskless VM on the Proxmox presenter (pick an unused <vmid>, e.g. 250):
qm create 250 \ --name asm-llm-os \ --machine pc \ --bios seabios \ --cpu presenter \ --cores 12 \ --memory 2048 \ --balloon 0 \ --vga std \ --serial0 socket \ --ide2 local:iso/asm-llm-os.iso,media=cdrom \ --boot order=ide2 qm commencement 250
  1. Interact either through the Proxmox web UI (noVNC Console, VGA + PS/2 keyboard) or complete the virtual serial port:

(Note: --cpu presenter is required so the VM exposes AVX2 + FMA, and --balloon 0 ensures 2 GiB of adjacent visitant RAM for the 649 MiB example + 449 MiB KV-cache arena.)


Interactive Multi-Turn Chat & Kernel Cmdline

Interactive Console (asm-llm>)

By default, the bare-metal console runs in multi-turn conversation manner alongside persistent KV-cache: all new person rotate prefills lone the new Qwen3 conversation tokens starting at g_chat_pos without recomputing before turns, closes all aide rotate in the KV-cache alongside <|im_end|>\n, and displays live environment use (ctx=X/2048) in the decode stats line. When the KV-cache approaches max_seq (pos + n_prompt + 64 >= max_seq), it prints [chat] environment full, resetting KV-cache and resets cleanly. If booted alongside prompt="..." and shutdown=0, the first lot rotate stays in the KV-cache so you can continue the identical conversation in the REPL.

Both PS/2 keyboard (VGA) and COM1 serial (115200 8N1) are polled simultaneously. Type any communication and media Enter, or use a built-in command:

Command Action
/clear, /new, /reset Clear conversation former (g_chat_pos = 0, prints ctx=0/2048)
/think Enable Qwen3 <think>...</think> reasoning mode
/nothink Disable reasoning manner (direct answers, default)
/bench Reset KV-cache and run built-in latency/throughput benchmark prompt
/halt or /quit Trigger spotless ACPI S5 poweroff
/help Show accessible console commands

Multiboot Kernel Cmdline Options

Pass via QEMU -append "...", create os-run CMDLINE='...', or create os-iso CMDLINE='...':

Option Default Description
prompt="..." (none) Run a lot immediate immediately following footwear (preserved in KV-cache if shutdown=0)
max_tokens=N 128 Maximum tokens to create per prompt
ctx=N 2048 KV-cache environment length
threads=N 0 Limit energetic SMP cores (0 = all online cores)
think=0|1 0 Enable or disable Qwen3 reasoning mode
raw=0|1 0 Skip Qwen3 conversation template
single_turn=0|1 0 Disable multi-turn KV-cache preservation (0 = multi-turn conversation by default)
temp=F 0.0 Sampling heat (0.0 = greedy)
topp=F 0.95 Top-p threshold
seed=N 42 PRNG seed
print_tokens=0|1 0 Print PROMPT_TOKENS: and GEN_TOKENS: markers
shutdown=0|1 0 Power off via ACPI following lot immediate completes

Phase 2 compiles the exact identical shared/math.asm, shared/matmul.asm, shared/tokenizer.asm, and shared/transformer.asm alongside -DBARE_METAL -DFAST_Q8=$(FAST_Q8) and pairs them alongside 4,167 lines of bare-metal OS code:

  • Boot & Long Mode (boot.asm, linker.ld): verifies Multiboot 1 magic (0x2BADB002), zeroes .bss, builds 4-level 2 MiB huge-page character tables covering 0..4 GiB (with 0xFE000000..0xFFFFFFFF marked Strong Uncacheable for Local APIC MMIO), enters 64-bit Long Mode, installs a 32-entry elimination IDT, verifies XSAVE + AVX + AVX2 + FMA + F16C via CPUID, and initializes CR0/CR4/XCR0=7/MXCSR.
  • Zero-Copy Weights & Physical Arena (runtime.asm): binds the .asmbin (v2 FP16 scales or v1 FP32 scales) Multiboot component immediately at its bodily location (validating "ASML" magic) and carves the 449 MiB KV-cache + scratch arena from the largest usable Multiboot memory-map region, zeroing it alongside AVX2.
  • Bare-Metal SMP (smp.asm): parses ACPI RSDP -> RSDT/XSDT -> MADT ("APIC") alongside a Local APIC broadcast fallback, installs a position-independent 16->32->64-bit trampoline at 0x8000, wakes all AP cores via Local APIC INIT-SIPI-SIPI, enables AVX2/FMA/MXCSR identically on all core, and dispatches the two matmul_q8_0 and gqa_attn_range (FAST_Q8=1) alongside a lock-free cache-line-isolated spin-barrier.
  • Hardware Drivers (drivers.asm):
    • COM1 Serial (0x3F8, 115200 8N1): raw UTF-8 TX and RX.
    • 80x25 Color VGA (0xB8000): colored footwear banner, hardware scrolling, batched CRTC cursor updates, and UTF-8 decoding alongside Cyrillic-to-ASCII fallback on VGA during streaming raw UTF-8 to COM1.
    • PS/2 Keyboard (0x60/0x64): scancode set 1 to ASCII (Shift, Backspace, Enter).
    • PIT Channel 2 + RDTSC: 20 ms hardware PIT calibration for exact nanosecond boot/prefill/decode profiling.
    • ACPI Poweroff: QEMU/Bochs ACPI S5 ports (0x604, 0xB004, 0x4004) affirmative isa-debug-exit fallback.

Verification & Benchmarks

Automated Verification Suite

python3 scripts/verify_os.py --model models/qwen3-0.6b-q8_0.asmbin --run-dir runs/os-verify

All 5 parity prompts (English technical, English concise, Russian UTF-8, arithmetic term problem, --think mode) equivalent Phase 1 (build/asm-llm) 100% exact in immediate tokens, generated tokens, and raw UTF-8 bytes.

KVM SMP Scaling (FAST_Q8=1, .asmbin v2 FP16 Scales, AMD EPYC 7642, 34 immediate tok, 64 gen tok, greedy)

vCPUs (KVM) SMP Online Boot Ready (ms) Boot -> 1st Tok (ms) Prefill (ms) Prefill (tok/s) Decode (ms) Decode (tok/s) Parity vs Phase 1
1 1/1 73.47 1161.48 1071.98 31.72 2685.04 23.46 (peak 23.67) Exact (64/64)
4 4/4 84.10 425.11 324.82 104.67 805.95 78.17 (peak 78.89) Exact (64/64)
8 8/8 84.54 316.71 215.93 157.46 529.53 118.97 Exact (64/64)
16 16/16 84.88 312.87 211.89 160.46 466.88 134.94 (peak 136.63) Exact (64/64)
24 24/24 94.27 288.75 178.32 190.67 473.44 133.07 Exact (64/64)
32 32/32 89.84 313.82 203.53 167.05 475.72 132.43 (peak 133.75) Exact (64/64)

(Because boot.asm identity-maps visitant RAM alongside 2 MiB huge pages, the bare-metal kernel avoids 4 KiB page-table walks throughout the 614 MiB example and outperforms the two Linux mmap() Phase 1 and llama.cpp CPU Q8_0 at all thread count.)

Live Proxmox VE VM (Intel Xeon E5-2670 v3, 12 Cores, 2 GiB RAM, 0 GB Disk, BIOS GRUB ISO)

Prompt Prompt Tokens Generated Tokens Prefill Decode
CPU cache clarification (3 sentences) 25 52 286.20 ms (87.35 tok/s) 920.15 ms (55.43 tok/s)
Alice & Bob apples term problem 39 96 446.86 ms (87.28 tok/s) 1770.49 ms (53.66 tok/s)
Other Article Hacker News
↑
Close Right Ads
Close Left Ads