Repository navigation
perf: Make Word.MergeAll compaction linear - #7934
nikitabhagat2307-prog wants to merge 2 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7934 +/- ##
==========================================
+ Coverage 70.85% 70.88% +0.03%
==========================================
Files 1420 1421 +1
Lines 279991 280145 +154
Branches 27594 27603 +9
==========================================
+ Hits 198374 198582 +208
+ Misses 73467 73421 -46
+ Partials 8150 8142 -8
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟢 Approval recommended
There are no active review issues for the tokenizer cleanup optimization.
No open issues 🎉
What changed in this PR
This PR addresses #7872 by removing quadratic cleanup costs from tokenizer Word.MergeAll. It preserves surviving symbol order and leaves merge selection unchanged; only the cleanup step becomes linear.
Changes:
- Add
Vec<T>.RemoveAllfor stable, in-place compaction with unused-tail clearing. - Replace repeated symbol removals in
Word.MergeAllwith one compaction pass.
| File | Description |
|---|---|
| src/Microsoft.ML.Tokenizers/Utils/Vec.cs | Adds predicate-based linear compaction and clears removed elements from the buffer tail. |
| src/Microsoft.ML.Tokenizers/Model/Word.cs | Uses RemoveAll to discard zero-length symbols without repeated shifting. |
Review effort: Balanced
| if (match is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(match)); | ||
| } | ||
|
|
There was a problem hiding this comment.
Coverlet complains about the the match is null check. It seems like we can't ever reach this line. match is non-nullable and the only caller passes a static lambda, so it can go like the _buffer check did or replace it with Debug.Assert
| if (match is null) | |
| { | |
| throw new ArgumentNullException(nameof(match)); | |
| } |
Summary
Word.MergeAllwith one stable linear compaction pass.Vec<T>.RemoveAllto compact in place and clear the unused tail of its buffer.This removes the quadratic shifting cost from cleanup while preserving the surviving symbol order and leaving merge selection unchanged.
Fixes #7872
Validation
dotnet testcompiled the Tokenizers library and test assembly fornet8.0with the repository analyzer disabled.git diff --checkpassed.