Skip to content

Commit 211bc24

Browse files
authored
Vectorize TensorPrimitives.Sigmoid and TensorPrimitives.SoftMax (#93029)
* Vectorize TensorPrimitives.Sigmoid and TensorPrimitives.SoftMax - Adds a SigmoidOperator that just wraps the ExpOperator - Vectorizes both passes of SoftMax, on top of ExpOperator. Simplest way to do this was to augment the existing InvokeSpanScalarIntoSpan to take a transform operator. - In doing so, found some naming inconsistencies I'd previously introduced, so I did some automatic renaming to make things more consistent. - Added XML comments to all the internal/private surface area. - Fleshes out some tests (and test values). * Disable tests on mono * Address PR feedback
1 parent 1ef38a8 commit 211bc24

File tree

4 files changed

+537
-234
lines changed

4 files changed

+537
-234
lines changed

src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -988,17 +988,7 @@ public static void Sigmoid(ReadOnlySpan<float> x, Span<float> destination)
988988
ThrowHelper.ThrowArgument_SpansMustBeNonEmpty();
989989
}
990990

991-
if (x.Length > destination.Length)
992-
{
993-
ThrowHelper.ThrowArgument_DestinationTooShort();
994-
}
995-
996-
ValidateInputOutputSpanNonOverlapping(x, destination);
997-
998-
for (int i = 0; i < x.Length; i++)
999-
{
1000-
destination[i] = 1f / (1f + MathF.Exp(-x[i]));
1001-
}
991+
InvokeSpanIntoSpan<SigmoidOperator>(x, destination);
1002992
}
1003993

1004994
/// <summary>Computes the element-wise hyperbolic sine of each single-precision floating-point radian angle in the specified tensor.</summary>
@@ -1067,17 +1057,9 @@ public static void SoftMax(ReadOnlySpan<float> x, Span<float> destination)
10671057

10681058
ValidateInputOutputSpanNonOverlapping(x, destination);
10691059

1070-
float expSum = 0f;
1071-
1072-
for (int i = 0; i < x.Length; i++)
1073-
{
1074-
expSum += MathF.Exp(x[i]);
1075-
}
1060+
float expSum = Aggregate<ExpOperator, AddOperator>(x);
10761061

1077-
for (int i = 0; i < x.Length; i++)
1078-
{
1079-
destination[i] = MathF.Exp(x[i]) / expSum;
1080-
}
1062+
InvokeSpanScalarIntoSpan<ExpOperator, DivideOperator>(x, expSum, destination);
10811063
}
10821064

10831065
/// <summary>Computes the element-wise difference between single-precision floating-point numbers in the specified tensors.</summary>

0 commit comments

Comments
 (0)