Skip to content

Commit 2023072

Browse files
eiriktsarpalispull[bot]
authored andcommitted
Reduce tolerances for floating point types in TP tests. (#99023)
* Increase tolerances for floating point types in TP tests. * Use tolerances as defined in the scalar test suites. * Use the same Half baseline as with scalar tests. * Increase tolerance for tests failing in CI hardware.
1 parent 487e8fb commit 2023072

File tree

4 files changed

+182
-84
lines changed

4 files changed

+182
-84
lines changed

src/libraries/System.Numerics.Tensors/tests/Helpers.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,60 @@ internal static class Helpers
1111
public static IEnumerable<int> TensorLengthsIncluding0 => Enumerable.Range(0, 257);
1212

1313
public static IEnumerable<int> TensorLengths => Enumerable.Range(1, 256);
14+
15+
// Tolerances taken from testing in the scalar math routines:
16+
// cf. https://github.com/dotnet/runtime/blob/89f7ad3b276fb0b48f20cb4e8408bdce85c2b415/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs
17+
// and https://github.com/dotnet/runtime/blob/fd48b6f5d1ff81a81d09e9d72982cc9e8d139852/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/HalfTests.cs
18+
public const double DefaultDoubleTolerance = 8.8817841970012523e-16;
19+
public const float DefaultFloatTolerance = 4.76837158e-07f;
20+
public const float DefaultHalfTolerance = 3.90625e-03f;
21+
public const double DefaultToleranceForEstimates = 1.171875e-02;
22+
23+
#if NETCOREAPP
24+
private static class DefaultTolerance<T> where T : unmanaged, INumber<T>
25+
{
26+
public static readonly T Value = DetermineTolerance<T>(DefaultDoubleTolerance, DefaultFloatTolerance, Half.CreateTruncating(DefaultHalfTolerance)) ?? T.CreateTruncating(0);
27+
}
28+
29+
public static bool IsEqualWithTolerance<T>(T expected, T actual, T? tolerance = null) where T : unmanaged, INumber<T>
30+
{
31+
tolerance = tolerance ?? DefaultTolerance<T>.Value;
32+
T diff = T.Abs(expected - actual);
33+
return !(diff > tolerance && diff > T.Max(T.Abs(expected), T.Abs(actual)) * tolerance);
34+
}
35+
#else
36+
public static bool IsEqualWithTolerance(float expected, float actual, float? tolerance = null)
37+
{
38+
tolerance ??= DefaultFloatTolerance;
39+
float diff = MathF.Abs(expected - actual);
40+
return !(diff > tolerance && diff > MathF.Max(MathF.Abs(expected), MathF.Abs(actual)) * tolerance);
41+
}
42+
#endif
43+
44+
public static T? DetermineTolerance<T>(
45+
double? doubleTolerance = null,
46+
float? floatTolerance = null
47+
#if NETCOREAPP
48+
, Half? halfTolerance = null
49+
#endif
50+
) where T : struct
51+
{
52+
if (typeof(T) == typeof(double) && doubleTolerance != null)
53+
{
54+
return (T?)(object)doubleTolerance;
55+
}
56+
else if (typeof(T) == typeof(float) && floatTolerance != null)
57+
{
58+
return (T?)(object)floatTolerance;
59+
}
60+
#if NETCOREAPP
61+
else if (typeof(T) == typeof(Half) && halfTolerance != null)
62+
{
63+
return (T?)(object)halfTolerance;
64+
}
65+
#endif
66+
67+
return null;
68+
}
1469
}
1570
}

0 commit comments

Comments
 (0)