Vectorize argmax with TensorPrimitives - #10
Merged
Conversation
Use TensorPrimitives.IndexOfMax for the contiguous per-token class scan, with scalar parity coverage and benchmark results. 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 switches the per-token argmax decoding path to use System.Numerics.Tensors.TensorPrimitives.IndexOfMax for potentially faster vectorized max-index selection, while adding parity coverage and preserving the scalar implementation for benchmarking comparison.
Changes:
- Replace the scalar argmax loop with
TensorPrimitives.IndexOfMaxinPrivacyFilter.ArgMax. - Add
System.Numerics.Tensorspackage reference (centrally versioned) to align with the targeted .NET servicing version. - Add a finite-scores parity test and split benchmarks to measure scalar vs tensor-based argmax.
Show a summary per file
| File | Description |
|---|---|
| tests/PrivacyFilter.Net.Tests/DecoderTests.cs | Adds parity coverage comparing tensor-based argmax to a scalar reference for finite scores (including all-zero tie behavior). |
| src/PrivacyFilter.Net/PrivacyFilter.Net.csproj | Adds a direct System.Numerics.Tensors dependency and exposes internals to tests/benchmarks (already present). |
| src/PrivacyFilter.Net/PrivacyFilter.cs | Replaces scalar per-token argmax with TensorPrimitives.IndexOfMax. |
| Directory.Packages.props | Centrally versions System.Numerics.Tensors to match the repo’s dependency management pattern. |
| bench/PrivacyFilter.Net.Benchmarks/Program.cs | Keeps scalar argmax as a benchmark and adds a separate tensor-based argmax benchmark for comparison. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 5/5 changed files
- Comments generated: 1
- Review effort level: Low
Comment on lines
246
to
253
| internal static int[] ArgMax(ReadOnlySpan<float> scores, int tokenCount, int classCount) | ||
| { | ||
| var labels = new int[tokenCount]; | ||
| for (int token = 0; token < tokenCount; token++) | ||
| { | ||
| int offset = token * classCount; | ||
| int bestLabel = 0; | ||
| float bestScore = scores[offset]; | ||
| for (int label = 1; label < classCount; label++) | ||
| { | ||
| float score = scores[offset + label]; | ||
| if (score > bestScore) | ||
| { | ||
| bestScore = score; | ||
| bestLabel = label; | ||
| } | ||
| } | ||
|
|
||
| labels[token] = bestLabel; | ||
| labels[token] = TensorPrimitives.IndexOfMax( | ||
| scores.Slice(token * classCount, classCount)); | ||
| } |
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
TensorPrimitives.IndexOfMaxfor independent per-token argmax decodingSystem.Numerics.Tensorsdirectly at the matching .NET servicing versionTesting
dotnet test PrivacyFilter.Net.sln -c Release --nologodotnet pack src/PrivacyFilter.Net/PrivacyFilter.Net.csproj -c Release --no-build