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.

AVX1: support FAST_Q8=0 as well

ruledicaprio2159ec11237e
diff --git a/01-qwen3-inference/README.md b/01-qwen3-inference/README.md
index 0ae6fde..a41404a 100644
--- a/01-qwen3-inference/README.md
+++ b/01-qwen3-inference/README.md
@@ -217,20 +217,20 @@ Head-to-head decode comparison on **AMD EPYC 7642** (47C online, 256 MiB L3, DDR
## AVX1 build for Sandy Bridge / Ivy Bridge (`AVX1=1`)
-Phases 1 and 2 (`FAST_Q8=1`) can be built for CPUs without AVX2, FMA and F16C:
+Phases 1 and 2, with `FAST_Q8=1` or `FAST_Q8=0`, can be built for CPUs without AVX2, FMA and F16C:
```bash
-make inference os AVX1=1
+make inference os AVX1=1 # add FAST_Q8=0 for FP32 activations
make isa-check AVX1=1 # objdump scan of the AVX1 binaries for the AVX2 / FMA / F16C / BMI forms listed in scripts/check_isa.py
python3 scripts/test_math.py build/test_math # after make test-math AVX1=1
```
-- INT8xINT8 Q8_0 kernel runs each 32-byte block as two VEX-128 halves (`vpsignb` / `vpabsb` / `vpmaddubsw` / `vpmaddwd`), halves summed in int32 before the fp32 scale.
+- `FAST_Q8=1`: the INT8xINT8 Q8_0 kernel runs each 32-byte block as two VEX-128 halves (`vpsignb` / `vpabsb` / `vpmaddubsw` / `vpmaddwd`), halves summed in int32 before the fp32 scale. `FAST_Q8=0`: the 4-row Q8_0 x FP32 kernel keeps its structure, with each 8-wide int8 load as two 128-bit sign extensions.
- FMA becomes `vmulps` + `vaddps` (`shared/isa.inc` macros); fp16 block scales are converted in software (exact for all 63,488 finite halves, subnormals included), so `.asmbin` v2 and v1 both work.
- AVX2-only integer ops elsewhere (`expf` exponent rebuild, `argmax` indices, broadcasts) use 128-bit or fp32 equivalents. The bare-metal kernel checks CPUID for AVX + SSSE3 + SSE4.1 + XSAVE instead of AVX2 + FMA + F16C.
-- `test_math.py` covers the matmul with K = 160: one 128-byte 4-block body and one 32-byte tail, for v1 and v2 scales, in every build mode.
-- 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, and `FAST_Q8=1` re-quantizes activations to int8, so greedy text can part from the AVX2 build's after some tokens.
-- `AVX1=1` covers `FAST_Q8=1` only; `AVX1=1 FAST_Q8=0` stops with an error, and so does `make train AVX1=1` (Phase 3 keeps AVX2 + FMA).
+- `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`.
## Live Generation Samples (`build/asm-llm`, `-t 32`)
diff --git a/01-qwen3-inference/test_math.asm b/01-qwen3-inference/test_math.asm
index f0227b0..2117f6a 100644
--- a/01-qwen3-inference/test_math.asm
+++ b/01-qwen3-inference/test_math.asm
@@ -133,21 +133,37 @@ _start:
%endif
xor edi, edi
call matmul_set_fp16
+ ; rows 0..5 then 5..8: FAST_Q8=0 runs its 4-row body and its 1-row tail
lea rdi, [rel in_mm_w]
lea rsi, [rel in_mm_x]
lea rdx, [rel out_mm]
mov rcx, 160 ; K
xor r8d, r8d ; r0 = 0
+ mov r9, 5 ; r1 = 5
+ call matmul_q8_0_range
+ lea rdi, [rel in_mm_w]
+ lea rsi, [rel in_mm_x]
+ lea rdx, [rel out_mm]
+ mov rcx, 160 ; K
+ mov r8, 5 ; r0 = 5
mov r9, 8 ; r1 = 8
call matmul_q8_0_range
mov edi, 1
call matmul_set_fp16
+ ; rows 0..5 then 5..8: FAST_Q8=0 runs its 4-row body and its 1-row tail
lea rdi, [rel in_mm_w16]
lea rsi, [rel in_mm_x]
lea rdx, [rel out_mm16]
mov rcx, 160 ; K
xor r8d, r8d ; r0 = 0
+ mov r9, 5 ; r1 = 5
+ call matmul_q8_0_range
+ lea rdi, [rel in_mm_w16]
+ lea rsi, [rel in_mm_x]
+ lea rdx, [rel out_mm16]
+ mov rcx, 160 ; K
+ mov r8, 5 ; r0 = 5
mov r9, 8 ; r1 = 8
call matmul_q8_0_range
diff --git a/shared/isa.inc b/shared/isa.inc
index 681788c..ed0b5a8 100644
--- a/shared/isa.inc
+++ b/shared/isa.inc
@@ -115,4 +115,17 @@
%endif
%endmacro
+; Sign-extend 8 int8 at [%3] to 8 int32 in ymm %1 (its low half is xmm %2).
+; AVX2: one vpmovsxbd ymm, qword. AVX1: two 128-bit vpmovsxbd and a lane
+; insert through the xmm scratch %4. %3 = address expression, no brackets.
+%macro PMOVSXBD8 4
+%if AVX1
+ vpmovsxbd %2, dword [%3]
+ vpmovsxbd %4, dword [%3 + 4]
+ vinsertf128 %1, %1, %4, 1
+%else
+ vpmovsxbd %1, qword [%3]
+%endif
+%endmacro
+
%endif
diff --git a/shared/matmul.asm b/shared/matmul.asm
index 913ec76..b4d6a15 100644
--- a/shared/matmul.asm
+++ b/shared/matmul.asm
@@ -42,9 +42,6 @@ default rel
%include "isa.inc"
-%if AVX1 && !FAST_Q8
-%error "AVX1=1 is implemented for FAST_Q8=1 only"
-%endif
%ifndef Q8_PREFETCH
%define Q8_PREFETCH prefetcht0
@@ -563,7 +560,8 @@ align 16
; ----------------------------------------------------------------------------
; void matmul_q8_0_range(const void *W, const float *x, float *out,
; uint64 K, uint64 r0, uint64 r1)
-; FAST_Q8=0 (legacy): 4-row unrolled AVX2 + FMA Q8_0 x FP32 matmul.
+; FAST_Q8=0 (legacy): 4-row unrolled AVX2 + FMA Q8_0 x FP32 matmul
+; (AVX1=1: the same structure through the isa.inc macros).
; Full FP32 activations (zero activation quantization noise).
; Auto-detects v1 FP32 scales vs v2 FP16 scales via g_fp16_scales.
; ----------------------------------------------------------------------------
@@ -647,33 +645,33 @@ matmul_q8_0_range:
vmovups ymm7, [r11 + r12 + 96]
; --- Interleaved Rows 0 & 1 ---
- vpmovsxbd ymm8, qword [rax + r13]
- vpmovsxbd ymm12, qword [rcx + r13]
+ PMOVSXBD8 ymm8, xmm8, rax + r13, xmm9
+ PMOVSXBD8 ymm12, xmm12, rcx + r13, xmm13
vcvtdq2ps ymm8, ymm8
vcvtdq2ps ymm12, ymm12
vmulps ymm8, ymm8, ymm4
vmulps ymm12, ymm12, ymm4
- vpmovsxbd ymm9, qword [rax + r13 + 8]
- vpmovsxbd ymm13, qword [rcx + r13 + 8]
+ PMOVSXBD8 ymm9, xmm9, rax + r13 + 8, xmm10
+ PMOVSXBD8 ymm13, xmm13, rcx + r13 + 8, xmm14
vcvtdq2ps ymm9, ymm9
vcvtdq2ps ymm13, ymm13
- vfmadd231ps ymm8, ymm9, ymm5
- vfmadd231ps ymm12, ymm13, ymm5
+ VFMADD231PS ymm8, ymm9, ymm5, ymm9
+ VFMADD231PS ymm12, ymm13, ymm5, ymm13
- vpmovsxbd ymm10, qword [rax + r13 + 16]
- vpmovsxbd ymm14, qword [rcx + r13 + 16]
+ PMOVSXBD8 ymm10, xmm10, rax + r13 + 16, xmm11
+ PMOVSXBD8 ymm14, xmm14, rcx + r13 + 16, xmm15
vcvtdq2ps ymm10, ymm10
vcvtdq2ps ymm14, ymm14
- vfmadd231ps ymm8, ymm10, ymm6
- vfmadd231ps ymm12, ymm14, ymm6
+ VFMADD231PS ymm8, ymm10, ymm6, ymm10
+ VFMADD231PS ymm12, ymm14, ymm6, ymm14
- vpmovsxbd ymm11, qword [rax + r13 + 24]
- vpmovsxbd ymm15, qword [rcx + r13 + 24]
+ PMOVSXBD8 ymm11, xmm11, rax + r13 + 24, xmm9
+ PMOVSXBD8 ymm15, xmm15, rcx + r13 + 24, xmm13
vcvtdq2ps ymm11, ymm11
vcvtdq2ps ymm15, ymm15
- vfmadd231ps ymm8, ymm11, ymm7
- vfmadd231ps ymm12, ymm15, ymm7
+ VFMADD231PS ymm8, ymm11, ymm7, ymm11
+ VFMADD231PS ymm12, ymm15, ymm7, ymm15
test ebp, ebp
jnz .s01_f16
@@ -683,44 +681,44 @@ matmul_q8_0_range:
.s01_f16:
movzx ebx, word [rdi + r14*2]
vmovd xmm9, ebx
- vcvtph2ps xmm9, xmm9
+ CVTPH2PS4 xmm9, xmm10
VBROADCASTSS_R ymm9, xmm9
movzx ebx, word [r8 + r14*2]
vmovd xmm13, ebx
- vcvtph2ps xmm13, xmm13
+ CVTPH2PS4 xmm13, xmm14
VBROADCASTSS_R ymm13, xmm13
.s01_ok:
- vfmadd231ps ymm0, ymm8, ymm9
- vfmadd231ps ymm1, ymm12, ymm13
+ VFMADD231PS ymm0, ymm8, ymm9, ymm8
+ VFMADD231PS ymm1, ymm12, ymm13, ymm12
; --- Interleaved Rows 2 & 3 ---
- vpmovsxbd ymm8, qword [rdx + r13]
- vpmovsxbd ymm12, qword [rsi + r13]
+ PMOVSXBD8 ymm8, xmm8, rdx + r13, xmm9
+ PMOVSXBD8 ymm12, xmm12, rsi + r13, xmm13
vcvtdq2ps ymm8, ymm8
vcvtdq2ps ymm12, ymm12
vmulps ymm8, ymm8, ymm4
vmulps ymm12, ymm12, ymm4
- vpmovsxbd ymm9, qword [rdx + r13 + 8]
- vpmovsxbd ymm13, qword [rsi + r13 + 8]
+ PMOVSXBD8 ymm9, xmm9, rdx + r13 + 8, xmm10
+ PMOVSXBD8 ymm13, xmm13, rsi + r13 + 8, xmm14
vcvtdq2ps ymm9, ymm9
vcvtdq2ps ymm13, ymm13
- vfmadd231ps ymm8, ymm9, ymm5
- vfmadd231ps ymm12, ymm13, ymm5
+ VFMADD231PS ymm8, ymm9, ymm5, ymm9
+ VFMADD231PS ymm12, ymm13, ymm5, ymm13
- vpmovsxbd ymm10, qword [rdx + r13 + 16]
- vpmovsxbd ymm14, qword [rsi + r13 + 16]
+ PMOVSXBD8 ymm10, xmm10, rdx + r13 + 16, xmm11
+ PMOVSXBD8 ymm14, xmm14, rsi + r13 + 16, xmm15
vcvtdq2ps ymm10, ymm10
vcvtdq2ps ymm14, ymm14
- vfmadd231ps ymm8, ymm10, ymm6
- vfmadd231ps ymm12, ymm14, ymm6
+ VFMADD231PS ymm8, ymm10, ymm6, ymm10
+ VFMADD231PS ymm12, ymm14, ymm6, ymm14
- vpmovsxbd ymm11, qword [rdx + r13 + 24]
- vpmovsxbd ymm15, qword [rsi + r13 + 24]
+ PMOVSXBD8 ymm11, xmm11, rdx + r13 + 24, xmm9
+ PMOVSXBD8 ymm15, xmm15, rsi + r13 + 24, xmm13
vcvtdq2ps ymm11, ymm11
vcvtdq2ps ymm15, ymm15
- vfmadd231ps ymm8, ymm11, ymm7
- vfmadd231ps ymm12, ymm15, ymm7
+ VFMADD231PS ymm8, ymm11, ymm7, ymm11
+ VFMADD231PS ymm12, ymm15, ymm7, ymm15
test ebp, ebp
jnz .s23_f16
@@ -730,15 +728,15 @@ matmul_q8_0_range:
.s23_f16:
movzx ebx, word [r9 + r14*2]
vmovd xmm9, ebx
- vcvtph2ps xmm9, xmm9
+ CVTPH2PS4 xmm9, xmm10
VBROADCASTSS_R ymm9, xmm9
movzx ebx, word [r10 + r14*2]
vmovd xmm13, ebx
- vcvtph2ps xmm13, xmm13
+ CVTPH2PS4 xmm13, xmm14
VBROADCASTSS_R ymm13, xmm13
.s23_ok:
- vfmadd231ps ymm2, ymm8, ymm9
- vfmadd231ps ymm3, ymm12, ymm13
+ VFMADD231PS ymm2, ymm8, ymm9, ymm8
+ VFMADD231PS ymm3, ymm12, ymm13, ymm12
add r12, 128
add r13, 32
@@ -774,18 +772,18 @@ matmul_q8_0_range:
xor r14d, r14d
mov ebp, [rsp + 40]
.blk1_lp:
- vpmovsxbd ymm8, qword [rax + r13]
- vpmovsxbd ymm9, qword [rax + r13 + 8]
- vpmovsxbd ymm10, qword [rax + r13 + 16]
- vpmovsxbd ymm11, qword [rax + r13 + 24]
+ PMOVSXBD8 ymm8, xmm8, rax + r13, xmm12
+ PMOVSXBD8 ymm9, xmm9, rax + r13 + 8, xmm12
+ PMOVSXBD8 ymm10, xmm10, rax + r13 + 16, xmm12
+ PMOVSXBD8 ymm11, xmm11, rax + r13 + 24, xmm12
vcvtdq2ps ymm8, ymm8
vcvtdq2ps ymm9, ymm9
vcvtdq2ps ymm10, ymm10
vcvtdq2ps ymm11, ymm11
vmulps ymm8, ymm8, [r11 + r12]
- vfmadd231ps ymm8, ymm9, [r11 + r12 + 32]
- vfmadd231ps ymm8, ymm10, [r11 + r12 + 64]
- vfmadd231ps ymm8, ymm11, [r11 + r12 + 96]
+ VFMADD231PS ymm8, ymm9, [r11 + r12 + 32], ymm9
+ VFMADD231PS ymm8, ymm10, [r11 + r12 + 64], ymm10
+ VFMADD231PS ymm8, ymm11, [r11 + r12 + 96], ymm11
test ebp, ebp
jnz .s1_f16
vbroadcastss ymm12, dword [rdi + r14*4]
@@ -793,10 +791,10 @@ matmul_q8_0_range:
.s1_f16:
movzx ebx, word [rdi + r14*2]
vmovd xmm12, ebx
- vcvtph2ps xmm12, xmm12
+ CVTPH2PS4 xmm12, xmm13
VBROADCASTSS_R ymm12, xmm12
.s1_ok:
- vfmadd231ps ymm0, ymm8, ymm12
+ VFMADD231PS ymm0, ymm8, ymm12, ymm8
add r12, 128
add r13, 32