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.

Preserve token 255 in full byte vocabularies

dagelf2e4540bab895
diff --git a/03-minigpt-train/main.asm b/03-minigpt-train/main.asm
index f308340..28ebb38 100644
--- a/03-minigpt-train/main.asm
+++ b/03-minigpt-train/main.asm
@@ -853,11 +853,7 @@ load_dataset:
cmp rcx, r14
jae .split
movzx edx, byte [r15 + rcx]
- movzx edx, byte [rbx + TRN_BYTE_TO_ID + rdx]
- cmp dl, 0xFF
- jne .enc_st
- xor edx, edx
-.enc_st:
+ BYTE_TO_TOKEN edx, rdx
mov [rax + rcx], dl
inc rcx
jmp .enc_lp
diff --git a/03-minigpt-train/model.asm b/03-minigpt-train/model.asm
index cc975b7..eab1a5a 100644
--- a/03-minigpt-train/model.asm
+++ b/03-minigpt-train/model.asm
@@ -1568,11 +1568,7 @@ gpt_sample:
.p_map:
; Map byte -> token ID via byte_to_id[byte]
- movzx eax, byte [rbx + TRN_BYTE_TO_ID + r15]
- cmp eax, 0xFF
- jne .p_id_ok
- xor eax, eax ; fallback to token 0
-.p_id_ok:
+ BYTE_TO_TOKEN eax, r15
cmp r13, 4096
jae .p_lp
mov [r12 + r13*4], eax
diff --git a/03-minigpt-train/train_constants.inc b/03-minigpt-train/train_constants.inc
index 4815c76..984cea0 100644
--- a/03-minigpt-train/train_constants.inc
+++ b/03-minigpt-train/train_constants.inc
@@ -156,3 +156,13 @@
%define TRN_BYTE_TO_ID 928 ; u8[256]
%define TRN_SIZE 1216 ; total struct size (64B multiple)
+
+; Resolve a byte using the v1 vocabulary tables in g_trn (rbx).
+; 0xFF denotes an unknown byte only when it falls outside the vocabulary.
+%macro BYTE_TO_TOKEN 2 ; output r32, input index r64
+ movzx %1, byte [rbx + TRN_BYTE_TO_ID + %2]
+ cmp %1, [rbx + TRN_VOCAB]
+ jb %%known
+ xor %1, %1 ; unknown bytes use token zero
+%%known:
+%endmacro
diff --git a/scripts/test_train_vocab.py b/scripts/test_train_vocab.py
new file mode 100644
index 0000000..1665a4c
--- /dev/null
+++ b/scripts/test_train_vocab.py
@@ -0,0 +1,80 @@
+#!/usr/bin/env python3
+"""Exercise byte vocabulary handling through the Mini-GPT CLI."""
+
+from pathlib import Path
+import struct
+import subprocess
+import sys
+import tempfile
+import unittest
+
+
+BINARY = Path(__file__).resolve().parents[1] / "build/asm-gpt-train"
+
+
+class TrainingDataTests(unittest.TestCase):
+ def setUp(self):
+ directory = tempfile.TemporaryDirectory()
+ self.addCleanup(directory.cleanup)
+ self.root = Path(directory.name)
+ self.data = self.root / "corpus"
+ self.dump = self.root / "debug"
+ self.checkpoint = self.root / "model.ckpt"
+
+ def train(self, *args):
+ return subprocess.run(
+ [BINARY, "--data", self.data, "--layers", "1", "--dim", "16",
+ "--heads", "1", "--hidden", "16", "--steps", "1", "--batch", "1",
+ "-t", "1", "--sample-every", "0", "--plain", *args],
+ capture_output=True, timeout=10,
+ )
+
+ def batch(self, name):
+ data = (self.dump / name).read_bytes()
+ return [value for value, in struct.iter_unpack("<I", data)]
+
+ def test_full_vocabulary(self):
+ self.data.write_bytes(bytes(range(256)) * 2)
+ for model in ([], ["--load", self.checkpoint]):
+ with self.subTest(resumed=bool(model)):
+ result = self.train("--seq", "256", "--dump-debug", self.dump,
+ "--save", self.checkpoint, *model)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ self.assertEqual(sorted(self.batch("batch_x.bin")), list(range(256)))
+ self.assertEqual(sorted(self.batch("batch_y.bin")), list(range(256)))
+
+ def test_checkpoint_prompt_byte_255(self):
+ self.data.write_bytes(bytes(range(256)) * 2)
+ self.train("--seq", "4", "--save", self.checkpoint).check_returncode()
+
+ # Give token 255 a known greedy continuation. The residual carries its
+ # first embedding component through the zeroed transformer block.
+ checkpoint = self.checkpoint.read_bytes()
+ header_size = 64 + 512
+ weights = bytearray(len(checkpoint) - header_size)
+ dim = 16
+ lm_head = len(weights) - 256 * dim * 4
+ struct.pack_into("<f", weights, 255 * dim * 4, 1.0)
+ struct.pack_into("<f", weights, lm_head - dim * 4, 1.0) # final RMSNorm
+ struct.pack_into("<f", weights, lm_head + ord("A") * dim * 4, 1.0)
+ self.checkpoint.write_bytes(checkpoint[:header_size] + weights)
+ result = subprocess.run(
+ [BINARY, "--load", self.checkpoint, "--sample-only", "--prompt", b"\xff",
+ "--sample-tokens", "1", "--temp", "0", "-t", "1"],
+ check=True, capture_output=True, timeout=10,
+ )
+ self.assertEqual(result.stdout, b"\xffA\n")
+
+ def test_unknown_bytes_keep_token_zero(self):
+ self.data.write_bytes(b"abc" * 32)
+ self.train("--seq", "4", "--save", self.checkpoint).check_returncode()
+ self.data.write_bytes(b"?" * 96)
+ self.train("--load", self.checkpoint, "--dump-debug", self.dump).check_returncode()
+ self.assertEqual(self.batch("batch_x.bin"), [0] * 4)
+ self.assertEqual(self.batch("batch_y.bin"), [0] * 4)
+
+
+if __name__ == "__main__":
+ if len(sys.argv) > 1 and not sys.argv[1].startswith("-"):
+ BINARY = Path(sys.argv.pop(1)).resolve()
+ unittest.main()