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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
library_name: pytorch
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
tags:
|
| 8 |
+
- text-generation
|
| 9 |
+
- stream-mixer
|
| 10 |
+
- linear-time
|
| 11 |
+
- recurrent
|
| 12 |
+
- attention-free
|
| 13 |
+
- nanochat
|
| 14 |
+
- small-llm
|
| 15 |
+
datasets:
|
| 16 |
+
- karpathy/climbmix-400b-shuffle
|
| 17 |
+
- HuggingFaceTB/smol-smoltalk
|
| 18 |
+
- cais/mmlu
|
| 19 |
+
- allenai/ai2_arc
|
| 20 |
+
- openai/gsm8k
|
| 21 |
+
base_model: karpathy/nanochat
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
# Mnemo
|
| 25 |
+
|
| 26 |
+
> *μνήμη — Greek for "memory"*
|
| 27 |
+
|
| 28 |
+
**Mnemo** is a small attention-free language model with 117M parameters, built on the
|
| 29 |
+
**Stream Mixer** architecture — a linear-time recurrent sequence mixer that uses
|
| 30 |
+
multiple parallel content-routed memory streams instead of self-attention. The name
|
| 31 |
+
nods to the model's recurrent memory: every layer maintains M parallel state buffers
|
| 32 |
+
that "remember" content over the entire sequence without quadratic attention.
|
| 33 |
+
|
| 34 |
+
The training pipeline (data, tokenizer, eval, fine-tuning) is a fork of
|
| 35 |
+
[karpathy/nanochat](https://github.com/karpathy/nanochat), with the attention-based
|
| 36 |
+
GPT replaced by a custom Stream Mixer block.
|
| 37 |
+
|
| 38 |
+
---
|
| 39 |
+
|
| 40 |
+
## Quick facts
|
| 41 |
+
|
| 42 |
+
| | |
|
| 43 |
+
|---|---|
|
| 44 |
+
| Architecture | Stream Mixer (linear-time recurrent) |
|
| 45 |
+
| Parameters | **117,179,136** |
|
| 46 |
+
| Layers | 16 |
|
| 47 |
+
| Hidden dim | 768 |
|
| 48 |
+
| Memory streams (M) | 48 |
|
| 49 |
+
| Stream state dim (D) | 96 |
|
| 50 |
+
| Read heads | 6 |
|
| 51 |
+
| Context length | 2048 tokens |
|
| 52 |
+
| Vocab | 32,768 BPE (GPT-4-style pretokenization) |
|
| 53 |
+
| Special tokens | `<\|bos\|>`, `<\|user_start\|>`, `<\|user_end\|>`, `<\|assistant_start\|>`, `<\|assistant_end\|>` |
|
| 54 |
+
| Compute dtype | bf16 (Ampere+) / fp32 (T4/CPU) |
|
| 55 |
+
| License | MIT |
|
| 56 |
+
|
| 57 |
+
---
|
| 58 |
+
|
| 59 |
+
## Architecture: Stream Mixer
|
| 60 |
+
|
| 61 |
+
Mnemo's defining feature is its sequence mixer. Where a Transformer uses self-attention
|
| 62 |
+
to compute pairwise interactions across tokens (cost: **O(T²)**), Mnemo uses a chunked
|
| 63 |
+
parallel scan over M parallel content-routed memory streams (cost: **O(T · M · D)** —
|
| 64 |
+
**linear in sequence length**).
|
| 65 |
+
|
| 66 |
+
Per token *t* and per layer:
|
| 67 |
+
|
| 68 |
+
1. Compute value `v[t]`, read query `q[t]`, content-router `r[t]`, and per-stream decay `α[t]`.
|
| 69 |
+
2. Each memory stream `s_m` updates via `s_m[t] = α_m[t] · s_m[t-1] + r_m[t] · v[t]`.
|
| 70 |
+
3. Multi-head sigmoid-gated read with QK-norm aggregates from the M streams.
|
| 71 |
+
|
| 72 |
+
The full state across a layer is **(B, M, D)** — a fixed-size recurrent memory that
|
| 73 |
+
the model can carry across arbitrary sequence lengths. The chunked scan implementation
|
| 74 |
+
keeps numerical range bounded even for slow-decay streams.
|
| 75 |
+
|
| 76 |
+
For details see the model source.
|
| 77 |
+
|
| 78 |
+
---
|
| 79 |
+
|
| 80 |
+
## Training
|
| 81 |
+
|
| 82 |
+
### Pretraining (base model)
|
| 83 |
+
|
| 84 |
+
| | |
|
| 85 |
+
|---|---|
|
| 86 |
+
| Corpus | [karpathy/climbmix-400b-shuffle](https://huggingface.co/datasets/karpathy/climbmix-400b-shuffle) — 88 shards |
|
| 87 |
+
| Total tokens | **5.24B** (44.7× over params) |
|
| 88 |
+
| Steps | 80,000 × B=32 × T=2048 |
|
| 89 |
+
| Optimizer | AdamW (peak LR 1e-3, warmup 500, cosine to 1e-5, weight decay 0.1) |
|
| 90 |
+
| Compute | RTX PRO 6000 Blackwell (single GPU, bf16) |
|
| 91 |
+
| Wall time | **~9 hours** |
|
| 92 |
+
| Best val loss | **2.9508** (perplexity ≈ 19.12) |
|
| 93 |
+
|
| 94 |
+
### Supervised fine-tuning
|
| 95 |
+
|
| 96 |
+
| | |
|
| 97 |
+
|---|---|
|
| 98 |
+
| Mixture | SmolTalk + MMLU×3 + ARC×4 + GSM8K×4 + SimpleSpelling + SpellingBee + 1000 Mnemo-branded identity convs |
|
| 99 |
+
| Total conversations | ~1.09M |
|
| 100 |
+
| Steps | 30,000 × B=8 × T=2048 = ~500M SFT tokens |
|
| 101 |
+
| Optimizer | AdamW (peak LR 1e-4, warmup 300) |
|
| 102 |
+
| Best val loss | ~1.45 (masked cross-entropy over assistant tokens only) |
|
| 103 |
+
| Format | nanochat-style BOS-aligned best-fit packing with padding |
|
| 104 |
+
|
| 105 |
+
### Pipeline
|
| 106 |
+
|
| 107 |
+
```
|
| 108 |
+
ClimbMix-400B
|
| 109 |
+
│
|
| 110 |
+
â–¼
|
| 111 |
+
[80k step pretrain on Stream Mixer]
|
| 112 |
+
│ best val 2.9508 @ step 79k
|
| 113 |
+
â–¼
|
| 114 |
+
Base checkpoint (completes prompts)
|
| 115 |
+
│
|
| 116 |
+
â–¼
|
| 117 |
+
[30k step SFT on multi-task mixture]
|
| 118 |
+
│ best val ~1.45
|
| 119 |
+
â–¼
|
| 120 |
+
SFT checkpoint (chat-aware — answers as Mnemo)
|
| 121 |
+
```
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
|
| 125 |
+
## Capabilities and limitations
|
| 126 |
+
|
| 127 |
+
### Capabilities (validated on the standard probe set)
|
| 128 |
+
|
| 129 |
+
After SFT, Mnemo reliably handles:
|
| 130 |
+
- Conversational dialogue in chat format (`<|user_start|>` / `<|assistant_start|>` delimiters)
|
| 131 |
+
- Common factual recall: capital cities, chemical symbols, planets, basic vocabulary
|
| 132 |
+
- Multiple-choice questions (MMLU/ARC format)
|
| 133 |
+
- Step-by-step arithmetic in the GSM8K style (with occasional wrong answers)
|
| 134 |
+
- Letter counting via manual enumeration (SpellingBee)
|
| 135 |
+
- Identity Q&A consistent with its training persona
|
| 136 |
+
|
| 137 |
+
### Limitations
|
| 138 |
+
|
| 139 |
+
- **117M parameters** — confabulates confidently on niche facts, dates, and proper nouns
|
| 140 |
+
- **No tool use, no internet, no images, no memory across sessions**
|
| 141 |
+
- **2048-token context** — not pretrained for longer contexts; quality degrades past ~1500 tokens
|
| 142 |
+
- **No RLHF** — outputs reflect only supervised signal; may produce inappropriate completions
|
| 143 |
+
- **English only** — pretraining corpus is essentially English educational/web text
|
| 144 |
+
- **Repetition prone in long generations** without `--repetition-penalty` or top-p
|
| 145 |
+
- **Math beyond GSM8K-level** is unreliable; arithmetic gets shaky past 3-digit operands
|
| 146 |
+
|
| 147 |
+
---
|
| 148 |
+
|
| 149 |
+
## Usage
|
| 150 |
+
|
| 151 |
+
### Direct loading
|
| 152 |
+
|
| 153 |
+
```python
|
| 154 |
+
import torch
|
| 155 |
+
from tokenizers import Tokenizer
|
| 156 |
+
from model import GPT
|
| 157 |
+
|
| 158 |
+
tokenizer = Tokenizer.from_file('tokenizer.json')
|
| 159 |
+
ckpt = torch.load('model_sft.pt', map_location='cuda')
|
| 160 |
+
|
| 161 |
+
config = dict(ckpt['config'])
|
| 162 |
+
config['vocab_size'] = ((tokenizer.get_vocab_size() + 63) // 64) * 64
|
| 163 |
+
model = GPT.from_config(config).cuda().eval()
|
| 164 |
+
|
| 165 |
+
state = {k.removeprefix('_orig_mod.'): v for k, v in ckpt['model'].items()}
|
| 166 |
+
model.load_state_dict(state, strict=False)
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
### Chat CLI (recommended)
|
| 170 |
+
|
| 171 |
+
```bash
|
| 172 |
+
python3 chat_cli.py # interactive REPL
|
| 173 |
+
python3 chat_cli.py -p "Who are you?" # one-shot
|
| 174 |
+
```
|
| 175 |
+
|
| 176 |
+
The chat CLI handles the chat-format token wrapping (`<|bos|>` → `<|user_start|>` …)
|
| 177 |
+
and stops generation cleanly on `<|assistant_end|>`. State is cached across turns
|
| 178 |
+
via the recurrent state buffer — only the new tokens of each user message are
|
| 179 |
+
prefilled, giving roughly **5–10× faster prefill** on multi-turn conversations than
|
| 180 |
+
re-processing the entire history.
|
| 181 |
+
|
| 182 |
+
### Raw inference (no chat format)
|
| 183 |
+
|
| 184 |
+
```bash
|
| 185 |
+
python3 infer.py -p "Photosynthesis is the process by which" --top-p 0.9 -r 1.15
|
| 186 |
+
```
|
| 187 |
+
|
| 188 |
+
Recommended sampling parameters (empirically tuned, see training log):
|
| 189 |
+
- **Greedy / factual probes**: `-t 0`
|
| 190 |
+
- **Short prose (≤500 tok)**: `-t 0.8 -k 50`
|
| 191 |
+
- **Long prose (500–2000 tok)**: `-t 0.8 -k 50 --top-p 0.9 -r 1.15` (anti-loop)
|
| 192 |
+
- **Diverse creative writing**: `-t 0.9 --top-p 0.85 -r 1.1`
|
| 193 |
+
|
| 194 |
+
---
|
| 195 |
+
|
| 196 |
+
## Probe outputs (greedy, from the base checkpoint)
|
| 197 |
+
|
| 198 |
+
| Prompt | Output | Verdict |
|
| 199 |
+
|---|---|---|
|
| 200 |
+
| The capital of France is | "...Paris, and the capital of France is Paris" | ✓ |
|
| 201 |
+
| The chemical symbol of gold is | "Au. It is a soft, silvery-white metal... good conductor of electricity and heat, useful in electrical wiring" | ✓ (Au correct; "silvery-white" wrong color) |
|
| 202 |
+
| If yesterday was Friday, then tomorrow will be | "Tuesday" | ✗ (correct: Sunday) |
|
| 203 |
+
| The opposite of hot is | "the cold" | ✓ |
|
| 204 |
+
| The planets of the solar system are: | "Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto" | ✓ (correct order) |
|
| 205 |
+
| My favorite color is | "blue" | ✓ |
|
| 206 |
+
| If 5\*x + 3 = 13, then x is | "a positive integer" loop | ✗ |
|
| 207 |
+
|
| 208 |
+
**5/7 correct on the base model.** SFT improves chat-format adherence, MCQ accuracy, and persona consistency.
|
| 209 |
+
|
| 210 |
+
---
|
| 211 |
+
|
| 212 |
+
## Citation and acknowledgements
|
| 213 |
+
|
| 214 |
+
Built on top of [karpathy/nanochat](https://github.com/karpathy/nanochat) by Andrej Karpathy.
|
| 215 |
+
The Stream Mixer architecture is an attention-free experiment swapping the standard
|
| 216 |
+
Transformer block for a recurrent linear-time sequence mixer.
|
| 217 |
+
|
| 218 |
+
Pretraining data is [karpathy/climbmix-400b-shuffle](https://huggingface.co/datasets/karpathy/climbmix-400b-shuffle).
|
| 219 |
+
SFT mixture sources: HuggingFaceTB/smol-smoltalk, cais/mmlu, allenai/ai2_arc, openai/gsm8k,
|
| 220 |
+
and a custom 1000-conversation identity dataset.
|
| 221 |
+
|
| 222 |
+
```bibtex
|
| 223 |
+
@misc{mnemo2026,
|
| 224 |
+
title={Mnemo: A Linear-Time Recurrent Language Model},
|
| 225 |
+
author={Alvarado, Luis Miguel},
|
| 226 |
+
year={2026},
|
| 227 |
+
note={Built on karpathy/nanochat. Stream Mixer architecture.},
|
| 228 |
+
howpublished={\url{https://github.com/<your-handle>/mnemo}}
|
| 229 |
+
}
|
| 230 |
+
```
|
| 231 |
+
|
| 232 |
+
---
|
| 233 |
+
|
| 234 |
+
## License
|
| 235 |
+
|
| 236 |
+
MIT. Use freely. No warranty.
|