Hacker News 熱門精选70

Show HN:一个驻留在引导扇区中的语言模型

Show HN: A language model that lives in a boot sector

· 7小时前
跳到正文

精选理由

展示如何将神经网络压缩进 510 字节引导扇区,并借由逐层调试揭示底层实现细节。

AI 摘要

所有想法都源自引导扇区演示圈,那里的人们一直在用 512 字节编写国际象棋和 BASIC 游戏,甚至还写出了 C 编译器。我想知道,用神经网络代替程序能做些什么,而为不存在的硬件生成启动日志,是个值得尝试的有趣点子。整个推理引擎占用 MBR 中的 510 字节,权重、词表和字符串则存放在 MBR 之后的扇区中。这个小型字符级神经网络使用机器生成的 dmesg 进行训练,因此机器可以无限生成 Linux 启动消息。train.py 执行的整数算术运算与汇编代码完全相同,因此其输出与 QEMU 的输出逐字符一致。差异分析揭示了我最喜欢的一个缺陷。开头的字符与模拟器生成的字符一致,但随后输出就出现了偏差。我通过 QEMU 的调试端口取出隐藏层数据,将各个神经元的值与模拟器中的值逐一比较。只有第一个神经元的值是正确的。激活计算涉及 idiv,而 idiv 会把余数存入 edx。偏置表的指针也存放在 edx 中,导致第一个神经元使用了正确的偏置,而后续神经元使用的都是垃圾数据。现在,偏置的地址根据循环计数器确定,不再需要让任何指针在除法运算后保持不变。另一个缺陷是我在首次试运行前,仅靠阅读代码就发现的:BIOS 打印例程会将 bx 清零,但 bx 中存放的是预测字符的编号。这就是为什么打印操作前后要对 bx 执行 push 和 pop。sectorllm 与我的项目最相似,因为它仅用 1.2K 的汇编代码,就运行了一个真正带有注意力机制的 5 层 llama2。它采取了与我相反的取舍:我的引擎必须塞进一个扇区,因此我的网络只有一个使用 int8 运算的隐藏层,用来梦出 dmesg。评论链接:https://news.ycombinator.com/item?id=50315782 得分:3 评论数:0

正文

bootgpt

A neural network that boots your PC.

The MBR is 512 bytes of hand written asm. It loads an 11.5 KB int8 model from the next 23 sectors and runs it in 16-bit real mode on a 386. No OS, no C runtime, nothing. The machine wakes up and dreams a Linux boot log: timestamps, PCI bars, ACPI events for hardware that never existed.

demo

Build thread: https://x.com/krlntn/status/2116514628457185757

With 8 chars of context and one hidden layer this is roughly the quality you get:

[  2.283888] pci 0000:01:01.4: [1b36:0a35] ryndrde c4128 6422 Vveine
[  1.953886] pci 0000:01:1f.2: reg 0x10: [mem 0x7e4515e5-0x2d766c27] usbbore: reg
[  0.206596] Command line: BOOT_IMAGE=/vmlinu -5.5.2-1-generic gcc version 13.3.0, GNU ld ...
[  1.815765] ACPI: PCI Interrupt Link [LNKA: v/s.xos212
[  1.883084] NET: Registered protocol family 16

Run it

brew install nasm qemu
python3 -m venv .venv && .venv/bin/pip install numpy
./build.sh
./run.sh

disk.img is a normal boot image. dd it to a USB stick and boot any BIOS box if you have one. It only reads its own sectors.

Pieces

corpus_gen.py makes 3 MB of fake dmesg. One kernel version and one disk per boot session, because real boot logs repeat themselves and that repetition is what makes a tiny model look sane.

train.py trains the model (char embeddings, one hidden layer, softmax) and quantizes everything to int8 with power-of-two scales, so the boot sector can do the whole forward pass with imul and a single idiv. The script also simulates the exact integer math the asm will run, LCG sampler included, so the Python output and the boot sector output can be diffed. They match char for char.

boot.asm is the entire runtime: int 13h to load weights, int 10h to print, a softclip activation done with cdq/idiv, argmax sampling with timer seeded noise. Any keypress reboots the machine.

record.py records the demo gif from headless QEMU through the monitor socket. No window involved.

The boot sector is code only. Strings, weights and the vocab table all live in the data sectors, it was the only way to fit inference plus BIOS plumbing into 510 bytes.

Prior art

The boot sector genre is old and glorious. Oscar Toledo put chess, a whole OS and even a BASIC interpreter in there, other people did a C compiler, all inside 512 bytes. His book on boot sector games is a good read.

The closest neighbor is sectorllm, which boots a real llama2 transformer (5 layers, attention, the works) from about 1.2 KB of real mode asm, running Karpathy's stories260K model. sectorlisp-nn runs a TensorFlow-trained network during boot on a Lisp interpreter, and there are UEFI apps that boot straight into llama-class models on modern hardware.

bootgpt takes the other extreme: the whole engine fits in the 510-byte MBR itself, weights and strings live in the following sectors, and the model is not a general story teller but a tiny char-level net trained for exactly one thing, hallucinating the boot log of the machine it wakes up on.

查看原文(Hacker News 熱門)