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.

Fix #15 zero-dim check, #35 macOS toolchain lookup, #37 REPO_ROOT, and sync binary sizes

Alexey Fateev8a12dad71b84
diff --git a/02-baremetal-os/README.md b/02-baremetal-os/README.md
index 8887779..a5b616a 100644
--- a/02-baremetal-os/README.md
+++ b/02-baremetal-os/README.md
@@ -2,7 +2,7 @@
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**.
-- **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).
+- **24,508 bytes** stripped static Multiboot 1 kernel (`FAST_Q8=1`, default: `.text` = 17,367 B, `.rodata` = 2,772 B, `.bss` = 2.1 MiB `NOBITS`, zero dynamic dependencies), or **23,612 bytes** (`FAST_Q8=0`, legacy Q8_0xFP32).
- **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.
- **100% token-exact and byte-for-byte parity** with [Phase 1](../01-qwen3-inference/) across `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).
@@ -42,7 +42,7 @@ make os-check
Expected output (`FAST_Q8=1`):
```text
--rwxr-xr-x 1 user user 24252 build/asm-llm-os.elf
+-rwxr-xr-x 1 user user 24508 build/asm-llm-os.elf
build/asm-llm-os.elf: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, stripped
There is no dynamic section in this file.
```
diff --git a/03-minigpt-train/README.md b/03-minigpt-train/README.md
index 568d0af..9905ebb 100644
--- a/03-minigpt-train/README.md
+++ b/03-minigpt-train/README.md
@@ -2,7 +2,7 @@
A complete Transformer training and autoregressive inference engine written in **100% pure x86-64 NASM assembly**. **Zero Python, zero C, zero libc, zero libm, and zero runtime dependencies** are needed to train a language model from scratch and generate text from it.
-- **32,944 bytes (`32.2 KiB`)** stripped static Linux x86-64 ELF (`.text` = 28,114 B, `.rodata` = 4,205 B, `.bss` = 173,568 B `NOBITS`, zero dynamic libraries).
+- **33,128 bytes (`32.4 KiB`)** stripped static Linux x86-64 ELF (`.text` = 28,218 B, `.rodata` = 4,297 B, `.bss` = 173,568 B `NOBITS`, zero dynamic libraries).
- **Modern Llama/GPT FP32 architecture:** auto-discovered character vocabulary, pre-norm RMSNorm, split-half RoPE (FP64-precomputed cos/sin tables), Causal Multi-Head Self-Attention, SwiGLU MLP, untied `tok_emb` and `lm_head`, and numerically stable Cross-Entropy Loss.
- **Hand-written `6x16` AVX2+FMA FP32 SGEMM:** 12 YMM accumulators, zero stack traffic in the inner `k`-loop, supporting `NN`, `TN`, and `NT` layouts with disjoint tile parallelism over our zero-libc `clone()` + `futex()` thread pool (**bit-exact** across `1T..24T`).
- **Full analytical backward pass + decoupled AdamW:** verified per-tensor against PyTorch CPU across all **21 parameter tensors** (`worst max_abs_diff = 3.35e-8`, `min cosine_sim = 1.0000000000`), global L2 gradient clipping, linear warmup + cosine LR decay, and `.gptckpt` binary checkpoints.
@@ -47,7 +47,7 @@ make train-check
Expected ELF inspection output:
```text
--rwxr-xr-x 1 user user 32944 build/asm-gpt-train
+-rwxr-xr-x 1 user user 33128 build/asm-gpt-train
build/asm-gpt-train: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, stripped
There is no dynamic section in this file.
```
diff --git a/03-minigpt-train/main.asm b/03-minigpt-train/main.asm
index a7d469d..4c8f125 100644
--- a/03-minigpt-train/main.asm
+++ b/03-minigpt-train/main.asm
@@ -236,8 +236,10 @@ _start:
jmp .arg_lp
.args_done:
- ; Kernels need D, head_dim and FF on a 16-float boundary, and H dividing D
+ ; Kernels need D, head_dim and FF on a positive 16-float boundary, and H dividing D
mov rax, [rbp + TRN_DIM]
+ test rax, rax
+ jz .err_shape
test al, 15
jnz .err_shape
mov rcx, [rbp + TRN_HEADS]
@@ -250,6 +252,8 @@ _start:
test al, 15
jnz .err_shape
mov rax, [rbp + TRN_HIDDEN]
+ test rax, rax
+ jz .err_shape
test al, 15
jnz .err_shape
diff --git a/Makefile b/Makefile
index efe3975..76990a8 100644
--- a/Makefile
+++ b/Makefile
@@ -1,19 +1,22 @@
# --- Cross-platform toolchain detection ---
-# Linux : GNU binutils (ld, objcopy) 原生支持所有参数
-# macOS : Apple ld/objcopy 不支持 ELF, 必须用 mold + llvm-objcopy + ld.lld
+# Linux : GNU binutils (ld, objcopy) supports all ELF and linker-script flags natively.
+# macOS : Apple ld/objcopy only targets Mach-O; use LLVM (ld.lld, llvm-objcopy) or mold.
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin)
LLVM_BIN := $(firstword \
+ $(wildcard /opt/homebrew/opt/llvm/bin) \
+ $(wildcard /usr/local/opt/llvm/bin) \
$(wildcard /opt/local/libexec/llvm-devel/bin) \
$(wildcard /opt/local/libexec/llvm-21/bin) \
$(wildcard /opt/local/libexec/llvm-19/bin) \
$(wildcard /opt/local/libexec/llvm-18/bin) \
$(wildcard /opt/local/libexec/llvm-17/bin) \
+ $(patsubst %/,%,$(dir $(shell command -v ld.lld 2>/dev/null))) \
)
- LD := mold
- OBJCOPY := $(LLVM_BIN)/llvm-objcopy
- OS_LD := $(LLVM_BIN)/ld.lld
+ OS_LD := $(if $(LLVM_BIN),$(LLVM_BIN)/ld.lld,ld.lld)
+ OBJCOPY := $(if $(LLVM_BIN),$(LLVM_BIN)/llvm-objcopy,llvm-objcopy)
+ LD := $(if $(shell command -v mold 2>/dev/null),mold,$(OS_LD))
else
LD := ld
OBJCOPY := objcopy
@@ -197,4 +200,4 @@ train-check: $(TRAIN_TARGET)
check-all: check os-check train-check
clean:
- rm -rf $(BUILD)
\ No newline at end of file
+ rm -rf $(BUILD)
diff --git a/README.md b/README.md
index 9f3b294..534b18b 100644
--- a/README.md
+++ b/README.md
@@ -16,8 +16,8 @@
| Phase | Target | Size | Stack | Headline Result |
|---|---|---:|---|---|
| [`01`](01-qwen3-inference/) | [`01-qwen3-inference/`](01-qwen3-inference/) · `Qwen3-0.6B` static ELF | **14.7 KB** (`14,712 B`) | Pure x86-64 NASM | `126.8 tok/s` (40T), `+18%..+36%` vs `llama.cpp` CPU, `64/64` PyTorch parity |
-| [`02`](02-baremetal-os/) | [`02-baremetal-os/`](02-baremetal-os/) · Bootable Bare-Metal OS | **24.3 KB** (`24,252 B`) | Pure x86-64 NASM | `74–94 ms` boot, `134.9 tok/s` KVM (16 vCPU), Proxmox `0 GB` disk (`55.4 tok/s`) |
-| [`03`](03-minigpt-train/) | [`03-minigpt-train/`](03-minigpt-train/) · Mini-GPT Trainer + TUI | **32.9 KB** (`32,944 B`) | Pure x86-64 NASM | `27,662 tok/s`, `1.0000000000` grad cosine vs PyTorch, `80x24` Braille UI |
+| [`02`](02-baremetal-os/) | [`02-baremetal-os/`](02-baremetal-os/) · Bootable Bare-Metal OS | **24.5 KB** (`24,508 B`) | Pure x86-64 NASM | `74–94 ms` boot, `134.9 tok/s` KVM (16 vCPU), Proxmox `0 GB` disk (`55.4 tok/s`) |
+| [`03`](03-minigpt-train/) | [`03-minigpt-train/`](03-minigpt-train/) · Mini-GPT Trainer + TUI | **33.1 KB** (`33,128 B`) | Pure x86-64 NASM | `27,662 tok/s`, `1.0000000000` grad cosine vs PyTorch, `80x24` Braille UI |
| [`04`](04-ring1-kernel-surgeon/) | [`04-ring1-kernel-surgeon/`](04-ring1-kernel-surgeon/) · Ring `-1` SVM Hypervisor | **75.4 KB** (`75,424 B`) | NASM + zero-libc C | MiniCPM5-2B live kernel crash patcher, `97.54%` pass@1 (`119/122`), `0%` dangerous |
| [`05`](05-ebpf-jev/) | [`05-ebpf-jev/`](05-ebpf-jev/) · Linux `6.12` eBPF LSM | **21.3 KB** (`21,280 B`) | eBPF C + INT8 RNN | `5.65 µs` median hook, `0` floats, `500/500` `0-LSB` parity, atomic hot-swap |
@@ -100,8 +100,8 @@ make all # Builds Phase 1 (asm-llm, test_math), Phase 2 (asm-llm-os.elf), Phas
```
- **01 · Qwen3 Inference (`14.7 KB`):** `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` → [01-qwen3-inference/README.md](01-qwen3-inference/README.md)
-- **02 · Bare-Metal OS (`24.3 KB`):** `make os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16` *(or `make os-iso` for BIOS+UEFI ISO)* → [02-baremetal-os/README.md](02-baremetal-os/README.md)
-- **03 · Mini-GPT Trainer (`32.9 KB`):** `./build/asm-gpt-train --data data/tinyshakespeare.txt --steps 400 --seq 128 -t 16 --save checkpoints/shakespeare.ckpt` → [03-minigpt-train/README.md](03-minigpt-train/README.md)
+- **02 · Bare-Metal OS (`24.5 KB`):** `make os-run MODEL=models/qwen3-0.6b-q8_0.asmbin SMP=16` *(or `make os-iso` for BIOS+UEFI ISO)* → [02-baremetal-os/README.md](02-baremetal-os/README.md)
+- **03 · Mini-GPT Trainer (`33.1 KB`):** `./build/asm-gpt-train --data data/tinyshakespeare.txt --steps 400 --seq 128 -t 16 --save checkpoints/shakespeare.ckpt` → [03-minigpt-train/README.md](03-minigpt-train/README.md)
- **04 · Ring `-1` Kernel Surgeon (`75.4 KB`):** `cd 04-ring1-kernel-surgeon && make hv && python3 verifier/kvm_verifier.py --selftest` → [04-ring1-kernel-surgeon/README.md](04-ring1-kernel-surgeon/README.md)
- **05 · Linux `6.12` eBPF LSM (`21.3 KB`):** `cd 05-ebpf-jev && bash testbed/build.sh && testbed/run_vm.sh verify` → [05-ebpf-jev/README.md](05-ebpf-jev/README.md)
diff --git a/scripts/verify-train-np/README.md b/scripts/verify-train-np/README.md
index b36ff39..3653f17 100644
--- a/scripts/verify-train-np/README.md
+++ b/scripts/verify-train-np/README.md
@@ -43,7 +43,7 @@ particular reference implementation drops sharply.
Self-contained: builds the dump itself by invoking the trainer.
```bash
-python3 scripts/verify_train_np.py \
+python3 scripts/verify-train-np/verify_train_np.py \
--bin build/asm-gpt-train \
--data data/tinyshakespeare.txt
```
@@ -51,7 +51,7 @@ python3 scripts/verify_train_np.py \
Verify an already-produced dump directory (no trainer invocation):
```bash
-python3 scripts/verify_train_np.py --dump-dir /tmp/mydump
+python3 scripts/verify-train-np/verify_train_np.py --dump-dir /tmp/mydump
```
All trainer options mirror `verify_train_ref.py` (`--layers --dim --heads
@@ -120,7 +120,7 @@ directory, copies it to a temp dir, adds `1e-2` to the first 64 float32 values o
`step0_grads.bin`, and re-runs the harness (the original dump is never touched):
```
-$ python _negative_test.py /tmp/mydump # any --dump-debug output
+$ python3 scripts/verify-train-np/_negative_test.py /tmp/mydump # any --dump-debug output
```
Output on this machine:
diff --git a/scripts/verify-train-np/verify_train_np.py b/scripts/verify-train-np/verify_train_np.py
index a1f820a..cfb1f7f 100644
--- a/scripts/verify-train-np/verify_train_np.py
+++ b/scripts/verify-train-np/verify_train_np.py
@@ -57,15 +57,15 @@ PATHS
USAGE
# 1. point at the freshly built binary; this script runs it for you
- python scripts/verify_train_np.py --bin build/asm-gpt-train \
- --data data/tinyshakespeare.txt
+ python3 scripts/verify-train-np/verify_train_np.py --bin build/asm-gpt-train \
+ --data data/tinyshakespeare.txt
# 2. or verify an already-produced dump directory (offline)
- python scripts/verify_train_np.py --dump-dir /tmp/np_verify/run1
+ python3 scripts/verify-train-np/verify_train_np.py --dump-dir /tmp/np_verify/run1
# 3. quick pass with a shorter run
- python scripts/verify_train_np.py --bin build/asm-gpt-train \
- --data data/tinyshakespeare.txt --steps 10 --keep-dump
+ python3 scripts/verify-train-np/verify_train_np.py --bin build/asm-gpt-train \
+ --data data/tinyshakespeare.txt --steps 10 --keep-dump
Exit code: 0 = all gates PASS, 1 = a gate FAILED, 2 = usage/IO error.
"""
@@ -84,9 +84,11 @@ import numpy as np
# --------------------------------------------------------------------------
_HERE = Path(__file__).resolve()
-# Expect to live at <repo>/scripts/verify_train_np.py. Fall back to the
-# containing directory so the file also works when copied elsewhere.
-REPO_ROOT = _HERE.parent.parent if _HERE.parent.name == "scripts" else _HERE.parent
+# Locate repository root (works from scripts/verify-train-np/ or scripts/).
+REPO_ROOT = next(
+ (p for p in _HERE.parents if (p / "Makefile").is_file()),
+ _HERE.parents[2] if len(_HERE.parents) > 2 else _HERE.parent,
+)
F32 = np.float32
F64 = np.float64