Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0] Numerics and Tensors backport #92245

Merged
merged 9 commits into from
Sep 19, 2023
Prev Previous commit
Next Next commit
Fix TensorPrimitives.CosineSimilarity to use vectorized implementatio…
…ns (#92204)
  • Loading branch information
stephentoub authored and michaelgsharp committed Sep 18, 2023
commit fa6c2951a52c2a74bb956aa24340e97fde6c19fb
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