Skip to content

npu tokenizer: a Gemma-family tokenizer.json encodes to the wrong ids (31 for the reference's 21) and replies keep the raw word-boundary symbols #2215

Closed

Description

@bong-water-water-bong

The NPU route's own tokenizer reader (npu/tokenizer.cpp) mis-reads the Gemma family's tokenizer.json. Prompts are encoded to different ids than the reference library gives, and replies come back with ▁ where spaces belong.

Found on FastFlowLM/Nemotron-3.5-Bolt-E4B-NPU2 while taking #1780's target to the device (see #2213 for the other defect on that pack).

Measured (device-free)

Same tokenizer.json, same text, engine reader against Hugging Face tokenizers 0.23.3:

text: "<|turn>user\nWrite a Python function that returns the n-th Fibonacci number.<turn|>\n<|turn>model\n"
engine:    31 ids: 105 2364 247723 6974 245237 236746 245237 32651 245237 2501 245237 7705 ...
reference: 21 ids: 105 2364 107 6974 496 17856 1292 600 7623 506 538 236772 594 123466 1548 236761 106 107 105 4368 107

Across the 19 distinct tokenizer.json files on this box, 8 texts each: 16 files match the reference 8 of 8. Three match 1 of 8, all of this shape: bolt-e4b, gemma3-270m-npu2, gemma22b-awq.

On the device the reply to that prompt read Here▁is▁a▁Python▁function▁that▁returns▁the▁....

Cause

The file is BPE with merges, a normalizer that only rewrites spaces ({Replace " " -> "▁"}, no Prepend), a Split pre-tokenizer on the space, and a decoder that turns ▁ back into a space. The reader recognises SentencePiece pieces when the merge list is empty or when the normalizer prepends the symbol; this shape is neither, so it goes down the byte-level path, which splits on spaces and byte-maps them, and its decode leaves the symbol alone.

After that normalizer no space is left for the pre-tokenizer to split on, so a run of text is one word merged by rank, with no prefix.

Fix

Recognise the shape, encode by rank without the prefix, decode the symbol to a space without stripping a leading one. PR follows, with a synthetic nine-token fixture whose expected ids come from the reference library.

Activity

  1. bong-water-water-bong commented on Nov 11, 2026

    @bong-water-water-bong
    CollaboratorAuthor

    Taking this row. The measurement reproduces, and the reason it was invisible is that the goldens are wired to a different reader.

    Measured on main today, device-free, with the engine's own reader. npu/tokenizer.cpp has not changed between the build I used and current main (git log HEAD..origin/main -- npu/tokenizer.cpp → 0 commits), so the reader under test is the one that ships.

    I paired two committed goldens with the exact tokenizer.json files they name by sha256, both present on this box:

    golden names tokenizer.json here sha256
    Gemma4-E2B-IT-NPU2.json cc8d3a0ce3 ~/scratch-packs/gemma4-e2b-npu2/tokenizer.json cc8d3a0ce364 ✓
    Gemma3-1B-NPU2.json 4667f2089529 ~/.config/flm/models/Medgemma-4B-NPU2/tokenizer.json 4667f2089529 ✓
    $ tokenizer_test <tokenizer.json> tests/data/tokenizers/Gemma4-E2B-IT-NPU2.json
    FAIL encode " "
      want [236743]
      got  [245237]
    2/17 cases match tokenizers 0.23.2, Gemma4-E2B-IT-NPU2 tokenizer.json sha256 cc8d3a0ce364
    
    $ tokenizer_test <tokenizer.json> tests/data/tokenizers/Gemma3-1B-NPU2.json
    FAIL encode " "
      want [236743]
      got  [245237]
    2/17 cases match tokenizers 0.23.2, Gemma3-1B-NPU2 tokenizer.json sha256 4667f2089529
    

    The first failing case is the symptom in this issue: a single space should be the ▁ piece (236743) and the reader gives 245237. That is the here▁is▁a▁Python output, at the smallest possible input.

    Why nothing caught it

    The 18 goldens in tests/data/tokenizers/ are registered against hf_tokenizer_test — the Rust tokenizer behind the C ABI — and only under two switches that are both off by default:

    option(ONEBIT_HF_TOKENIZERS "…" OFF)                      # needs cargo
    set(ONEBIT_TOKENIZER_ROOT "" CACHE PATH "…")              # must point at a models dir
      foreach(_g ${_tok_golden})  add_test(NAME hf_tokenizer_${_m} COMMAND hf_tokenizer_test …)

    The engine's own reader (tokenizer_test, linked against onebit_npu) is registered against one fixture — tests/data/spm_bpe_tokenizer.json — plus an optional ${ONEBIT_NPU_MODEL} case. So the goldens validate the Rust path, and the reader that actually serves the NPU route is not measured against them at all. Two committed gemma goldens fail the reader by 15 of 17 cases and main is green.

    Slice I am taking

    1. register tokenizer_test against tests/data/tokenizers/*.json the way hf_tokenizer_test already is, so this cannot be invisible again — gated on the golden's own named sha256 so a mismatched pack is a skip with a printed reason, not a fail on the wrong file;
    2. the reader fix for the space/▁ handling the issue describes, verified by those goldens going 17/17.

    ORCH: (2) needs a build and this box is at 169 G against the 175 G build threshold — incremental only, in an existing tree, if that is acceptable; otherwise it waits for disk. (1) I can prepare on a branch now, but it must land with (2): registering a failing golden would red main, which is the opposite of what a ratchet is for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions