Skip to content

unicode: include '~' in collapsed symbol class - #26972

Merged
CISC merged 1 commit into
ggml-org:masterfrom
tarruda:dsv4-fix-tokenizer
Aug 18, 2026
Merged

CISC merged 1 commit into
ggml-org:masterfrom
tarruda:dsv4-fix-tokenizer

Conversation

@tarruda

@tarruda tarruda commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Overview

The collapsed \p{S} class was missing ~, which split ~ into separate pre-tokens and prevented the Ġ~ BPE merge used by DeepSeek V4. This caused re-tokenized prompts to diverge from sampled tokens and broke KV cache reuse.

Additional information

While using DSv4 Flash locally, I noticed that long thinking blocks followed by a tool call was being reprocessed when a client returned the tool response. More information here

I've used Deepseek V4 Flash to debug and find the issue, and later verified that upstream tokenizer behavior matches this fix (~ being a separate token).

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: Yes this was debugged and fixed by deepseek with my assistance by live running a client.

The collapsed \p{S} class was missing '~', which split " ~" into
separate pre-tokens and prevented the Ġ~ BPE merge used by DeepSeek V4.
This caused re-tokenized prompts to diverge from sampled tokens and
broke KV cache reuse.

Assisted-by: Codex
@tarruda
tarruda requested a review from ggerganov as a code owner August 12, 2026 13:57
Comment thread tests/CMakeLists.txt
set_property(TEST ${TEST_TARGET} PROPERTY LABELS ${LLAMA_TEST_LABEL})
endfunction()

llama_build_and_test(test-unicode.cpp)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new test module for this as the existing test-unicode.cpp is focused on peg parser unicode handling.

Comment thread src/unicode.cpp
{ unicode_cpt_flags::PUNCTUATION, "\x21-\x23\x25-\x2A\x2C-\x2F\x3A-\x3B\x3F-\x40\\\x5B-\\\x5D\x5F\\\x7B\\\x7D" }, // !-#%-*,-/:-;?-@\[-\]_\{\}
{ unicode_cpt_flags::ACCENT_MARK, "" }, // no sub-128 codepoints
{ unicode_cpt_flags::SYMBOL, "\\\x24\\\x2B\x3C-\x3E\x5E\x60\\\x7C" }, // $+<=>^`|
{ unicode_cpt_flags::SYMBOL, "\\\x24\\\x2B\x3C-\x3E\x5E\x60\\\x7C\\\x7E" }, // $+<=>^`|~

@CISC CISC Aug 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also include at least £ and €?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I have no idea. Should I include it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the intention is here with the limited set, best leave it, just wondered. :)

@github-actions github-actions Bot added the testing Everything test related label Aug 12, 2026
@aldehir

aldehir commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Another individual added a dedicated pretokenizer, might be worth adopting? #26966

@CISC
CISC merged commit afd439d into ggml-org:master Aug 18, 2026
21 of 27 checks passed
@tarruda
tarruda deleted the dsv4-fix-tokenizer branch August 18, 2026 13:16
thecodacus pushed a commit to thecodacus/llama.cpp that referenced this pull request Sep 7, 2026
The collapsed \p{S} class was missing '~', which split " ~" into
separate pre-tokens and prevented the Ġ~ BPE merge used by DeepSeek V4.
This caused re-tokenized prompts to diverge from sampled tokens and
broke KV cache reuse.

Assisted-by: Codex
zbrad pushed a commit to zbrad/llama.cpp that referenced this pull request Sep 10, 2026
The collapsed \p{S} class was missing '~', which split " ~" into
separate pre-tokens and prevented the Ġ~ BPE merge used by DeepSeek V4.
This caused re-tokenized prompts to diverge from sampled tokens and
broke KV cache reuse.

Assisted-by: Codex
pl752 pushed a commit to pl752/llama.cpp that referenced this pull request Sep 15, 2026
The collapsed \p{S} class was missing '~', which split " ~" into
separate pre-tokens and prevented the Ġ~ BPE merge used by DeepSeek V4.
This caused re-tokenized prompts to diverge from sampled tokens and
broke KV cache reuse.

Assisted-by: Codex
midagedev added a commit to midagedev/bloomery that referenced this pull request Sep 24, 2026
…ed-category split, as mainline llama.cpp's k_ucat_map (ggml-org/llama.cpp#26972) and HF tokenizers have it — `~/` is one id (71520) instead of two; the oracle's V4.1 reference moves to the ik build that carries the same list and refuses a reference whose `~/` is not [71520]; five `~` cases; the gate's failure line prints both id windows and their decoded text at the first difference

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: midagedev <midagedev@gmail.com>
ikawrakow pushed a commit to ikawrakow/ik_llama.cpp that referenced this pull request Sep 24, 2026
Port of ggml-org/llama.cpp#26972.

Signed-off-by: midagedev <midagedev@gmail.com>
frostyautumnleaf pushed a commit to frostyautumnleaf/llama.cpp that referenced this pull request Oct 5, 2026
The collapsed \p{S} class was missing '~', which split " ~" into
separate pre-tokens and prevented the Ġ~ BPE merge used by DeepSeek V4.
This caused re-tokenized prompts to diverge from sampled tokens and
broke KV cache reuse.

Assisted-by: Codex
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants