{"slug": "bare-metal-qwen", "title": "Bare Metal Qwen", "summary": "A standalone bootable x86-64 operating system that runs Qwen/Qwen3-0.6B directly on hardware or KVM with no Linux, Windows, C, Rust or libc has been released, built as a 24,252-byte stripped static Multiboot 1 kernel (23,612 bytes with FAST_Q8=0). The project reports 74–94 ms cold boot to an interactive multi-turn chat with persistent KV-cache, 100% token-exact parity with its Phase 1 implementation across 1..32 vCPUs, and 134.9 tok/s decode on QEMU KVM (AMD EPYC 7642, 16 vCPUs), peaking at 136.6 tok/s, plus 55.4 tok/s on a diskless Proxmox VE VM (Intel Xeon E5-2670 v3, 12 cores). The kernel binds a 614 MiB FP16-scale (.asmbin v2) or 649 MiB FP32-scale (v1) model zero-copy from physical RAM on 2 MiB huge pages, and can be packed with GRUB2 and the model into a ~661 MiB bootable BIOS+UEFI ISO requiring zero hard disks.", "body_md": "A standalone bootable x86-64 operating system that runs **Qwen/Qwen3-0.6B** directly on hardware or KVM with **no Linux, no Windows, no C, no Rust, and no libc**.\n\n- **24,252 bytes** stripped static Multiboot 1 kernel (`FAST_Q8=1` , default:`.text` = 17,143 B,`.rodata` = 2,772 B,`.bss` = 2.1 MiB`NOBITS` , zero dynamic dependencies), or**23,612 bytes** (`FAST_Q8=0` , legacy Q8_0xFP32).\n- **74–94 ms cold boot** to interactive`asm-llm>` multi-turn chat with persistent KV-cache across turns, all SMP cores online, and the`.asmbin` v2 (`614 MiB` FP16 scales) or v1 (`649 MiB` FP32 scales) model bound zero-copy from physical RAM on 2 MiB huge pages.\n- **100% token-exact and byte-for-byte parity** with[Phase 1](https://github.com/alesha-pro/llm.asm/blob/main/01-qwen3-inference) across`1..32` vCPUs.\n- **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).\n\n*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):*\n\n- x86-64 CPU with **AVX2 + FMA + F16C** .\n- Core build & QEMU KVM:\n\n```\nsudo apt install nasm binutils qemu-system-x86\n```\n\n- Optional, for building a bootable BIOS+UEFI hybrid ISO (`make os-iso` ):\n\n```\nsudo apt install grub-pc-bin grub-efi-amd64-bin grub-common mtools xorriso\n```\n\n- An exported `models/qwen3-0.6b-q8_0.asmbin` file (`--scale-dtype fp16` v2 recommended, or legacy v1`--scale-dtype fp32` ; see[Phase 1 setup](https://github.com/alesha-pro/llm.asm/blob/main/01-qwen3-inference/README.md#1-download-qwenqwen3-06b) ).\n\nFrom the repository root (`FAST_Q8=1` is enabled by default; pass `FAST_Q8=0` for legacy FP32-activation mode):\n\n```\nmake os FAST_Q8=1\nmake os-check\n```\n\nExpected output (`FAST_Q8=1`):\n\n```\n-rwxr-xr-x 1 user user 24252 build/asm-llm-os.elf\nbuild/asm-llm-os.elf: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, stripped\nThere is no dynamic section in this file.\n```\n\n*(The kernel is linked as 64-bit ELF and wrapped into ELF32 via `objcopy -O elf32-i386` for standard Multiboot 1 loaders like QEMU `-kernel` and GRUB2; `boot.asm` transitions immediately into 64-bit Long Mode.)*\n\nBoot directly in the terminal over COM1 serial (`-nographic`):\n\n```\nqemu-system-x86_64 -enable-kvm -cpu host -m 2G -smp 16 \\\n  -kernel build/asm-llm-os.elf \\\n  -initrd models/qwen3-0.6b-q8_0.asmbin \\\n  -nographic\n```\n\nOr use the Makefile shortcut:\n\n```\nmake os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16\n```\n\nRun a one-shot batch prompt with clean ACPI shutdown:\n\n```\nmake os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16 \\\n  CMDLINE='prompt=\"Explain how a CPU cache works in 3 concise sentences.\" max_tokens=64 think=0 shutdown=1'\n```\n\nOpen a live **80x25 VGA GUI window** with PS/2 keyboard input:\n\n```\nqemu-system-x86_64 -enable-kvm -cpu host -m 2G -smp 16 \\\n  -kernel build/asm-llm-os.elf \\\n  -initrd models/qwen3-0.6b-q8_0.asmbin \\\n  -vga std -serial stdio\n```\n\nPackages the 23 KB kernel, GRUB2 Multiboot 1 config, and the 649 MiB `.asmbin` model into a single self-contained `~661 MiB` bootable ISO (`build/asm-llm-os.iso`):\n\n```\nmake os-iso MODEL=models/qwen3-0.6b-q8_0.asmbin\n```\n\nBoot the ISO in QEMU KVM (SeaBIOS or UEFI OVMF):\n\n```\n# Legacy BIOS (SeaBIOS)\nqemu-system-x86_64 -enable-kvm -cpu host -m 2G -smp 16 \\\n  -cdrom build/asm-llm-os.iso -nographic\n\n# UEFI (OVMF)\nqemu-system-x86_64 -enable-kvm -cpu host -m 2G -smp 16 \\\n  -bios /usr/share/ovmf/OVMF.fd \\\n  -cdrom build/asm-llm-os.iso -nographic\n```\n\nBecause the entire OS and model live inside the ISO and run out of RAM, the VM needs **zero hard disks**.\n\n1. Upload `build/asm-llm-os.iso` to your Proxmox node's ISO storage (`local` ->**ISO Images** ->**Upload** , or`scp` into`/var/lib/vz/template/iso/asm-llm-os.iso` ).\n2. Create and start a diskless VM on the Proxmox host (pick an unused `<vmid>` , e.g.`250` ):\n\n```\nqm create 250 \\\n  --name asm-llm-os \\\n  --machine pc \\\n  --bios seabios \\\n  --cpu host \\\n  --cores 12 \\\n  --memory 2048 \\\n  --balloon 0 \\\n  --vga std \\\n  --serial0 socket \\\n  --ide2 local:iso/asm-llm-os.iso,media=cdrom \\\n  --boot order=ide2\n\nqm start 250\n```\n\n1. Interact either through the Proxmox web UI (**noVNC Console** , VGA + PS/2 keyboard) or over the virtual serial port:\n\n```\nqm terminal 250\n```\n\n*(Note: `--cpu host` is required so the VM exposes AVX2 + FMA, and `--balloon 0` ensures 2 GiB of contiguous guest RAM for the 649 MiB model + 449 MiB KV-cache arena.)*\n\nBy default, the bare-metal console runs in **multi-turn chat mode with persistent KV-cache**: each new user turn prefills only the new Qwen3 chat tokens starting at `g_chat_pos` without recomputing earlier turns, closes every assistant turn in the KV-cache with `<|im_end|>\\n`, and displays live context usage (` ctx=X/2048`) in the decode stats line. When the KV-cache approaches `max_seq` (`pos + n_prompt + 64 >= max_seq`), it prints `[chat] context full, resetting KV-cache` and resets cleanly. If booted with `prompt=\"...\"` and `shutdown=0`, the initial batch turn stays in the KV-cache so you can continue the same conversation in the REPL.\n\nBoth PS/2 keyboard (VGA) and COM1 serial (`115200 8N1`) are polled simultaneously. Type any message and press Enter, or use a built-in command:\n\n| Command | Action | \n|---|---|\n| `/clear` ,`/new` ,`/reset` | Clear conversation history ( `g_chat_pos = 0` , prints`ctx=0/2048` ) | \n| `/think` | Enable Qwen3 `<think>...</think>` reasoning mode | \n| `/nothink` | Disable reasoning mode (direct answers, default) | \n| `/bench` | Reset KV-cache and run built-in latency/throughput benchmark prompt | \n| `/halt` or`/quit` | Trigger clean ACPI S5 poweroff | \n| `/help` | Show available console commands | \n\nPass via QEMU `-append \"...\"`, `make os-run CMDLINE='...'`, or `make os-iso CMDLINE='...'`:\n\n| Option | Default | Description | \n|---|---|---|\n| `prompt=\"...\"` | *(none)* | Run a batch prompt immediately after boot (preserved in KV-cache if `shutdown=0` ) | \n| `max_tokens=N` | `128` | Maximum tokens to generate per prompt | \n| `ctx=N` | `2048` | KV-cache context length | \n| `threads=N` | `0` | Limit active SMP cores ( `0` = all online cores) | \n| `think=0\\|1` | `0` | Enable or disable Qwen3 reasoning mode | \n| `raw=0\\|1` | `0` | Skip Qwen3 chat template | \n| `single_turn=0\\|1` | `0` | Disable multi-turn KV-cache retention ( `0` = multi-turn chat by default) | \n| `temp=F` | `0.0` | Sampling temperature ( `0.0` = greedy) | \n| `topp=F` | `0.95` | Top-p threshold | \n| `seed=N` | `42` | PRNG seed | \n| `print_tokens=0\\|1` | `0` | Print `PROMPT_TOKENS:` and`GEN_TOKENS:` markers | \n| `shutdown=0\\|1` | `0` | Power off via ACPI after batch prompt completes | \n\nPhase 2 compiles the exact same `shared/math.asm`, `shared/matmul.asm`, `shared/tokenizer.asm`, and `shared/transformer.asm` with `-DBARE_METAL -DFAST_Q8=$(FAST_Q8)` and pairs them with 4,167 lines of bare-metal OS code:\n\n- **Boot & Long Mode (`boot.asm`, `linker.ld`):** verifies Multiboot 1 magic (` 0x2BADB002` ), zeroes`.bss` , builds 4-level 2 MiB huge-page identity tables covering`0..4 GiB` (with`0xFE000000..0xFFFFFFFF` marked Strong Uncacheable for Local APIC MMIO), enters 64-bit Long Mode, installs a 32-entry exception IDT, verifies`XSAVE + AVX + AVX2 + FMA + F16C` via CPUID, and initializes`CR0` /`CR4` /`XCR0=7` /`MXCSR` .\n- **Zero-Copy Weights & Physical Arena (`runtime.asm`):** binds the`.asmbin` (v2 FP16 scales or v1 FP32 scales) Multiboot module directly at its physical address (validating`\"ASML\"` magic) and carves the 449 MiB KV-cache + scratch arena from the largest usable Multiboot memory-map region, zeroing it with AVX2.\n- **Bare-Metal SMP (`smp.asm`):** parses ACPI RSDP -> RSDT/XSDT -> MADT (`\"APIC\"` ) with 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 every core, and dispatches both`matmul_q8_0` and`gqa_attn_range` (`FAST_Q8=1` ) with a lock-free cache-line-isolated spin-barrier.\n- **Hardware Drivers (`drivers.asm`):**  - **COM1 Serial (` 0x3F8`, 115200 8N1):** raw UTF-8 TX and RX.\n  - **80x25 Color VGA (`0xB8000`):** colored boot banner, hardware scrolling, batched CRTC cursor updates, and UTF-8 decoding with Cyrillic-to-ASCII fallback on VGA while streaming raw UTF-8 to COM1.\n  - **PS/2 Keyboard (`0x60`/` 0x64`):** scancode set 1 to ASCII (Shift, Backspace, Enter).\n  - **PIT Channel 2 + RDTSC:** 20 ms hardware PIT calibration for accurate nanosecond boot/prefill/decode profiling.\n  - **ACPI Poweroff:** QEMU/Bochs ACPI S5 ports (`0x604` ,`0xB004` ,`0x4004` ) plus`isa-debug-exit` fallback.\n\n```\npython3 scripts/verify_os.py --model models/qwen3-0.6b-q8_0.asmbin --run-dir runs/os-verify\n```\n\nAll 5 parity prompts (English technical, English concise, Russian UTF-8, arithmetic word problem, `--think` mode) match Phase 1 (`build/asm-llm`) **100% exact** in prompt tokens, generated tokens, and raw UTF-8 bytes.\n\n### KVM SMP Scaling (`FAST_Q8=1`, `.asmbin` v2 FP16 Scales, AMD EPYC 7642, 34 prompt tok, 64 gen tok, greedy)\n\n| 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 | \n|---|---|---|---|---|---|---|---|---|\n| 1 | 1/1 | 73.47 | 1161.48 | 1071.98 | 31.72 | 2685.04 | **23.46***(peak 23.67)* | **Exact (64/64)** | \n| 4 | 4/4 | 84.10 | 425.11 | 324.82 | 104.67 | 805.95 | **78.17***(peak 78.89)* | **Exact (64/64)** | \n| 8 | 8/8 | 84.54 | 316.71 | 215.93 | 157.46 | 529.53 | **118.97** | **Exact (64/64)** | \n| **16** | **16/16** | **84.88** | **312.87** | **211.89** | **160.46** | **466.88** | **134.94***(peak 136.63)* | **Exact (64/64)** | \n| 24 | 24/24 | 94.27 | 288.75 | 178.32 | **190.67** | 473.44 | **133.07** | **Exact (64/64)** | \n| 32 | 32/32 | 89.84 | 313.82 | 203.53 | 167.05 | 475.72 | **132.43***(peak 133.75)* | **Exact (64/64)** | \n\n*(Because `boot.asm` identity-maps guest RAM with 2 MiB huge pages, the bare-metal kernel avoids 4 KiB page-table walks across the 614 MiB model and outperforms both Linux `mmap()` Phase 1 and `llama.cpp` CPU `Q8_0` at every thread count.)*\n\n| Prompt | Prompt Tokens | Generated Tokens | Prefill | Decode | \n|---|---|---|---|---|\n| CPU cache explanation (3 sentences) | 25 | 52 | 286.20 ms ( **87.35 tok/s** ) | 920.15 ms ( **55.43 tok/s** ) | \n| Alice & Bob apples word problem | 39 | 96 | 446.86 ms ( **87.28 tok/s** ) | 1770.49 ms ( **53.66 tok/s** ) |", "url": "https://wpnews.pro/news/bare-metal-qwen", "canonical_source": "https://github.com/alesha-pro/llm.asm/tree/main/02-baremetal-os", "published_at": "2026-11-25 22:15:34+00:00", "updated_at": "2026-11-25 22:42:12.611692+00:00", "lang": "en", "topics": ["large-language-models", "ai-infrastructure", "ai-tools", "machine-learning"], "entities": ["Qwen/Qwen3-0.6B", "QEMU", "Proxmox VE", "AMD EPYC 7642", "Intel Xeon E5-2670 v3", "GRUB2", "Multiboot 1"], "also_reported_by": [], "takeaway": "The 24,252-byte Multiboot 1 kernel boots Qwen3-0.6B in 74–94 ms and hits 134.9 tok/s decode at 16 vCPUs on QEMU KVM, with FAST_Q8=1 (default) cutting the kernel to 24,252 bytes versus 23,612 bytes in legacy FP32-activation mode.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/bare-metal-qwen", "markdown": "https://wpnews.pro/news/bare-metal-qwen.md", "text": "https://wpnews.pro/news/bare-metal-qwen.txt", "jsonld": "https://wpnews.pro/news/bare-metal-qwen.jsonld"}}