Skip to content

Commit

Permalink
Fix TensorPrimitives.CosineSimilarity to use vectorized implementatio…
Browse files Browse the repository at this point in the history
…ns (dotnet#92204)
  • Loading branch information
stephentoub authored and michaelgsharp committed Sep 18, 2023
1 parent 6eb4ed5 commit fa6c295
Showing 1 changed file with 1 addition and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,18 +271,7 @@ public static float CosineSimilarity(ReadOnlySpan<float> x, ReadOnlySpan<float>
ThrowHelper.ThrowArgument_SpansMustHaveSameLength();
}

float dotprod = 0f;
float magx = 0f;
float magy = 0f;

for (int i = 0; i < x.Length; i++)
{
dotprod += x[i] * y[i];
magx += x[i] * x[i];
magy += y[i] * y[i];
}

return dotprod / (MathF.Sqrt(magx) * MathF.Sqrt(magy));
return CosineSimilarityCore(x, y);
}

/// <summary>
Expand Down

0 comments on commit fa6c295

Please sign in to comment.