Optimize managed postprocessing and allocations - #6
Merged
Conversation
Decode raw model logits, use sparse Viterbi transitions, and pool temporary inference and backpointer buffers. Add parity coverage and before/after benchmarks. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: a9621082-f9c8-4ec8-ac3c-faaf34eb7d0f
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes managed postprocessing in PrivacyFilter.Net by reducing allocations and improving decoding throughput (notably Viterbi), while adding tests and benchmarks to validate parity and measure performance.
Changes:
- Switch decoding to operate directly on raw logits (removing the production log-softmax pass) and pool model score buffers.
- Replace dense Viterbi transition scanning with sparse valid-predecessor tables and pooled compact backpointers.
- Add randomized decoder parity tests and expand BenchmarkDotNet harness/bench documentation for managed postprocessing.
Show a summary per file
| File | Description |
|---|---|
| tests/PrivacyFilter.Net.Tests/DecoderTests.cs | Adds randomized parity tests (log-softmax invariance) and a dense Viterbi reference to validate the sparse decoder. |
| src/PrivacyFilter.Net/ViterbiDecoder.cs | Implements sparse predecessor tables and pooled byte backpointers; updates Decode to accept ReadOnlySpan<float>. |
| src/PrivacyFilter.Net/PrivacyFilter.cs | Pools score buffers, avoids copying dense ONNX outputs when possible, and changes inference to fill a caller-provided Span<float>. |
| src/PrivacyFilter.Net/PrivacyFilter.Net.csproj | Exposes internals to the benchmarks project for performance testing. |
| bench/PrivacyFilter.Net.Benchmarks/Program.cs | Uses BenchmarkSwitcher and adds managed postprocessing benchmarks (log-softmax, dense vs sparse Viterbi, argmax). |
| bench/results.md | Documents new managed postprocessing benchmark results and allocation/time deltas. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (1)
src/PrivacyFilter.Net/ViterbiDecoder.cs:96
tokenCount * classCountis computed without overflow/negative validation. IftokenCountis negative or large enough to overflow, the dimension check can behave incorrectly and later allocations (e.g.,new int[tokenCount]) will throw in less clear ways. Consider validatingtokenCount >= 0and usingcheckedfor the expected emission length.
int classCount = _labels.TokenClassNames.Length;
if (emissions.Length != tokenCount * classCount)
{
throw new ArgumentException("Emission dimensions do not match the label space.", nameof(emissions));
}
- Files reviewed: 6/6 changed files
- Comments generated: 1
- Review effort level: Low
Comment on lines
+138
to
+144
| private void RunModel(int[] tokenIds, Span<float> scores) | ||
| { | ||
| int classCount = _labels.TokenClassNames.Length; | ||
| var allLogProbabilities = new float[tokenIds.Length * classCount]; | ||
| if (scores.Length != tokenIds.Length * classCount) | ||
| { | ||
| throw new ArgumentException("Score dimensions do not match the token input.", nameof(scores)); | ||
| } |
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
Results
Testing
dotnet test PrivacyFilter.Net.sln -c Release --nologo