Compiling token spellings into weight space for character-level tasks in frozen models
str | aw | berry, the highlighted bars add up to three r’s. The demo uses Qwen3’s recorded tokenization.
How many r’s are in strawberry? Qwen3’s tokenizer splits the quoted word into str | aw | berry. The model receives three IDs and looks up learned vectors. To count letters, it has to recover the spelling from those vectors. The tokenizer already has the exact text.
BPE X-Ray puts that spelling lookup inside the model. I compile the tokenizer’s vocabulary into fixed tables indexed by token ID. Looking up the three tokens gives 1 + 0 + 2 = 3 occurrences of r. That’s the weight-space intervention. The lookup runs in the forward pass while the pretrained weights stay frozen.
The X-Ray ALU computes and writes the answer when it sees an [XRAY …] tag, with no training. The Word Retina uses trained adapters to feed spelling features into hidden layers, so the model can answer a plain-language question about a quoted word.
With the input IDs fixed, lowering berry’s r count by one changes the model’s answer from 3 to 2. Try it in the demo.
Byte-pair encoding
I built the benchmark from 418 training words and 162 held-out English words, with no overlap between the sets. The four perceptual tasks ask the model to count letters, measure word length, find the nth letter, or detect an adjacent double. The other three ask it to delete a letter, replace a letter, or reverse the word. I also test unfamiliar strings, alternate token splits, and tasks held out from training.
Frozen Qwen3-0.6B-Basereverse and rot13. On the same six-task, two-shot evaluation, Qwen3-1.7B reaches 34.7%.
A tokenizer is a finite map from token ID to string. I read it once, offline, and compile a frozen CharWeightBank (src/char_tables.py) of three tables indexed by token ID:
E_hist [V, 26] holds case-insensitive letter counts (7.9 MB in bf16 for Qwen's 151,936 tokens).E_alpha_chars [V, L_max] stores ordered letter codes up to the table’s character limit. E_alpha_len stores the alphabetic length.F_retina [V, 358] holds a richer per-token sensory vector, laid out in the figure below.
The tokenizer still converts the prompt into token IDs. Once those IDs enter the forward pass, the modules read their character features by indexing frozen tensors, for example F_retina[input_ids].
F_retina for one token. Hover a cell to see what it encodes. Each 27-way block is a one-hot with slot 0 meaning "none". The vector is computed in your browser with the same rules as char_tables.py; the token IDs are Qwen3's.Each row describes one token. A word may span several rows. The ALU combines them after recognising a tagged call. The retina combines them as it scans a quoted span.
The ALU is a tensor circuit attached to the host’s final RMSNorm. It follows the read, compute, and write recipe from Weight-Space Micromachines]= activates the circuit. Until then, the hook returns the hidden state untouched. In my checks, logits on ordinary prose and before the trigger were bit-identical to the base model’s.
[XRAY, the operation, an optional argument, |, and ]=.count and len sum E_hist or E_alpha_len over the span. nth, spell, reverse and rot13 use cumulative lengths and ReLU indicator gates to select a token and a slot inside it. ROT13 is a fixed 26 × 26 permutation.W_write[t] against lm_head. Adding γ·W_write[t] then makes t the argmax.γ·W_write[t] after the final norm so that t becomes the argmax. The 28 transformer layers are never modified.Below, scrub through generation of one call. Each step shows the prefix-sum span state, whether the ALU is active, and what it writes.
" strawberry", with its leading space, is a single token (72600): the base model receives no sub-word pieces at all, and answers 2.
Even turning a count into decimal digits is ReLU arithmetic. Python's str(n) is never called. An indicator relu_step(x, t) = ReLU(x − t + 1) − ReLU(x − t) is exactly 1 for integers x ≥ t and 0 otherwise. Summing nine of them gives the hundreds digit; the remainder repeats the trick for tens.
Σ relu_step(n, 100c) for c = 1…9, and it is exact on every integer from 0 to 999. Drag n; this is the real computation, run in your browser.def relu_step(x, t): # 1 iff integer x >= t
return F.relu(x - (t - 1)) - F.relu(x - t)
d_hundreds = sum(relu_step(n, 100 * c) for c in range(1, 10))
rem = n - 100 * d_hundreds
d_tens = sum(relu_step(rem, 10 * c) for c in range(1, 10))
d_ones = rem - 10 * d_tens
I ran the same compiler, unchanged, on three models from two families with different tokenizers. Each was evaluated 2-shot, with in-context examples teaching only the tag format and no gradient steps. I also checked each circuit directly on all 13 suites.
reverse and rot13. Hover for values.| Model | Compile | Prose logits | Held-out (6 tasks) | Pseudowords | Multi-word | Direct |
|---|---|---|---|---|---|---|
| Qwen3-0.6B-Base | 28.8 s | bit-identical | 25.0 → 100.0 | 22.3 → 92.0 | 28.0 → 100.0 | 100.0 |
| Qwen3-1.7B-Base | 12.6 s | bit-identical | 34.7 → 100.0 | 29.3 → 93.0 | 34.0 → 100.0 | 100.0 |
| SmolLM2-1.7B | 5.2 s | bit-identical | 29.7 → 99.7 | 18.3 → 94.7 | 30.0 → 100.0 | 100.0 |
The gap on pseudowords (92–95% vs. 100% direct) is a copying error, not a circuit error. Under 2-shot prompting the base model sometimes mis-copies an unfamiliar string into the tag before the ALU fires. Once triggered, the ALU made no mistakes on any suite, on any of the three models.
results/alu_eval_Qwen_Qwen3-0.6B-Base.json.
The Word Retina uses the same tables to answer natural-language questions about quoted words. For How many times does the letter 'r' appear in 'strawberry'?, the scanner computes the count and trained adapters feed features into the model’s hidden layers. I test whether the frozen host can use them to produce the answer as its next token, without a special tag or a written chain of thought.
Every design below was trained on Qwen3-0.6B-Base with 4,096 word-disjoint examples over the seven tasks. All are compared against a plain LoRA
F_retina. Each panel reports perceptual accuracy, the accuracy drop when the feature path is turned off, and the fraction of count edits the model follows. The Word Retina shows the strongest dependence on the compiled features.
Layer-0 injection (src/xray_retina.py) projects F_retina[ids] into the input embeddings alongside a LoRA (10.81M parameters). It scores 47.9% on held-out words and 58.1% on pseudowords, slightly below plain LoRA (49.5 / 60.7). Ablating the retina costs just 2.4 points. At layer 0, the token embedding for berry is still independent of the question. The small ablation drop suggests that the trained model relied little on this feature path.
Generic cross-attention at layers 14, 20 and 24 (V1, src/visual_cortex.py) attaches where the residual stream already encodes the query. The host now depends on it (−17.1 points when ablated), but accuracy stalls at 35.0%. A generic attention module has to learn subword-to-word aggregation from 4,096 examples.
Cross-token doubles offered a clue. In a V1 variant with LoRA, accuracy on that test reached 78.8% and fell by 30 points when I turned off the feature path. I had already precomputed the double-letter signal across token boundaries. That led me to compute the rest of the word-level features explicitly and train the adapters to use those features.
WordRetinaScanner (src/word_retina.py) is a causal, gated linear recurrence over F_retina[ids] and E_alpha_chars[ids]. Inside a quoted span it accumulates the word's letter histogram and length. It detects doubles across token boundaries with one product, prev_last · first, and places each letter into a 16 × 26 grid at its cumulative offset. It also forms query readouts such as matched_cnt, matched_nth and is_anagram, for 623 dimensions in all.
The state enters the frozen host through three SwiGLU-styleLatentIntuitionTap) at layers 16, 20 and 24:
h′ = h + α · Wout( SiLU(Wg2 h̃) ⊙ Wmix( SiLU(Wg1 h̃) ⊙ Ws S ) )
The taps total 6.47M trainable parameters, 57% of the LoRA budget. Each output projection is zero-initialised, so the model is bit-identical at step 0, and a KL term on prose during training discourages changes to the base model’s output distribution.
| Qwen3-0.6B, n = 4,096 | Params | Host | Perceptual* | All 7, held-out | All 7, pseudo | Cross-token dbl | OOD multi-word | ΔPPL† |
|---|---|---|---|---|---|---|---|---|
| Frozen base | 0 | frozen | 32.1 | 19.1 | 10.7 | 50.0 | 25.0 | 0.00 |
| Plain LoRA, r = 18 | 11.35M | adapted | 71.7 | 49.5 | 60.7 | 70.0 | 38.3 | +5.08 |
| Layer-0 retina + LoRA | 10.81M | adapted | 70.8 | 47.9 | 58.1 | 60.0 | 43.3 | +7.72 |
| Generic V1 | 8.35M | frozen | 55.0 | 35.0 | 43.6 | 46.3 | 33.3 | +0.94 |
| BPE-V2 Word Retina | 6.47M | frozen | 97.1 | 60.0 | 63.6 | 83.8 | 73.3 | +0.31 |
| BPE-V2 + LoRA | 11.51M | adapted | 96.7 | 63.8 | 70.0 | 82.5 | 71.7 | +0.27 |
* Count, length, nth and double on held-out words; frozen V2 per task: 98.3 / 91.7 / 100.0 / 98.3. On compound pseudowords (perceptual / all 7): LoRA 64.4 / 44.3, V1 46.9 / 31.1, V2 96.9 / 58.6, V2 + LoRA 93.8 / 62.1. OOD anagram: base 38.3, LoRA 58.3, layer-0 63.3, V1 40.0, V2 61.7, V2 + LoRA 38.3. † Held-out prose perplexity shift. The LoRA and layer-0 arms were evaluated on a different prose sample (base PPL 25.9) from the V1/V2 arms (30.2–30.4).
The adapted system scores higher on these tasks. To test what the gain depends on, I turn the feature path off, then edit an individual count while keeping the prompt fixed.
Setting the retina’s scale to zero removes its contribution at test time. Accuracy across the seven held-out tasks drops from 60.0% to 17.4%, close to the untouched base model’s 19.1%.
A stronger test leaves the input untouched. I hold the token IDs fixed and add an integer δ to a single feature: the 'r' count of F_retina for the token berry. If the host is reading its retina, it should look at the unchanged word strawberry and report a count the text contradicts.
On strawberry, the frozen host reports the edited count when δ = −2, −1 and 0. For positive edits, it keeps predicting 3 as the edited count rises. The systematic suite uses 25 held-out words, a random letter each, and δ ∈ {+1, +2} on the word's last token. There, the edit is followed 88% of the time (44/50) on the frozen host and 94% (47/50) with LoRA.
Compound pseudowords glue random 4–7 character BPE chunks into 9–13 character strings of at most three tokens, the worst case for memorised spellings. Frozen V2 reaches 96.9% on perceptual tasks (count 100, double 100, length 95, nth 92.5), against 64.4% for LoRA.
Re-tokenization splits held-out words into a different valid tokenization of the same bytes. Evaluated on count, length, nth and reverse, V2 does not lose aggregate accuracy under the tested re-splits (70.0 → 73.1 on the frozen host, 73.1 → 75.0 with LoRA). I did not run the ALU on this split.
String rewriting. On the frozen host, V2 scores 23% on deletion, 7% on replacement, and 2% on reversal. The strong results on short answers do not extend to generating edited words.
Readout saturation. In the strawberry intervention, the readout stops tracking the feature above three. The experiment leaves open how well the adapter handles larger counts in other words.
Span latching. The scanner relies on quote tokens to find its target span. Unquoted prose would need learned boundary gates or a rolling word register.
Scale. The ALU is verified on three models in two families. V2 was trained only on Qwen3-0.6B, with a single seed. Larger hosts, more seeds, and RL co-training as in WSM are untested.
The tokenizer supplies the spellings. The circuit and scanner compute the relevant character features. With the retina, training teaches the adapters how to pass those features to a frozen model so it can use them in an answer.
All numbers come from the saved runs in results/*.json; the charts on this page are rendered from them directly. A few corrections relative to the first version of this post:
F_retina encodes the first six and last four letters, not the first and last five.s|tr|aw|ber|ry); traw is not in the vocabulary.Every BPE-V2 number is from a single seed (42).
Code and results: github.com/samarth777/bpe-xray.
src/char_tables.py compiles the weight bank from any Hugging Face tokenizer.src/xray_alu.py and run_alu_eval.py contain the ALU and its evaluation.src/word_retina.py and train_v2.py contain BPE-V2 and the causal suites.src/xray_retina.py and visual_cortex.py contain the layer-0 and V1 ablations.For attribution in academic contexts, please cite this work as
P, Samarth. "BPE X-Ray Vision." 2026. https://samarth777.github.io/bpe-xray/
BibTeX citation
@misc{samarth2026bpexray,
title = {BPE X-Ray Vision: Weight-Space Character Modules for Token-Blind LLMs},
author = {P, Samarth},
year = {2026},
month = {November},
url = {https://samarth777.github.io/bpe-xray/}
}