fix(bpe): drop heap entries with non-positive pair_counts before u64 cast - #2389
Open
okxint wants to merge 1 commit into
Open
fix(bpe): drop heap entries with non-positive pair_counts before u64 cast#2389okxint wants to merge 1 commit into
okxint wants to merge 1 commit into
Conversation
…cast
When `continuing_subword_prefix` (or `end_of_word_suffix`) and
`max_token_length` are both active, a neighbour-delta update in
`do_train` can apply the -1 side of a delta while the length guard
rejects the matching +1, driving `pair_counts[pair]` negative.
The staleness re-check reads that i32 value through `as u64` without a
sign guard:
if top.count != pair_counts[&top.pair] as u64 { // -1 → u64::MAX
top.count = pair_counts[&top.pair] as u64; // u64::MAX stored
queue.push(top);
continue;
}
On the next pop the staleness test passes (both sides are u64::MAX),
`top.count < 1` is false, and the trainer emits a merge for a pair that
occurs zero times in the corpus — ahead of every legitimate candidate,
because this is a max-heap. The phantom merge silently occupies a
vocabulary slot and shifts every subsequent merge rank.
The two `queue.push` call sites already guard against non-positive
counts; the staleness branch is the only path missing the guard.
Fix: read the live count as `i32` first, discard the entry if it is ≤ 0,
and only then cast to `u64` for the comparison and the re-push.
Add `bpe_no_phantom_merge_with_prefix_and_max_token_length` reproducing
the 8-word corpus from issue huggingface#2320. The test verifies that every entry
in the trained merge table refers to vocab ids that actually exist,
which would fail if a phantom merge were emitted (the merged token would
be unreachable from the corpus).
Fixes huggingface#2320
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2320.
Problem
When
continuing_subword_prefix(orend_of_word_suffix) andmax_token_lengthare both set,BpeTrainercan emit a merge for a pair that occurs zero times in the corpus — a phantom merge.Mechanism:
pair_countsisAHashMap<Pair, i32>(signed). A neighbour-delta update subtracts 1 from the pair that is being displaced. Whenmax_token_lengthrejects the matching+1side (because the would-be merged token would exceed the limit), the pair count can go negative.The staleness re-check in the training loop reads this value through
as u64with no sign guard:On the next pop, the staleness check passes (both sides are
u64::MAX),top.count < 1isfalse, and the trainer merges a pair that nothing in the corpus could have produced — ahead of every legitimate pair, because this is a max-heap. The phantom merge silently consumes a vocabulary slot and shifts every subsequent merge rank, changing the trained tokenizer.The two
queue.pushcall sites correctly guard against non-positive counts:The staleness branch is the only path that skips the guard.
This affects
WordPieceTrainer(which setscontinuing_subword_prefix("##")by default) whenevermax_token_lengthis also specified. Reproducible with 8 words / 38 bytes ontokenizers==0.23.1.Fix
Read the live count as
i32first, drop the entry immediately if it is ≤ 0, then cast tou64for the comparison and re-push:Widening
pair_countstoi64(as proposed in the related #2058) does not fix this on its own — any negative value sign-extends throughas u64identically.Test
Added
bpe_no_phantom_merge_with_prefix_and_max_token_lengthusing the 8-word corpus from the issue. It trains withcontinuing_subword_prefix = "##"andmax_token_length = 4, then verifies that every entry in the trained merge table refers to vocabulary IDs that actually exist — a condition that would fail if a phantom merge were emitted, since the merged token would be unreachable from the corpus.