Make tokenizer per-thread to unblock concurrent Redact - #8
Merged
Conversation
PrivacyFilterTokenizer serialized all tokenization behind a global lock, bottlenecking concurrent Redact calls on a single shared PrivacyFilter even though ONNX Runtime's Run is thread-safe. TiktokenTokenizer keeps internal encode caches that aren't guaranteed safe for concurrent mutation, so give each thread its own instance (ThreadLocal) instead of locking, and dispose it with the filter. Adds a concurrency test that hammers a shared tokenizer from many threads against the oracle. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 617e892b-0037-40a1-b8ff-ce4c08b8e051
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes the global tokenizer lock that serialized all tokenization work, enabling a single shared PrivacyFilter instance to handle many concurrent Redact calls without bottlenecking on tokenization.
Changes:
- Replaced global locking in
PrivacyFilterTokenizerwith a per-threadTiktokenTokenizerviaThreadLocal<T>. - Ensured
PrivacyFilterdisposes its tokenizer duringDispose(). - Added a concurrency stress test to validate
Encodebehavior under parallel usage.
Show a summary per file
| File | Description |
|---|---|
| tests/PrivacyFilter.Net.Tests/TokenizerTests.cs | Adds a parallel stress test to validate thread-safe Encode behavior against the oracle. |
| src/PrivacyFilter.Net/PrivacyFilterTokenizer.cs | Switches to a per-thread tokenizer instance and adds IDisposable to clean up the ThreadLocal. |
| src/PrivacyFilter.Net/PrivacyFilter.cs | Disposes the tokenizer when the filter instance is disposed. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Low
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Review details
Comments suppressed due to low confidence (1)
tests/PrivacyFilter.Net.Tests/TokenizerTests.cs:33
PrivacyFilterTokenizernow implementsIDisposable, butMatchesTiktokenOraclestill constructs it without disposing. This can leave the internalThreadLocal<T>undisposed across the test run; switch this test tousing varas well for symmetry with the new concurrency test.
[Fact]
public void EncodeIsThreadSafeUnderConcurrency()
{
// The tokenizer is now per-thread (replacing a global lock), so a single
// shared instance must serve concurrent Encode calls without corruption.
// Hammer it from many threads against the oracle: mismatched ids/offsets or
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Low
Comment on lines
+54
to
+58
| Assert.Equal(testCase.Ids, actual.TokenIds); | ||
| Assert.Equal(testCase.Decoded, actual.DecodedText); | ||
| Assert.Equal(testCase.Starts, actual.CharacterStarts); | ||
| Assert.Equal(testCase.Ends, actual.CharacterEnds); | ||
| } |
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.
Summary
PrivacyFilterTokenizerserialized all tokenization behind a globallock, which bottlenecked concurrentRedactcalls on a single sharedPrivacyFilter— even though ONNX Runtime'sRunis already thread-safe. The tokenizer was the last serialization point preventing a shared filter from serving many concurrent redactions at full throughput.This gives each thread its own
TiktokenTokenizerviaThreadLocal<T>instead of locking (the tokenizer keeps internal encode caches that aren't guaranteed safe for concurrent mutation), and disposes it with the filter.Encodelogic is otherwise unchanged.Why not a batch API?
Concurrency beats batching for this model: a padded batch does
maxLenwork for every row, so throughput comes from driving many concurrent single-stringRuncalls on one shared instance — which this unblocks — not from a batch entrypoint. The single-string API is unchanged.Testing
EncodeIsThreadSafeUnderConcurrencytest hammers a shared tokenizer from all cores (256 iterations) against the tokenizer oracle; mismatched ids/offsets or an exception would reveal unsafe shared state.