Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="2.0.0" />
<PackageVersion Include="Microsoft.ML.Tokenizers.Data.O200kBase" Version="2.0.0" />
<PackageVersion Include="Microsoft.Bcl.Memory" Version="10.0.10" />
<PackageVersion Include="System.Numerics.Tensors" Version="10.0.10" />
</ItemGroup>

<ItemGroup Label="Tests">
Expand Down
34 changes: 33 additions & 1 deletion bench/PrivacyFilter.Net.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,39 @@ public int[] ViterbiDense() =>
public int[] ViterbiSparse() => _decoder.Decode(_logProbabilities, TokenCount);

[Benchmark]
public int[] ArgMax() => PrivacyFilter.ArgMax(_logProbabilities, TokenCount, _classCount);
public int[] ArgMaxScalar() =>
ArgMaxScalar(_logProbabilities, TokenCount, _classCount);

[Benchmark]
public int[] ArgMaxTensorPrimitives() =>
PrivacyFilter.ArgMax(_logProbabilities, TokenCount, _classCount);

private static int[] ArgMaxScalar(
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;
}

return labels;
}

private static int[] DecodeDense(
float[] emissions,
Expand Down
1 change: 1 addition & 0 deletions src/PrivacyFilter.Net/PrivacyFilter.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<PackageReference Include="Microsoft.ML.Tokenizers" />
<PackageReference Include="Microsoft.ML.Tokenizers.Data.O200kBase" />
<PackageReference Include="Microsoft.Bcl.Memory" />
<PackageReference Include="System.Numerics.Tensors" />
</ItemGroup>

</Project>
17 changes: 3 additions & 14 deletions src/PrivacyFilter.Net/PrivacyFilter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Buffers;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using TensorPrimitives = System.Numerics.Tensors.TensorPrimitives;

namespace PrivacyFilterNet;

Expand Down Expand Up @@ -247,20 +248,8 @@ internal static int[] ArgMax(ReadOnlySpan<float> scores, int tokenCount, int cla
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));
}

return labels;
Expand Down
49 changes: 49 additions & 0 deletions tests/PrivacyFilter.Net.Tests/DecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ public void SparseViterbiMatchesDenseReference()
decoder.Decode(tiedEmissions, 8));
}

[Fact]
public void TensorArgMaxMatchesScalarForFiniteScores()
{
const int tokenCount = 128;
int classCount = ClassNames.Length;
var scores = new float[tokenCount * classCount];
var random = new Random(42);
for (int index = 0; index < scores.Length; index++)
{
scores[index] = (random.NextSingle() * 20) - 10;
}

Assert.Equal(
ArgMaxScalar(scores, tokenCount, classCount),
PrivacyFilter.ArgMax(scores, tokenCount, classCount));

Array.Clear(scores);
Assert.Equal(
ArgMaxScalar(scores, tokenCount, classCount),
PrivacyFilter.ArgMax(scores, tokenCount, classCount));
}

[Fact]
public void SpanDecoderTrimsAndRedacts()
{
Expand Down Expand Up @@ -113,6 +135,33 @@ public void SpanDecoderTrimsAndRedacts()

private static int ClassIndex(string name) => Array.IndexOf(ClassNames, name);

private static int[] ArgMaxScalar(
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;
}

return labels;
}

private static int[] DecodeDenseReference(float[] emissions, int tokenCount, int classCount)
{
const float invalid = -1e9f;
Expand Down