Skip to content

tokenizer: pre-size vocabulary for sparse added-token IDs - #13

Draft
pd95 wants to merge 3 commits into
fix/gemma4-tool-call-safetyfrom
fix/tokenizer-sparse-added-tokens
Draft

pd95 wants to merge 3 commits into
fix/gemma4-tool-call-safetyfrom
fix/tokenizer-sparse-added-tokens

Conversation

@pd95

@pd95 pd95 commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Motivation

Hugging Face tokenizer files may assign added tokens IDs far above the base
vocabulary and leave gaps between IDs. Ollama stores vocabulary entries in an
ID-indexed slice. Growing that slice one token at a time causes repeated
allocation and copying, while trusting an unchecked high ID can trigger an
excessive allocation. Duplicate IDs can also overwrite an existing token
silently.

The complete required extent is knowable before insertion and should be
validated before memory is allocated.

What this changes

  • scans base and added-token IDs to determine the final vocabulary extent;
  • allocates the ID-indexed vocabulary once instead of repeatedly growing it;
  • preserves intentional holes in sparse token ranges;
  • detects collisions rather than overwriting an existing token;
  • rejects negative, overflowing, or unreasonably large added-token IDs;
  • applies the same bounded handling while tokenizer data is loaded during
    model creation;
  • adds table-driven coverage for sparse ranges, collisions, limits, and normal
    dense vocabularies.

Dependency and scope

This generic tokenizer change depends on Gemma parser-safety PR #12. It does
not introduce a tokenizer format or model-family-specific behavior.

Compare:
fix/gemma4-tool-call-safety...fix/tokenizer-sparse-added-tokens

Validation

The complete x/tokenizer package passes with the expanded loader tests,
together with the integrated Linux test/lint/build and Darwin MLX build gates.

Refresh and AI assistance

Reconstructed on Ollama v0.34.0; head
e893cdbc39691edc1ccac63d7caa21e135fb7fb6. Codex materially assisted
implementation review, tests, refresh validation, and this PR description.

@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch from a51850f to af5993f Compare August 8, 2026 17:37
@pd95 pd95 changed the title tokenizer: pre-size vocab for sparse added tokens tokenizer: support sparse added-token IDs safely Aug 8, 2026
@pd95
pd95 changed the base branch from main to fix/gemma4-tool-call-safety August 8, 2026 17:41
@pd95 pd95 changed the title tokenizer: support sparse added-token IDs safely tokenizer: pre-size vocabulary for sparse added-token IDs Aug 8, 2026
@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch from af5993f to 0531b4b Compare August 12, 2026 15:26
@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch from 0531b4b to 52a09e0 Compare August 14, 2026 21:08
@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch 2 times, most recently from 48c2c56 to 2e44b8c Compare August 28, 2026 14:46
@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch from 2e44b8c to 5b3d114 Compare August 31, 2026 05:38
@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch from 5b3d114 to b29136f Compare September 5, 2026 09:33
@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch from b29136f to e893cdb Compare September 14, 2026 06:09
@pd95
pd95 removed this pull request from stack #26 September 14, 2026 18:24
@pd95
pd95 added this pull request to stack #27 September 14, 2026 18:24
pd95 and others added 3 commits September 18, 2026 06:31
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
Co-authored-by: Codex <noreply@openai.com>
@pd95
pd95 force-pushed the fix/tokenizer-sparse-added-tokens branch from e893cdb to fe0227a Compare September 18, 2026 12:17
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