Skip to content

Tokenizer: request gaps with no normalized added spelling stay on the memo - #2844

Merged
ashhart merged 5 commits into
mainfrom
lead/tokenizer-memo-gaps
Nov 21, 2026
Merged

ashhart merged 5 commits into
mainfrom
lead/tokenizer-memo-gaps

Conversation

@ashhart

@ashhart ashhart commented Nov 11, 2026

Copy link
Copy Markdown
Owner

For a tokenizer with normalized added tokens (DeepSeek-style ones have them; our Qwen, Nemotron and LFM tokenizers don't), every gap between a request's message spans took the masked path (encodeTextMasked), which re-encodes the gap cold, even when the gap's normalized text holds no added-token spelling at all.

What changes (tokenizer.zig): before choosing the masked path, the gap is normalized the same way encodeTextMasked normalizes it, and when it holds no t.normalized match it takes the same memoised path a non-request gap takes. Gaps that do carry a spelling keep the mask, unchanged. MaskWork.masked counts the masked gaps.

Exactness: with no normalized match, the masked path's split returns the whole normalized gap as one segment and encodes it exactly as the fast path does, so the ids are the same; the decision is made on the normalized gap because a normalizer can create a spelling (e plus U+0301 under NFC).

Checks: three new tests in masked_test.zig: a gap with no spelling takes the memo (second call hits; fails on main: expected 0 masked, found 1), a gap carrying a spelling still takes the mask, and a golden replay of 96 random and adversarial texts over four fixtures (ASCII with normalized and plain added tokens, Unicode NFC and NFD) whose ids were recorded on main fb8fdf3, cold and with a warm memo. All 57 tokenizer tests pass; zig fmt --check clean; no new lean findings. On a 32K-word request the host benchmark goes from 268-271 µs to 152-171 µs a call.

Still to run: an end-to-end /tokenize comparison on a DeepSeek-style tokenizer, ids equal.

  • Synthena (Ash's Agent)

@ashhart
ashhart force-pushed the lead/tokenizer-memo-gaps branch from 67ffeab to 2ff7317 Compare November 14, 2026 03:24
@ashhart
ashhart force-pushed the lead/tokenizer-memo-gaps branch from 2ff7317 to 6d0d4b7 Compare November 18, 2026 02:40
@ashhart
ashhart force-pushed the lead/tokenizer-memo-gaps branch from 6d0d4b7 to 3cf692a Compare November 19, 2026 01:17
ashhart added a commit that referenced this pull request Nov 21, 2026
@ashhart
ashhart force-pushed the lead/tokenizer-memo-gaps branch from 3cf692a to d3076fd Compare November 21, 2026 13:06
A request gap whose normalized text has no t.normalized match encodes
through encodeSegment exactly as the clipped-span mask would, so test the
normalized gap and skip the mask; MaskWork.masked counts the slow path.
Golden ids from 96 base-commit cases replay identically, warm memo included.
…malized text goes into the encode step

The gap check normalized a request gap, then encodeTextMasked (masked branch) or encodeText (memo miss or short gap) normalized it again. The check's text and first-piece offset now pass into encodeTextMasked, the same computation done once, so the ids are unchanged. encodeText had no callers left and is gone.
@ashhart
ashhart force-pushed the lead/tokenizer-memo-gaps branch from d3076fd to 2b2a918 Compare November 21, 2026 17:37
@ashhart
ashhart merged commit f946bc1 into main Nov 21, 2026
2 of 12 checks passed
@ashhart
ashhart deleted the lead/tokenizer-memo-gaps branch November 21, 2026 17:37
ashhart added a commit that referenced this pull request Nov 21, 2026
ashhart added a commit that referenced this pull request Nov 21, 2026
ashhart added a commit that referenced this pull request Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant