File size: 12,834 Bytes
4c5820e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Plan: 262K context on 6GB VRAM, matching Claude

**Goal.** `recursion + local model == frontier model`, under one hard constraint:
**6GB VRAM**. Parameter count is NOT constrained -- a 35B model whose experts stream
from system RAM is in scope, because only its resident footprint has to fit.

This was initially mis-scoped as "small model", which pushed everything toward 2-4B
models and made capability the bottleneck. The constraint is VRAM alone.

Two independent claims. Keeping them separate matters, because one is proven and
one is not:

| claim | status |
|---|---|
| A. 262K of context served within 6GB VRAM | **proven, reproduced 4x** |
| B. ...and it produces the correct answer | **MET** via ctxstream (2026-08-24) |

**Claim B was closed by removing the model from the aggregation step, not by a
bigger model.** See "How claim B was actually met" below. The MoE expert-offload
work is no longer on the critical path for correctness; it remains the route to
higher-quality *extraction* per fragment.

---

## Hardware

| machine | GPU | role |
|---|---|---|
| desktop | RTX 3080 20GB + Tesla P100 16GB | orchestration, Claude CLI, dev |
| `rickesh-Laptop` @ `10.147.20.104` | **RTX 3060 Laptop, 6144 MiB**, 62GB RAM | the constraint; all VRAM claims measured here |

The laptop runs Ollama 0.32.15 in a container (`ollama-bench`, reversible, no
system install). The desktop reaches it via
`ssh -f -N -L 11436:127.0.0.1:11434 rickesh@10.147.20.104`, so candidate runs use
the 3060 while the desktop GPU stays free. Set
`RLM_TEST_OLLAMA_URL=http://127.0.0.1:11436`.

---

## What is measured

**VRAM vs context on the 6GB card** (flash-attn + `q8_0` KV, matching the desktop
service config — without those the KV blows up and the direct arm is unfairly
handicapped):

| `num_ctx` | footprint | fits 5.5GB usable |
|---:|---:|:---:|
| 8,192 | 3.2 GB | yes |
| 16,384 | 3.3 GB | yes |
| **32,768** | **3.3 GB** | **yes — the ceiling** |
| 65,536 | 10.4 GB | no (31% GPU) |
| 131,072 | 10.9 GB | no (28% GPU) |

KV is nearly free to 32k, then the allocator falls off a cliff. **Direct maxes at
32,768 tokens.**

**RLM at 261,226 tokens on that card:** peak **4.23 / 4.24 / 4.25 GB** across three
runs. Fits, with ~1.2GB headroom. VRAM is flat in total context because the corpus
lives in CPU RAM as a REPL string and only chunk-sized slices reach the KV cache.
That is an **8x context multiple at constant VRAM**.

**Model ceilings.** `gemma4:e4b` = 131,072; `qwen3:4b` = 262,144 trained context.
1M is not reachable with these weights — that was the Claude `[1m]` variants, not
local GGUF.

**6GB-class candidates** (32k ctx, measured):

| model | footprint | fits 6GB | tool_ok |
|---|---:|:---:|---:|
| Gemma 4 E2B q4_0 | 1.8 GB | yes | 0.95 |
| **Gemma 4 E4B** | **3.5 GB** | yes | 0.90 |
| Qwen3 4B | 5.3 GB | marginal | 0.65 |
| Ornith 1.5 9B Q4_K_M | 6.2 GB | **no** | 0.95 |

E4B's 9.6GB GGUF is only 3.5GB resident (MatFormer / per-layer embeddings) — file
size badly overestimates VRAM here.

---

## What failed, and why it matters

**The reference is not a reliable oracle at this size.** Opus 4.8 [1m] over 439,742
tokens answered the *same* question two different ways on byte-identical input
(`Spatial Relationship` correct, `Normal scene Understanding` wrong), and is 2/3
across three samples. Ground truth must come from Python, with Opus scored as a
candidate. `bench/reference_reliability.py` quantifies this; it is cost-capped
because a call is $0.26 warm and $4.79 cold.

**The candidate's failure moved three times as plumbing was fixed:**

| attempt | calls | answer | defect |
|---|---:|---|---|
| 1 | 7 | prose | read ~33% of corpus |
| 2 | 11 | `Category: Spatial` | correct arithmetic, truncated label |
| 3 | **65** | `Category: Counterfactual` | swept corpus, well-formed, **wrong** |

Attempt 3 consumed 425,054 input tokens — it genuinely read everything and answered
in the required format. **The remaining gap is aggregation accuracy: a 4B correctly
combining 65 chunk-level counts.** That is a capability question, not plumbing.

### Guards added (none leak the answer)

| guard | catches |
|---|---|
| `require_repl` | answering from an empty REPL (measured: 1 call, pure guess) |
| `min_coverage=0.8` | answering from a partial read |
| `max_answer_chars` | returning the corpus instead of an answer |
| placeholder check | returning `Label: [least_common_status]` unsubstituted |
| `ContextTruncated` | Ollama silently clipping 50k→16,387 tokens |
| `QuotaExhausted` | rate limits scored as wrong answers |

`map_chunks(question)` was added to the REPL: it splits the entire context and
queries every chunk concurrently, collapsing the chunk-and-loop code a 4B fails to
write. It moved sub-calls from 17 → 75 and *reduced* wall-clock 16.6m → 7.8m.

### Scoring traps hit (both fixed)

- `gold in answer` passed a raw corpus dump because the dump contained the gold
  label. Strict scorers now reject replies with >2 ` || ` separators or over a
  length bound; the dump is a regression test.
- Caching a single sample of a non-deterministic reference froze a *wrong* answer
  as the oracle. The cache is now keyed on
  `sha256(model + instruction + context)`, so any change invalidates it.

---

## Remaining work

1. **Close claim B.** The candidate sweeps correctly but aggregates wrong. Options,
   cheapest first:
   - Make chunk-level output machine-parseable (ask each chunk for strict
     `label<TAB>count` lines) and aggregate in Python rather than trusting the
     model to combine 65 prose summaries.
   - Try E2B and Ornith 9B as the *root* (both scored 0.95 on tool behaviour vs
     E4B's 0.90); Ornith needs a card above 6GB or a smaller quant.
   - Root = Sonnet/Opus with sub-calls on the local 4B — the paper's own
     BrowseComp arrangement, untested here.
2. **Terminal-Bench baselines** — never produced a valid run. The ufw rule
   (`sudo ufw allow from 172.16.0.0/12 to any port 11435 proto tcp`) is in place and
   the bridge works; the suite needs re-running for E2B / E4B / Qwen3-4B.
3. **Quantify reference reliability** — run `bench/reference_reliability.py` for N
   samples to put a number on Opus's instability at 440k.
4. **Decide the finetune** — nothing is trained yet. Base and training data are
   both open (see below).
5. **MoE expert-offload — see the dedicated section below. This is the main line.**

---

## How claim B was actually met

`~/Projects/ctxstream` (C++17, zero third-party deps). The corpus is treated like
a video stream, and the model is removed from every step it was failing:

| video | ctxstream |
|---|---|
| manifest | segment plan, computed in code before any model call |
| buffer | N segments in flight |
| decoder | model sees ONE segment, emits `key<TAB>number`, never prose |
| playback | reduce — aggregation in code, strategy chosen explicitly |

Result on the 957,493-char imagined-risk corpus — the exact task that defeated
every earlier attempt, same 4B, same RTX 3060:

    17 segments · failed=0 · records=611 · unparsed_lines=3 · keys=15 · 641s
    Category: Spatial Relationship        <- gold, correct

Opus 4.8 [1m] on the same corpus: 2/3 correct, self-inconsistent on byte-identical
input, $4.79/call. The 4B on a 6GB laptop: correct, $0.00.

Still to confirm: the same pipeline against the full 262,144-token synthetic
ledger used by `bench/rlm_262k.py`, to show it holds at that size too.

---

## Main line: frontier-class quality inside 6GB via MoE expert-offload

The constraint is VRAM, not parameters. A Mixture-of-Experts model can put nearly
all of its weight in system RAM and keep only the always-active tensors resident,
so **35B-class quality is reachable inside 6GB**. This attacks the actual blocker
directly: a 4B cannot aggregate 65 chunk-level summaries; a 35B-A3B plausibly can.

### The candidate

`ornith-ai/Ornith-1.5-35B-A3B` (`Qwen3_5MoeForConditionalGeneration`), Q4_K_M =
21.7GB on disk. From its config: **40 layers, 256 experts, top-8 per token**,
`moe_intermediate_size` 512, and **30 of 40 layers use linear attention** (only 10
are full attention).

| component | params | ~Q4 size | placement |
|---|---:|---:|---|
| experts (256 x 3 x 2048 x 512 x 40) | ~32.2B | ~19.3 GB | **CPU RAM** |
| attention + embeddings + router + shared experts | ~1.7B | **~1.2 GB** | **GPU** |

Two properties make this unusually favourable: experts are tiny and fine-grained
(~1.9 MB each), and the 3:1 linear-attention ratio makes the KV cache far smaller
than a normal 35B — so long contexts cost little VRAM.

Laptop capacity as measured: **51 GB RAM available, 483 GB disk, 5.8 GB free VRAM.**
The 21.7 GB of experts fit in RAM with room to spare.

### Tooling

**Ollama cannot do this** — it exposes only `num_gpu` (a layer count), which would
naively push ~75% of layers to CPU including attention. The local llama.cpp build
already has the right flags:

    -cmoe,  --cpu-moe            keep ALL MoE weights on CPU
    -ncmoe, --n-cpu-moe N        keep MoE weights of the first N layers on CPU
    -ot,    --override-tensor    per-tensor placement by regex

`ghcr.io/ggml-org/llama.cpp:server-cuda` avoids building on the laptop.

### Steps

1. Pull `Ornith-1.5-35B-Q4_K_M.gguf` (21.7GB) to the laptop.
2. Run `llama-server` with `--n-gpu-layers 99 -cmoe`, so every layer's attention is
   on GPU and every expert FFN is on CPU. Measure resident VRAM; tune with
   `-ncmoe N` if there is headroom left under 6GB.
3. Add an OpenAI-compatible backend to `rlm_haiku/utils/llm.py` — llama-server
   serves `/v1/chat/completions`, not Ollama's `/api/chat`. Keep the
   `ContextTruncated` check: it needs a processed-token count from the response.
4. Re-run the equivalence test with this as the RLM **root** (sub-calls can stay on
   a cheap fast model, which is the paper's own BrowseComp arrangement).

### Known cost, stated up front

Expert streaming is memory-bandwidth-bound. Only ~3B params are active per token,
but a *different* ~600 MB of experts is touched every token, so throughput is set
by RAM bandwidth rather than compute — expect single-digit to low-double-digit
tokens/sec on a laptop. RLM multiplies that by ~65 sub-calls plus ~325k tokens of
prefill across chunks. **Minutes becomes tens of minutes to hours.** If quality
parity lands, that is the trade: VRAM and quality bought with wall-clock.

Fallbacks if throughput is unusable: `-ncmoe N` to pin as many expert layers on GPU
as fit (~4 GB spare / 1.9 MB per expert ~= 2,100 of 10,240 slots, so roughly 20%
resident); or a smaller MoE. True per-token LRU expert paging is what would help
most, and llama.cpp does not implement it (ktransformers, MoE-Infinity do).

## Open decisions

- **Finetune base and data.** Deferred pending baselines. Worth knowing:
  `empero-ai/Qwable-9B-Claude-Fable-5` already exists — Qwen3.5-9B fully fine-tuned
  on Fable-5 traces + `gpt5.5-terminal`, tagged `agentic-coding`. That is close to
  what we were going to build; evaluate it before training anything.
- **Release scope.** `Rickesh/rlm-oolong-reproduction` is published (harness +
  results, MIT, no weights). Nothing is trained, so there is no model release yet.
  Ollama.com namespace is separate from HF and needs your handle.

## Non-obvious findings worth keeping

- Ollama auto-sizes context to the prompt up to a ceiling, then **silently** falls
  back: 30,021 tokens pass intact, 50k and 70k both clip to exactly 16,387, with no
  error. `num_ctx` is **ignored** on the Anthropic-compatible `/v1/messages` and
  honoured on native `/api/chat`, which is also the only route reporting
  `prompt_eval_count` — the thing that makes truncation detectable.
- Terminal-Bench puts each task on its own compose network, so firewall rules
  scoped to `docker0` do not match. Scope by source subnet.
- RLM *hurts* when the context fits the window: OOLONG-131k, Haiku, 0.269 recursive
  vs 0.428 direct. It only pays when the context does not fit.
- The "behave like Claude" system prompt bought **zero** tool-selection accuracy
  across 4 models (deltas 0.00 / 0.00 / -0.05 / -0.05) while halving output tokens.

## Running things

```bash
# fast checks, no model calls
python -m pytest tests/test_equivalence.py -v

# equivalence, candidate on the 3060 (reference is cached; $0)
RLM_TEST_OLLAMA_URL=http://127.0.0.1:11436 \
  python -m pytest tests/test_equivalence.py -v -m integration -s

# 262k on the 6GB card (run ON the laptop)
python3 bench/rlm_262k.py --model gemma4:e4b --target-tokens 262144 \
  --num-ctx 8192 --max-prompt-chars 20000 --workers 2

# VRAM curve
python3 bench/vram_curve.py --model gemma4:e4b --budget-gb 5.5

# ask a question about an oversized file with a local model
python3 rlm_ask.py --file huge.log --query "which error appears most often?"
```