Fermion ResearchFermion Research

QwenLinux: booting Linux inside Qwen3.5-0.8B

We compiled a 32-bit RISC-V computer and 10 MB of RAM into the weights of a standard Qwen3.5 model. It boots Linux to a shell and runs a small transformer inside Linux, while the same checkpoint still chats as Qwen3.5-0.8B.

We compiled a complete, self-contained 32-bit RISC-V computer (RV32IMA, Zicsr, machine-mode traps, MRET, a 64-bit CLINT timer and an hvc0 console) plus 10.0 MB of mutable RAM directly into the weights of an unmodified HuggingFace Qwen3_5ForCausalLM, merged with Qwen/Qwen3.5-0.8B.

No gradient descent, no custom PyTorch ops, and no external memory. From the reset vector 0x80000000, ordinary token-by-token generation boots unmodified mainline Linux 6.1.14 to a live /bin/sh shell, unpacks an initramfs, and launches a 4-bit, 5-layer Llama-2 transformer (260K parameters, compiled into a 7.7 KB RV32IMA binary) inside that kernel to write English text. The same checkpoint still chats as Qwen/Qwen3.5-0.8B, with KL 0.0005 to 0.0021 against the stock model.

A transformer, inside Linux, inside a transformer
Console output produced by the model's own forward passes.
[ 0.000000] Linux version 6.1.14 (riscv32-buildroot-linux-uclibc-gcc 12.2.0) #4 [ 0.000000] Machine model: riscv-minimal-nommu,qemu [ 0.000000] clint: clint@11000000: timer running at 1000000 Hz ⋮ [ 0.718308] Unpacking initramfs... [ 0.776848] Run /bin/sh as init process ~ # l Linux (none) 6.1.14 #4 Sat Mar 25 09:20:08 EDT 2023 riscv32 Once upon a time, there was a boy named Timmy. ~ #
427,500,002consecutive forward passes
51,512,377RISC-V instructions
10.0 MBRAM held in recurrent state
Run 2, the inner transformer. Selected lines; full transcripts are in the GitHub repo.

Two runs, end to end

Every instruction fetch, register update, syscall, timer interrupt and console byte below came from executing the compiled Qwen3_5ForCausalLM weights.

Run 1: a live Linux shell

287,990,241 forward passes, 45,000,915 RISC-V instructions (6.40 tokens per instruction), 0 errors, 13,591 s at 21,189 tokens/s on one NVIDIA L4. Linux 6.1.14 boots to ~ # and runs commands typed in through the console:

[    0.000000] Linux version 6.1.14 (cnlohr@cnlohr-1520) (riscv32-buildroot-linux-uclibc-gcc.br_real (Buildroot 2023.02-145-gf6c5488ad2) 12.2.0, GNU ld (GNU Binutils) 2.38) #4 Sat Mar 25 09:20:08 EDT 2023
[    0.000000] Machine model: riscv-minimal-nommu,qemu
[    0.000000] Memory: 6540K/10236K available (1487K kernel code, 281K rwdata, 157K rodata, 1464K init, 145K bss, 3696K reserved, 0K cma-reserved)
[    0.000000] clint: clint@11000000: timer running at 1000000 Hz
[    0.080868] clocksource: Switched to clocksource clint_clocksource
[    0.765676] Run /bin/sh as init process
~ # uname -a
Linux (none) 6.1.14 #4 Sat Mar 25 09:20:08 EDT 2023 riscv32 GNU/Linux
~ # ls /
bin      init     linuxrc  opt      run      tmp
dev      lib      media    proc     sbin     usr
etc      lib32    mnt      root     sys      var
~ #

Run 2: a transformer inside Linux inside a transformer

427,500,002 forward passes, 51,512,377 RISC-V instructions (8.30 tokens per instruction), 0 errors, 19,743 s at 21,654 tokens/s on one NVIDIA L4. We packed an initrd holding a 7.7 KB RV32IMA binary of a 4-bit, 5-layer Llama-2 model (dim 64, hidden 172, 8 heads, 4 KV heads, 512-token vocabulary, 150 KB of quantized weights, multiply-free INT4 lookup-table matmul), then ran it from the shell. It prints its story and returns cleanly to ~ #.

Why Qwen3.5: constant-size RAM inside Gated DeltaNet

A CPU built on softmax attention has to keep its memory in the KV cache, which grows with every token. At 427,500,002 tokens that would mean petabytes.

Qwen3.5 is a hybrid built on Gated DeltaNet linear-attention layers. Each one keeps a fixed-size recurrent state matrix S that updates in constant time and memory per token, whether it is token 1 or token 427,500,002. We use those states as RAM:

  • 10 GDN layers (L1 to L10) × 128 value heads × 1,024 key slots × 128 value dims, a 1.34 GB fp32 state that never grows.
  • Each key row stores two 32-bit words: 1.0 MB of Linux RAM per layer, 10.0 MB in total.
One standard Qwen3_5ForCausalLM, two subspaces
46 layers deep, 2,304 channels wide.
Depth
L0–L10bus, 10 MB RAM
L11–L21RV32IMA CPU, timer
L22–L45stock Qwen3.5-0.8B, untouched
Width
0–1023chat
1024–2303CPU buses
The CPU lives in its own layers and channels. During chat its channels stay at zero and its layers pass the chat stream straight through.
  • Depth, 46 layers. L0 to L21 hold the compiled CPU and RAM (13 linear-attention layers and 9 full-attention layers with zeroed attention projections). L22 to L45 are all 24 stock Qwen/Qwen3.5-0.8B layers.
  • Width, hidden_size = 2304. Channels 0 to 1023 carry the stock chat stream; channels 1024 to 2303 (1,244 of 1,280 used) carry the CPU buses.
  • Vocabulary, 313,856 tokens. Tokens 0 to 248,319 are stock Qwen3.5 text tokens. The remaining 65,536 encode a 5-bit bus command plus an 11-bit payload.
  • CPU footprint. 13,134 SwiGLU neurons and 102,431 nonzero MLP weights, 1.06 MB in all, compiled analytically by our HDL compiler with zero gradient steps.

Five bfloat16 tricks that made it boot

bfloat16 has a 7-bit mantissa, 128 steps per power of two, so a naive encoding of 32-bit arithmetic collapses at once. Five hardware tricks kept 427.5 million consecutive forward passes exact:

  1. An 8 × 4-bit carry-lookahead adder. A 32-bit weighted sum needs weights up to 231. Splitting the add into eight 4-bit nibbles caps every weight at 30, well inside bfloat16's exact range (4,000 of 4,000 random add, subtract and carry vectors exact).
  2. A 64-bit timer in one GDN head. Linux needs a real clock (clint_clocksource, HZ=100). One Gated DeltaNet head with a fixed decay acts as an RC oscillator that fires every 32 instructions and advances the 64-bit mtime register.
  3. Differential (12, 4) RAM. Each bit is stored as the pair (12, 4) for 1 and (4, 12) for 0, so every row has the same L2 norm, √10240, whatever it holds. RMSNorm never drifts, and a zero-bias readout cancels cross-talk exactly.
  4. Word assembly in the native conv1d. A 32-bit instruction or memory word arrives as three 11-bit tokens. L0's built-in causal conv1d(kernel_size=4) assembles them into one bus word in a single forward pass.
  5. Rescaled rms_norm_eps. Widening from 1,024 to 2,304 channels with the CPU channels at zero shrinks the RMS by √(1024/2304) = 2/3. Scaling rms_norm_eps from 1e-6 to 1.25e-7 and compensating the L22 to L45 norm scales cut chat divergence 364×, from KL 0.5533 to 0.0005–0.0021, with every CPU logit at 0.0000.

Exactness, and why one laptop core runs at 27,061 tokens/s

Three engines, one result
Each fast executor runs the same exported weights and matches HuggingFace PyTorch token for token.
3,800 / 3,800tokens exact in full 46-layer HF PyTorch, bfloat16
2,000 / 2,000tokens identical, CUDA and C++ executors vs HF
0.0005–0.0021chat KL vs stock Qwen3.5-0.8B
  1. Full 46-layer HuggingFace Qwen3_5ForCausalLM.forward() in bfloat16, 5.1 tokens/s. 1,800 of 1,800 tokens exact across the full RV32IMA, Zicsr, trap, MRET and UART instruction suite, and 2,000 of 2,000 live Linux boot tokens exact, including 11 timer ticks.
  2. Persistent single-SM CUDA executor (qwen_linux_cuda.cu), 21,654 tokens/s on one NVIDIA L4. It runs the literal exported weights and the literal 1.34 GB state tensor, and matches HF PyTorch on 2,000 of 2,000 tokens.
  3. Single-core C++ executor (qwen_linux_cpu.cpp), 27,061 tokens/s on one MacBook CPU core. No GPU, same exported weights and state, and it matches HF PyTorch on 2,000 of 2,000 tokens.
One laptop core outruns an L4
Tokens per second, log scale. Identical tokens on every engine.
1 MacBook CPU coreC++, no GPU
27,061
1 NVIDIA L4CUDA, 1 SM
21,654
Dense PyTorchHF forward, bfloat16
5.1
1101001k10k100k
About 5,300× faster than dense PyTorch on one laptop core, and 4,200× on the L4.

This is not a general speedup for dense language models. It comes from the structure of compiled circuits:

  • In the dense 17.75 GB checkpoint, more than 99.9% of the compiled CPU weights are exactly 0.0. Dense PyTorch multiplies every one of them, at 5.1 tokens/s.
  • All the nonzero CPU weights together take 1.06 MB (102,431 values).
  • On a GPU, sequential batch-1 steps over 1.06 MB spend most of their time on synchronisation and memory round trips: 46 µs per token, 21,654 tokens/s.
  • On one Apple Silicon core, the 1.06 MB sits entirely in L2 cache with no kernel launches: 37 µs per token, 27,061 tokens/s.

Skipping an exact zero changes nothing (x + 0 = x), so the fast executors produce the same token IDs as dense Qwen3_5ForCausalLM.forward(), and Linux inside Qwen boots on a laptop with no GPU.

Questions

Is Qwen3.5-0.8B modified inside its original 24 layers? No. The 24 stock layers are intact. The model is the standard Qwen3_5ForCausalLM class configured at 46 layers (L0 to L21 compiled CPU and RAM, L22 to L45 stock Qwen3.5-0.8B) and hidden_size = 2304.

Do any weights change at runtime? Never. Every weight is frozen. The 10.0 MB of Linux memory, the registers x0 to x31, the CSRs and the timer all live in the Gated DeltaNet recurrent states, updated by ordinary forward passes.

Why did one sentence of story take hours at 21,000 to 27,000 tokens/s? Each forward pass is about an eighth of one RISC-V clock cycle, an effective CPU clock of 3 to 4 kHz. Booting Linux takes about 26.85 million instructions (about 170 million tokens), and the inner 5-layer network takes about 24.6 million more (about 257 million tokens). Skipping zeros is the difference between a five-hour run and a 2.6-year dense PyTorch run.

Does the same checkpoint still chat normally? Yes. On English prompts L0 to L21 pass channels 0 to 1023 straight through, L22 to L45 run stock Qwen3.5-0.8B, CPU logits stay at 0.0000, and KL against unmodified Qwen/Qwen3.5-0.8B is 0.001379, 0.000539 and 0.002077.

Run it on your laptop

Clone the GitHub repo, download qwen_linux_weights.bin, ram_10mb.bin and ram_inception_10mb.bin from Hugging Face, then:

# build the C++ executor
clang++ -O3 -std=c++17 qwen_linux_cpu.cpp -o qwen_linux_cpu

# write a 2,000-token trace to compare against HuggingFace PyTorch
./qwen_linux_cpu qwen_linux_weights.bin ram_10mb.bin 809ff940 1000000 "" 2000

# boot Linux 6.1.14 on one core and save a snapshot at the shell prompt
./qwen_linux_cpu qwen_linux_weights.bin ram_10mb.bin 809ff940 50000000 "uname -a\nls /" 0 --save-prompt linux_prompt.snap

# Linux, then the inner 4-bit Llama-2 model
./qwen_linux_cpu qwen_linux_weights.bin ram_inception_10mb.bin 809fe000 60000000 "l" 0

Once linux_prompt.snap is saved at ~ #, --load-state linux_prompt.snap drops you back into the booted shell in under a second, ready for any command.

Links

More from the lab

All research