Skip to content

Commit fa6c295

Browse files
stephentoubmichaelgsharp
authored andcommitted
Fix TensorPrimitives.CosineSimilarity to use vectorized implementations (dotnet#92204)
1 parent 6eb4ed5 commit fa6c295

File tree

1 file changed

+1
-12
lines changed

1 file changed

+1
-12
lines changed

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -271,18 +271,7 @@ public static float CosineSimilarity(ReadOnlySpan<float> x, ReadOnlySpan<float>
271271
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
272272
}
273273

274-
float dotprod = 0f;
275-
float magx = 0f;
276-
float magy = 0f;
277-
278-
for (int i = 0; i < x.Length; i++)
279-
{
280-
dotprod += x[i] * y[i];
281-
magx += x[i] * x[i];
282-
magy += y[i] * y[i];
283-
}
284-
285-
return dotprod / (MathF.Sqrt(magx) * MathF.Sqrt(magy));
274+
return CosineSimilarityCore(x, y);
286275
}
287276

288277
/// <summary>

0 commit comments

Comments
 (0)