Skip to content

Commit

Permalink
Disable TensorPrimitives vectorization of Log, Cbrt, Pow, and RootN (#…
Browse files Browse the repository at this point in the history
…101690)

We had a test bug that was hiding cases where one of the expected / actual was NaN and the other wasn't.
  • Loading branch information
stephentoub committed Apr 29, 2024
1 parent 33a68d0 commit aab8803
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
Expand Up @@ -26,7 +26,7 @@ public static void Cbrt<T>(ReadOnlySpan<T> x, Span<T> destination)
private readonly struct CbrtOperator<T> : IUnaryOperator<T, T>
where T : IRootFunctions<T>
{
public static bool Vectorizable => typeof(T) == typeof(float) || typeof(T) == typeof(double);
public static bool Vectorizable => false; // typeof(T) == typeof(float) || typeof(T) == typeof(double); // TODO: https://github.com/dotnet/runtime/issues/100535

public static T Invoke(T x) => T.Cbrt(x);

Expand Down
Expand Up @@ -161,7 +161,7 @@ public static Vector512<T> Invoke(Vector512<T> x)
private readonly struct LogBaseOperator<T> : IBinaryOperator<T>
where T : ILogarithmicFunctions<T>
{
public static bool Vectorizable => LogOperator<T>.Vectorizable;
public static bool Vectorizable => false; //LogOperator<T>.Vectorizable; // TODO: https://github.com/dotnet/runtime/issues/100535
public static T Invoke(T x, T y) => T.Log(x, y);
public static Vector128<T> Invoke(Vector128<T> x, Vector128<T> y) => LogOperator<T>.Invoke(x) / LogOperator<T>.Invoke(y);
public static Vector256<T> Invoke(Vector256<T> x, Vector256<T> y) => LogOperator<T>.Invoke(x) / LogOperator<T>.Invoke(y);
Expand Down
Expand Up @@ -59,7 +59,7 @@ public static void Pow<T>(T x, ReadOnlySpan<T> y, Span<T> destination)
private readonly struct PowOperator<T> : IBinaryOperator<T>
where T : IPowerFunctions<T>
{
public static bool Vectorizable => typeof(T) == typeof(float) || typeof(T) == typeof(double);
public static bool Vectorizable => false; // typeof(T) == typeof(float) || typeof(T) == typeof(double); // TODO: https://github.com/dotnet/runtime/issues/100535

public static T Invoke(T x, T y) => T.Pow(x, y);

Expand Down
Expand Up @@ -28,7 +28,7 @@ public static void RootN<T>(ReadOnlySpan<T> x, int n, Span<T> destination)
{
private readonly int _n = n;

public static bool Vectorizable => typeof(T) == typeof(float) || typeof(T) == typeof(double);
public static bool Vectorizable => false; // typeof(T) == typeof(float) || typeof(T) == typeof(double); // TODO: https://github.com/dotnet/runtime/issues/100535

public T Invoke(T x) => T.RootN(x, _n);

Expand Down
10 changes: 10 additions & 0 deletions src/libraries/System.Numerics.Tensors/tests/Helpers.cs
Expand Up @@ -28,13 +28,23 @@ private static class DefaultTolerance<T> where T : unmanaged, INumber<T>

public static bool IsEqualWithTolerance<T>(T expected, T actual, T? tolerance = null) where T : unmanaged, INumber<T>
{
if (T.IsNaN(expected) != T.IsNaN(actual))
{
return false;
}

tolerance = tolerance ?? DefaultTolerance<T>.Value;
T diff = T.Abs(expected - actual);
return !(diff > tolerance && diff > T.Max(T.Abs(expected), T.Abs(actual)) * tolerance);
}
#else
public static bool IsEqualWithTolerance(float expected, float actual, float? tolerance = null)
{
if (float.IsNaN(expected) != float.IsNaN(actual))
{
return false;
}

tolerance ??= DefaultFloatTolerance;
float diff = MathF.Abs(expected - actual);
return !(diff > tolerance && diff > MathF.Max(MathF.Abs(expected), MathF.Abs(actual)) * tolerance);
Expand Down

0 comments on commit aab8803

Please sign in to comment.