rule-lm: a language model with no neural network
A language model whose "weights" are tables a program builds by counting and indexing text, with no gradient descent and no neural network anywhere. It is built as the explicit analogue of a neural LM:
- the n-gram tables and the knowledge base play the role of the MLP (stored knowledge);
- the access rules (retrieval, working memory, copy pointer) play the role of attention (what to use from context).
It was trained on the same data as a small neural reference (nanochat d8, 126M parameters) and compared against it.
flowchart LR
ctx[context tokens] --> ng[n-gram tables<br/>orders 1-4, KN]
ctx --> sa[suffix array<br/>orders 5-32]
ctx --> cls[word classes<br/>class 6-grams]
ctx --> doc[document memory]
ctx -->|every 8 tokens| acc[access step<br/>BM25 over knowledge base<br/>+ tf-idf over documents]
acc --> wm[working memory<br/>retrieved passages]
wm --> ptr[copy pointer<br/>follows a passage]
ng & sa & cls & doc & ptr --> mix[mixture + topic rescaling<br/>+ anti-loop rule]
mix --> p[next-token distribution]
Quick start
pip install -r requirements.txt
python download.py p3-16m-alpaca # small: 16M-token model (1.6 GB) + Alpaca chat fine-tune
python chat_cli.py --model models/p3-16m-alpaca
python chat_cli.py --model models/p3-16m --complete # prompt continuation with the pre-trained model
python chat_cli.py --model models/p3-16m-alpaca --show-plan # show what the chat engine understood and planned
In the chat: /reset starts a new conversation, /complete and /chat switch mode, /temp 0.5 and /max 200 change
decoding, /quit exits. Loading is memory-mapped and takes seconds; the first replies are slow while the tables are
paged in from disk.
Models ("weights")
| name | what | size |
|---|---|---|
p3-16m |
pre-trained on the first 16M tokens of ClimbMix | 1.6 GB |
p3-16m-alpaca |
p3-16m + chat fine-tune on Alpaca-52k (a delta; download.py fetches the base too) |
0.4 GB |
p3-1b |
pre-trained on 1.07B tokens of ClimbMix, the same data as the neural reference | 58 GB |
p3-1b-alpaca |
p3-1b + chat fine-tune on Alpaca-52k (delta) |
0.4 GB |
p3-2b |
pre-trained on 2.15B tokens of ClimbMix (knowledge base 74M sentences); its neural reference is d8 at 2.1B tokens | 97 GB |
A fine-tune is stored as a delta: a directory with base.json pointing at its pre-trained model plus only the files
the fine-tune added. The loader combines them through symlinks, so one large base serves many fine-tunes.
What is in a model directory
Everything is numpy arrays and text, memory-mapped at prediction time. For p3-1b:
| file prefix | what | size |
|---|---|---|
o{1..4}_* |
hashed Kneser-Ney n-gram tables, orders 1-4 (sorted 64-bit keys, counts, continuation counts) | 15.8 GB |
sa.npy, tokens.npy |
suffix array over the training tokens (longer contexts, and the copy pointer) | 6.4 GB |
cls.npy, c{1..6}_* |
256 word classes from the exchange algorithm + class 6-gram tables | 21.4 GB |
ix_* |
tf-idf document index (document retrieval) | 7.4 GB |
kb_* |
knowledge base: 36.9M corpus sentences, BM25 over word stems, 3.6M definition entries | 6.3 GB |
chatlm/, chat_style.json |
fine-tunes only: a chat-only n-gram LM and the reply style learned from the conversations | 0.4 GB |
Train, fine-tune, evaluate
# 1. data: tokenize ClimbMix shards (https://huggingface.co/datasets/karpathy/climbmix-400b-shuffle) or any text
python prepare_data.py --out data/climbmix --max-tokens 1100000000 shard_00000.parquet shard_00001.parquet ...
python prepare_data.py --out data/heldout --max-tokens 2000000 some_other_shard.parquet # text you won't train on
# 2. pre-train: count and index the first N tokens (one process; 1M tokens ~30 s / 1 GB RAM, 1B tokens ~10 h / 54 GB)
python train.py --data data/climbmix --budget 16000000 --out models/my-16m
# 3. chat fine-tune (Alpaca parquet, Dolly jsonl, or {"messages": [...]} jsonl); --delta stores only the new files
python sft.py --base models/my-16m --data alpaca/train.parquet --out models/my-16m-alpaca --delta
# 4. evaluate: held-out bits per byte, scored token by token
python eval_bpb.py --model models/my-16m --data data/heldout --docs 200 --workers 8
# 5. talk to it
python chat_cli.py --model models/my-16m-alpaca
The tokenizer is nanochat's 32,768-token BPE, shipped as plain JSON in tokenizer/ and loaded with tiktoken.
Results
Same training data (ClimbMix) for both models. The neural reference is nanochat d8, a 126M-parameter transformer.
Held-out bits per byte (nanochat validation documents; lower is better):
| training tokens | 1M | 4M | 16M | 64M | 256M | 1.07B |
|---|---|---|---|---|---|---|
| rule LM (best-bpb variant) | 1.72 | 1.58 | 1.48 | 1.41 | 1.34 | 1.27 |
| neural d8 | 2.09 | 1.86 | 1.38 | 1.16 (at 50M) | 1.01 | 0.95 |
- Small data: the rule LM is better than every neural size tried below about 11M tokens, and better per unit of training energy below about 7 Wh.
- Large data: the network wins, with a steeper scaling law and a far smaller state. The rule LM's tables grow with the data (51 GB at 1.07B tokens, against 252 MB of weights), so its knowledge is indexed rather than compressed.
Other comparisons at 1.07B tokens:
| evaluation | rule LM | neural d8 |
|---|---|---|
| CORE-style benchmark score, log-likelihood, 13 tasks (MMLU, ARC, HellaSwag, PIQA, ...; 0 = chance) | 0.056 (held-out test split: 0.058) | 0.078 (d8 at 256M tokens: 0.044) |
| HumanEval pass@1 | 3.7% | 3.7% |
| MBPP pass@1 | 7.0% | 6.8% |
| chat (Alpaca SFT for both), judged head-to-head | 20% wins | 80% wins |
| text continuation, judged head-to-head | up to 34% wins (best design so far) | the rest |
On HumanEval and MBPP both models use the same retrieval and self-test search, which supplies nearly all of the solves. SWE-bench Verified: 0/30.
Limitations
- Replies are often off-topic or stitched together from retrieved sentences. Even at 1.07B tokens it is far below a neural chat model.
- It reproduces training text: the copy pointer follows retrieved passages verbatim.
- Memory and disk grow linearly with the training data.
Data and licenses
The pre-training data is ClimbMix, through nanochat's shuffled copy karpathy/climbmix-400b-shuffle. The chat
fine-tunes use Alpaca-52k (tatsu-lab/alpaca). Both datasets carry their own licenses, which apply to models derived
from them; see their dataset cards. The models contain sentences from these datasets verbatim, in the knowledge base
and the token array.