From e0df6e22660aeaacdfa7f39b2588b6c72a23b63f Mon Sep 17 00:00:00 2001 From: Barion Date: Fri, 6 Mar 2026 13:48:36 +0100 Subject: [PATCH] SoftMax: Multiply by 1 / expSum instead of dividing by expSum --- .../src/System/Numerics/Tensors/TensorPrimitives.Single.cs | 2 +- .../System/Numerics/Tensors/netcore/TensorPrimitives.SoftMax.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Single.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Single.cs index 9e3a2944c49820..2a3842ebd05f11 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Single.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/TensorPrimitives.Single.cs @@ -858,7 +858,7 @@ public static void SoftMax(ReadOnlySpan x, Span destination) InvokeSpanIntoSpan(x, destination); float expSum = Sum(destination); - InvokeSpanScalarIntoSpan(destination, expSum, destination); + InvokeSpanScalarIntoSpan(destination, 1f / expSum, destination); } /// Computes the element-wise difference between single-precision floating-point numbers in the specified tensors. diff --git a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.SoftMax.cs b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.SoftMax.cs index af0e7be409654e..e25d39d30e86ed 100644 --- a/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.SoftMax.cs +++ b/src/libraries/System.Numerics.Tensors/src/System/Numerics/Tensors/netcore/TensorPrimitives.SoftMax.cs @@ -38,7 +38,7 @@ public static void SoftMax(ReadOnlySpan x, Span destination) InvokeSpanIntoSpan>(x, destination); T expSum = Sum(destination); - InvokeSpanScalarIntoSpan>(destination, expSum, destination); + InvokeSpanScalarIntoSpan>(destination, T.One / expSum, destination); } } }