Ternary MatMul-free language models trained on TinyStories, for matmul-free-lm.c: train in PyTorch, inference with one simple C file. The architecture is HGRN-Bit from ridgerchu/matmulfreellm: every linear layer has ternary weights in {-1, 0, +1} and int8 activations, and tokens are mixed by an elementwise gated recurrence instead of attention.
The model series mirrors the Llama 2 models of llama2.c (karpathy/tinyllamas): same data, same number of training tokens, same context length.
| model | dim | n_layers | hidden_dim | max context length | parameters | val loss | llama2.c val loss |
|---|---|---|---|---|---|---|---|
| stories260K | 64 | 4 | 160 | 512 | 257K | 1.609 | 1.297 |
| stories1M | 128 | 5 | 272 | 512 | 988K | 1.199 | |
| stories15M | 288 | 6 | 768 | 256 | 15M | 1.252 | 1.072 |
| stories42M | 512 | 8 | 1376 | 1024 | 42M | 0.847 | |
| stories110M | 768 | 12 | 2048 | 1024 | 110M | 0.760 |
The val loss is measured on 1,048,576 tokens of the TinyStories validation shard in fp32 (eval/ in each folder). The 260K and 1M models use the 512-token vocabulary of stories260K (tok512.model, shipped with them), the others the Llama 2 tokenizer.
Each folder holds:
config.json, model.safetensors, tokenizer files): an HGRNBitForCausalLM, loadable with the upstream code, or with python export.py model.bin --hf <folder> after downloadstories<size>.pt: the PyTorch checkpoint written by train.pystories<size>.bin: the checkpoint for run.cgit clone https://github.com/ruhai-lin/matmul-free-lm.c.git
cd matmul-free-lm.c
wget https://huggingface.co/ruhai-lin/matmul-free-lm.c/resolve/main/stories15M/stories15M.bin
make run
./run stories15M.bin
stories15M/stories15M.pt 61 MBstories15M/model.safetensors 61 MBstories15M/stories15M.bin 52 MBstories1M/stories1M.pt 4 MBstories1M/model.safetensors 4 MBstories1M/stories1M.bin 1 MBstories260K/stories260K.pt 1 MBstories260K/model.safetensors 1 MBstories260K/stories260K.bin 364.7 kBstories1M/tok512.bin 6.2 kBstories260K/tok512.bin 6.2 kB