From 8e4596be2043cac531706bd46edfb697c400a8be Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Tue, 27 Feb 2024 23:31:43 +0000 Subject: [PATCH 1/4] Increase tolerances for floating point types in TP tests. --- .../System.Numerics.Tensors/tests/Helpers.cs | 51 ++++++ .../tests/TensorPrimitives.Generic.cs | 150 ++++++++++-------- .../TensorPrimitives.NonGeneric.Single.cs | 5 +- .../tests/TensorPrimitivesTests.cs | 30 +++- 4 files changed, 162 insertions(+), 74 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/tests/Helpers.cs b/src/libraries/System.Numerics.Tensors/tests/Helpers.cs index d6b5eef63d9dae..f3f3f83d0c5a81 100644 --- a/src/libraries/System.Numerics.Tensors/tests/Helpers.cs +++ b/src/libraries/System.Numerics.Tensors/tests/Helpers.cs @@ -11,5 +11,56 @@ internal static class Helpers public static IEnumerable TensorLengthsIncluding0 => Enumerable.Range(0, 257); public static IEnumerable TensorLengths => Enumerable.Range(1, 256); + + private const float DefaultFloatTolerance = 1e-6f; +#if NETCOREAPP + private const double DefaultDoubleTolerance = 1e-15; + private const float DefaultHalfTolerance = 1e-4f; + + private static class DefaultTolerance where T : unmanaged, INumber + { + public static readonly T Value = DetermineTolerance(DefaultDoubleTolerance, DefaultFloatTolerance, Half.CreateTruncating(DefaultHalfTolerance)) ?? T.CreateTruncating(0); + } + + public static bool IsEqualWithTolerance(T expected, T actual, T? tolerance = null) where T : unmanaged, INumber + { + tolerance = tolerance ?? DefaultTolerance.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) + { + tolerance ??= DefaultFloatTolerance; + float diff = MathF.Abs(expected - actual); + return !(diff > tolerance && diff > MathF.Max(MathF.Abs(expected), MathF.Abs(actual)) * tolerance); + } +#endif + + public static T? DetermineTolerance( + double? doubleTolerance = null, + float? floatTolerance = null +#if NETCOREAPP + , Half? halfTolerance = null +#endif + ) where T : struct + { + if (typeof(T) == typeof(double) && doubleTolerance != null) + { + return (T?)(object)doubleTolerance; + } + else if (typeof(T) == typeof(float) && floatTolerance != null) + { + return (T?)(object)floatTolerance; + } +#if NETCOREAPP + else if (typeof(T) == typeof(Half) && halfTolerance != null) + { + return (T?)(object)halfTolerance; + } +#endif + + return null; + } } } diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs index cd6ae2455491c8..da584bd8fdd761 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs @@ -113,7 +113,7 @@ private static void ConvertTruncatingImpl() for (int i = 0; i < tensorLength; i++) { - if (!IsEqualWithTolerance(TTo.CreateTruncating(source.Span[i]), destination.Span[i])) + if (!Helpers.IsEqualWithTolerance(TTo.CreateTruncating(source.Span[i]), destination.Span[i])) { throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Actual: {destination.Span[i]}. Expected: {TTo.CreateTruncating(source.Span[i])}."); } @@ -145,7 +145,7 @@ private static void ConvertSaturatingImpl() for (int i = 0; i < tensorLength; i++) { - if (!IsEqualWithTolerance(TTo.CreateSaturating(source.Span[i]), destination.Span[i])) + if (!Helpers.IsEqualWithTolerance(TTo.CreateSaturating(source.Span[i]), destination.Span[i])) { throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Actual: {destination.Span[i]}. Expected: {TTo.CreateSaturating(source.Span[i])}."); } @@ -177,7 +177,7 @@ private static void ConvertCheckedImpl() for (int i = 0; i < tensorLength; i++) { - if (!IsEqualWithTolerance(TTo.CreateChecked(source.Span[i]), destination.Span[i])) + if (!Helpers.IsEqualWithTolerance(TTo.CreateChecked(source.Span[i]), destination.Span[i])) { throw new XunitException($"{typeof(TFrom).Name} => {typeof(TTo).Name}. Input: {source.Span[i]}. Actual: {destination.Span[i]}. Expected: {TTo.CreateChecked(source.Span[i])}."); } @@ -199,7 +199,7 @@ private static void ConvertCheckedImpl(TFrom valid, TFrom invalid) TensorPrimitives.ConvertChecked(source.Span, destination.Span); foreach (TTo result in destination.Span) { - Assert.True(IsEqualWithTolerance(TTo.CreateChecked(valid), result)); + Assert.True(Helpers.IsEqualWithTolerance(TTo.CreateChecked(valid), result)); } // Test with at least one invalid @@ -211,19 +211,6 @@ private static void ConvertCheckedImpl(TFrom valid, TFrom invalid) } }; } - - private static bool IsEqualWithTolerance(T expected, T actual, T? tolerance = null) where T : unmanaged, INumber - { - tolerance ??= T.CreateTruncating(0.0001); - - T diff = T.Abs(expected - actual); - if (diff > tolerance && diff > T.Max(T.Abs(expected), T.Abs(actual)) * tolerance) - { - return false; - } - - return true; - } } // The tests for some types have been marked as OuterLoop simply to decrease inner loop testing time. @@ -371,18 +358,27 @@ public static IEnumerable SpanDestinationFunctionsToTest() yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Atanh), new Func(T.Atanh) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.AtanPi), new Func(T.AtanPi) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Atan), new Func(T.Atan) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cbrt), new Func(T.Cbrt) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cbrt), new Func(T.Cbrt), Helpers.DetermineTolerance(doubleTolerance: 1e-13) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Ceiling), new Func(T.Ceiling) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cos), new Func(T.Cos) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cosh), new Func(T.Cosh) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.CosPi), new Func(T.CosPi) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cos), new Func(T.Cos), Helpers.DetermineTolerance(doubleTolerance: 1e-4) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cosh), new Func(T.Cosh), Helpers.DetermineTolerance(doubleTolerance: 1e-14) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.CosPi), new Func(T.CosPi), Helpers.DetermineTolerance(doubleTolerance: 1e-4) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.DegreesToRadians), new Func(T.DegreesToRadians) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp), new Func(T.Exp) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp2), new Func(T.Exp2) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp10), new Func(T.Exp10) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ExpM1), new Func(T.ExpM1) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp2M1), new Func(T.Exp2M1) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp10M1), new Func(T.Exp10M1) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp2), new Func(T.Exp2), Helpers.DetermineTolerance(doubleTolerance: 1e-6, floatTolerance: 1e-5f) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp10), new Func(T.Exp10), Helpers.DetermineTolerance(doubleTolerance: 1e-6, floatTolerance: 1e-5f) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ExpM1), new Func(T.ExpM1), Helpers.DetermineTolerance(doubleTolerance: 1e-6) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp2M1), new Func(T.Exp2M1), Helpers.DetermineTolerance(doubleTolerance: 1e-6, floatTolerance: 1e-5f) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp10M1), new Func(T.Exp10M1), Helpers.DetermineTolerance(doubleTolerance: 1e-6, floatTolerance: 1e-5f) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Floor), new Func(T.Floor) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Log), new Func(T.Log) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Log2), new Func(T.Log2) }; @@ -392,15 +388,21 @@ public static IEnumerable SpanDestinationFunctionsToTest() yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Log10P1), new Func(T.Log10P1) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.RadiansToDegrees), new Func(T.RadiansToDegrees) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Reciprocal), new Func(f => T.One / f) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalEstimate), new Func(T.ReciprocalEstimate), T.CreateTruncating(1.171875e-02) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalEstimate), new Func(T.ReciprocalEstimate), Helpers.DetermineTolerance(doubleTolerance: 1e-3, floatTolerance: 1e-3f) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalSqrt), new Func(f => T.One / T.Sqrt(f)) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalSqrtEstimate), new Func(T.ReciprocalSqrtEstimate), T.CreateTruncating(1.171875e-02) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalSqrtEstimate), new Func(T.ReciprocalSqrtEstimate), Helpers.DetermineTolerance(doubleTolerance: 1e-3, floatTolerance: 1e-3f) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Round), new Func(T.Round) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sin), new Func(T.Sin) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sinh), new Func(T.Sinh) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.SinPi), new Func(T.SinPi) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sin), new Func(T.Sin), Helpers.DetermineTolerance(doubleTolerance: 1e-13) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sinh), new Func(T.Sinh), Helpers.DetermineTolerance(doubleTolerance: 1e-14) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.SinPi), new Func(T.SinPi), Helpers.DetermineTolerance(doubleTolerance: 1e-13, floatTolerance: 1e-4f) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sqrt), new Func(T.Sqrt) }; - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Tan), new Func(T.Tan) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Tan), new Func(T.Tan), Helpers.DetermineTolerance(doubleTolerance: 1e-10) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Tanh), new Func(T.Tanh) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.TanPi), new Func(T.TanPi) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Truncate), new Func(T.Truncate) }; @@ -516,12 +518,12 @@ public static IEnumerable SpanSpanDestinationFunctionsToTest() yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Hypot), new Func(T.Hypot) }; yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Ieee754Remainder), new Func(T.Ieee754Remainder) }; yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Log), new Func(T.Log) }; - yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Pow), new Func(T.Pow) }; + yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Pow), new Func(T.Pow), Helpers.DetermineTolerance(doubleTolerance: 1e-13, floatTolerance: 1e-4f) }; } [Theory] [MemberData(nameof(SpanSpanDestinationFunctionsToTest))] - public void SpanSpanDestination_AllLengths(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanSpanDestination_AllLengths(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -532,14 +534,14 @@ public void SpanSpanDestination_AllLengths(SpanSpanDestinationDelegate tensorPri tensorPrimitivesMethod(x, y, destination); for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i]); + AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i], tolerance); } }); } [Theory] [MemberData(nameof(SpanSpanDestinationFunctionsToTest))] - public void SpanSpanDestination_InPlace(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanSpanDestination_InPlace(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -550,14 +552,14 @@ public void SpanSpanDestination_InPlace(SpanSpanDestinationDelegate tensorPrimit for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(xOrig[i], xOrig[i]), x[i]); + AssertEqualTolerance(expectedMethod(xOrig[i], xOrig[i]), x[i], tolerance); } }); } [Theory] [MemberData(nameof(SpanSpanDestinationFunctionsToTest))] - public void SpanSpanDestination_SpecialValues(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanSpanDestination_SpecialValues(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengths, tensorLength => { @@ -570,7 +572,7 @@ public void SpanSpanDestination_SpecialValues(SpanSpanDestinationDelegate tensor tensorPrimitivesMethod(x.Span, y.Span, destination.Span); for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i]); + AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i], tolerance); } }, x); @@ -587,8 +589,11 @@ public void SpanSpanDestination_SpecialValues(SpanSpanDestinationDelegate tensor [Theory] [MemberData(nameof(SpanSpanDestinationFunctionsToTest))] - public void SpanSpanDestination_ThrowsForMismatchedLengths(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanSpanDestination_ThrowsForMismatchedLengths(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + Assert.All(Helpers.TensorLengths, tensorLength => { using BoundedMemory x = CreateAndFillTensor(tensorLength); @@ -602,8 +607,11 @@ public void SpanSpanDestination_ThrowsForMismatchedLengths(SpanSpanDestinationDe [Theory] [MemberData(nameof(SpanSpanDestinationFunctionsToTest))] - public void SpanSpanDestination_ThrowsForTooShortDestination(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanSpanDestination_ThrowsForTooShortDestination(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + Assert.All(Helpers.TensorLengths, tensorLength => { using BoundedMemory x = CreateAndFillTensor(tensorLength); @@ -616,8 +624,11 @@ public void SpanSpanDestination_ThrowsForTooShortDestination(SpanSpanDestination [Theory] [MemberData(nameof(SpanSpanDestinationFunctionsToTest))] - public void SpanSpanDestination_ThrowsForOverlapppingInputsWithOutputs(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanSpanDestination_ThrowsForOverlapppingInputsWithOutputs(SpanSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + T[] array = new T[10]; AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(5, 2), array.AsSpan(0, 2))); AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), array.AsSpan(5, 2), array.AsSpan(2, 2))); @@ -633,7 +644,8 @@ public static IEnumerable SpanScalarDestinationFunctionsToTest() yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.Atan2Pi), new Func(T.Atan2Pi) }; yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.CopySign), new Func(T.CopySign) }; yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.Ieee754Remainder), new Func(T.Ieee754Remainder) }; - yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.Pow), new Func(T.Pow) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.Pow), new Func(T.Pow), Helpers.DetermineTolerance(doubleTolerance: 1e-13, floatTolerance: 1e-4f) }; yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.Log), new Func(T.Log) }; yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.Max), new Func(T.Max) }; yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.MaxMagnitude), new Func(T.MaxMagnitude) }; @@ -643,7 +655,7 @@ public static IEnumerable SpanScalarDestinationFunctionsToTest() [Theory] [MemberData(nameof(SpanScalarDestinationFunctionsToTest))] - public void SpanScalarDestination_AllLengths(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanScalarDestination_AllLengths(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -654,14 +666,14 @@ public void SpanScalarDestination_AllLengths(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanScalarDestination_InPlace(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -673,14 +685,14 @@ public void SpanScalarDestination_InPlace(SpanScalarDestinationDelegate for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(xOrig[i], y), x[i]); + AssertEqualTolerance(expectedMethod(xOrig[i], y), x[i], tolerance); } }); } [Theory] [MemberData(nameof(SpanScalarDestinationFunctionsToTest))] - public void SpanScalarDestination_SpecialValues(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanScalarDestination_SpecialValues(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengths, tensorLength => { @@ -693,7 +705,7 @@ public void SpanScalarDestination_SpecialValues(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanScalarDestination_ThrowsForTooShortDestination(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + Assert.All(Helpers.TensorLengths, tensorLength => { using BoundedMemory x = CreateAndFillTensor(tensorLength); @@ -715,8 +730,11 @@ public void SpanScalarDestination_ThrowsForTooShortDestination(SpanScalarDestina [Theory] [MemberData(nameof(SpanScalarDestinationFunctionsToTest))] - public void SpanScalarDestination_ThrowsForOverlapppingInputsWithOutputs(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanScalarDestination_ThrowsForOverlapppingInputsWithOutputs(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + T[] array = new T[10]; AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), default, array.AsSpan(0, 2))); AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), default, array.AsSpan(2, 2))); @@ -817,13 +835,14 @@ public void SpanScalarFloatDestination_ThrowsForOverlapppingInputsWithOutputs(Sc #region Span,Int,Span -> Destination public static IEnumerable SpanIntDestinationFunctionsToTest() { - yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.RootN), new Func(T.RootN) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.RootN), new Func(T.RootN), Helpers.DetermineTolerance(doubleTolerance: 1e-13) }; yield return new object[] { new SpanScalarDestinationDelegate(TensorPrimitives.ScaleB), new Func(T.ScaleB) }; } [Theory] [MemberData(nameof(SpanIntDestinationFunctionsToTest))] - public void SpanIntDestination_AllLengths(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanIntDestination_AllLengths(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -834,14 +853,14 @@ public void SpanIntDestination_AllLengths(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanIntDestination_InPlace(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -853,14 +872,14 @@ public void SpanIntDestination_InPlace(SpanScalarDestinationDelegate for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(xOrig[i], y), x[i]); + AssertEqualTolerance(expectedMethod(xOrig[i], y), x[i], tolerance); } }); } [Theory] [MemberData(nameof(SpanIntDestinationFunctionsToTest))] - public void SpanIntDestination_SpecialValues(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanIntDestination_SpecialValues(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengths, tensorLength => { @@ -873,7 +892,7 @@ public void SpanIntDestination_SpecialValues(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanIntDestination_ThrowsForTooShortDestination(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + Assert.All(Helpers.TensorLengths, tensorLength => { using BoundedMemory x = CreateAndFillTensor(tensorLength); @@ -895,8 +917,11 @@ public void SpanIntDestination_ThrowsForTooShortDestination(SpanScalarDestinatio [Theory] [MemberData(nameof(SpanIntDestinationFunctionsToTest))] - public void SpanIntDestination_ThrowsForOverlapppingInputsWithOutputs(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanIntDestination_ThrowsForOverlapppingInputsWithOutputs(SpanScalarDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + T[] array = new T[10]; AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), 2, array.AsSpan(0, 2))); AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(array.AsSpan(1, 2), 2, array.AsSpan(2, 2))); @@ -2071,10 +2096,7 @@ protected override T NextRandom() protected override void AssertEqualTolerance(T expected, T actual, T? tolerance = null) { - tolerance ??= T.CreateTruncating(0.0001); - - T diff = T.Abs(expected - actual); - if (diff > tolerance && diff > T.Max(T.Abs(expected), T.Abs(actual)) * tolerance) + if (!Helpers.IsEqualWithTolerance(expected, actual, tolerance)) { throw EqualException.ForMismatchedValues(expected, actual); } diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.NonGeneric.Single.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.NonGeneric.Single.cs index ac883851299d8f..d6df8365c59db6 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.NonGeneric.Single.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.NonGeneric.Single.cs @@ -106,10 +106,7 @@ protected override float MinMagnitude(float x, float y) protected override void AssertEqualTolerance(float expected, float actual, float? tolerance = null) { - tolerance ??= 0.0001f; - - double diff = Math.Abs((double)expected - (double)actual); - if (diff > tolerance && diff > Math.Max(Math.Abs(expected), Math.Abs(actual)) * tolerance) + if (!Helpers.IsEqualWithTolerance(expected, actual, tolerance)) { throw EqualException.ForMismatchedValues(expected, actual); } diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs index b0b44fddb0aef5..1617327f7b670d 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs @@ -663,6 +663,9 @@ public void Cosh_ValueRange() { if (!IsFloatingPoint) return; + // TODO https://github.com/dotnet/runtime/issues/98861 + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-14); + Assert.All(VectorLengthAndIteratedRange(ConvertFromSingle(-100f), ConvertFromSingle(100f), ConvertFromSingle(3f)), arg => { T[] x = new T[arg.Length]; @@ -674,7 +677,7 @@ public void Cosh_ValueRange() T expected = Cosh(arg.Element); foreach (T actual in dest) { - AssertEqualTolerance(expected, actual); + AssertEqualTolerance(expected, actual, tolerance); } }); } @@ -952,6 +955,9 @@ public void Dot_ThrowsForMismatchedLengths_x_y() [Fact] public void Dot_AllLengths() { + // TODO https://github.com/dotnet/runtime/issues/98861 + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-14f, floatTolerance: 1e-3f); + Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { using BoundedMemory x = CreateAndFillTensor(tensorLength); @@ -963,7 +969,7 @@ public void Dot_AllLengths() dot = Add(dot, Multiply(x[i], y[i])); } - AssertEqualTolerance(dot, Dot(x, y)); + AssertEqualTolerance(dot, Dot(x, y), tolerance); }); } #endregion @@ -2879,6 +2885,9 @@ public void Sinh_ValueRange() { if (!IsFloatingPoint) return; + // TODO https://github.com/dotnet/runtime/issues/98861 + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-14); + Assert.All(VectorLengthAndIteratedRange(ConvertFromSingle(-100f), ConvertFromSingle(100f), ConvertFromSingle(3f)), args => { T[] x = new T[args.Length]; @@ -2890,7 +2899,7 @@ public void Sinh_ValueRange() T expected = Sinh(args.Element); foreach (T actual in dest) { - AssertEqualTolerance(expected, actual); + AssertEqualTolerance(expected, actual, tolerance); } }); } @@ -3139,6 +3148,9 @@ public void Subtract_TensorScalar_ThrowsForOverlapppingInputsWithOutputs() [Fact] public void Sum_AllLengths() { + // TODO https://github.com/dotnet/runtime/issues/98861 + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-14, floatTolerance: 1e-5f); + Assert.All(Helpers.TensorLengths, tensorLength => { using BoundedMemory x = CreateAndFillTensor(tensorLength); @@ -3148,7 +3160,7 @@ public void Sum_AllLengths() { sum = Add(sum, value); } - AssertEqualTolerance(sum, Sum(x)); + AssertEqualTolerance(sum, Sum(x), tolerance); }); } #endregion @@ -3157,6 +3169,9 @@ public void Sum_AllLengths() [Fact] public void SumOfMagnitudes_AllLengths() { + // TODO https://github.com/dotnet/runtime/issues/98861 + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-12); + Assert.All(Helpers.TensorLengths, tensorLength => { using BoundedMemory x = CreateTensor(tensorLength); @@ -3167,7 +3182,7 @@ public void SumOfMagnitudes_AllLengths() { sum = Add(sum, Abs(value)); } - AssertEqualTolerance(sum, SumOfMagnitudes(x)); + AssertEqualTolerance(sum, SumOfMagnitudes(x), tolerance); }); } #endregion @@ -3176,6 +3191,9 @@ public void SumOfMagnitudes_AllLengths() [Fact] public void SumOfSquares_AllLengths() { + // TODO https://github.com/dotnet/runtime/issues/98861 + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-12); + Assert.All(Helpers.TensorLengths, tensorLength => { using BoundedMemory x = CreateAndFillTensor(tensorLength); @@ -3185,7 +3203,7 @@ public void SumOfSquares_AllLengths() { sum = Add(sum, Multiply(value, value)); } - AssertEqualTolerance(sum, SumOfSquares(x)); + AssertEqualTolerance(sum, SumOfSquares(x), tolerance); }); } #endregion From 14ad09894609eb5613c88cd4304712a4e9d3790b Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 29 Feb 2024 14:30:31 +0000 Subject: [PATCH 2/4] Use tolerances as defined in the scalar test suites. --- .../System.Numerics.Tensors/tests/Helpers.cs | 11 +++--- .../tests/TensorPrimitives.Generic.cs | 36 +++++++++++-------- .../tests/TensorPrimitivesTests.cs | 4 +-- 3 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/tests/Helpers.cs b/src/libraries/System.Numerics.Tensors/tests/Helpers.cs index f3f3f83d0c5a81..eb9622b57ff4bf 100644 --- a/src/libraries/System.Numerics.Tensors/tests/Helpers.cs +++ b/src/libraries/System.Numerics.Tensors/tests/Helpers.cs @@ -12,11 +12,14 @@ internal static class Helpers public static IEnumerable TensorLengths => Enumerable.Range(1, 256); - private const float DefaultFloatTolerance = 1e-6f; -#if NETCOREAPP - private const double DefaultDoubleTolerance = 1e-15; - private const float DefaultHalfTolerance = 1e-4f; + // Tolerances taken from testing in the scalar math routines: + // cf. https://github.com/dotnet/runtime/blob/89f7ad3b276fb0b48f20cb4e8408bdce85c2b415/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs + public const double DefaultDoubleTolerance = 8.8817841970012523e-16; + public const float DefaultFloatTolerance = 4.76837158e-07f; + public const float DefaultHalfTolerance = 1e-4f; + public const double DefaultToleranceForEstimates = 1.171875e-02; +#if NETCOREAPP private static class DefaultTolerance where T : unmanaged, INumber { public static readonly T Value = DetermineTolerance(DefaultDoubleTolerance, DefaultFloatTolerance, Half.CreateTruncating(DefaultHalfTolerance)) ?? T.CreateTruncating(0); diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs index da584bd8fdd761..1f46f2f2adc2dd 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs @@ -366,7 +366,7 @@ public static IEnumerable SpanDestinationFunctionsToTest() // TODO https://github.com/dotnet/runtime/issues/98861 yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cosh), new Func(T.Cosh), Helpers.DetermineTolerance(doubleTolerance: 1e-14) }; // TODO https://github.com/dotnet/runtime/issues/98861 - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.CosPi), new Func(T.CosPi), Helpers.DetermineTolerance(doubleTolerance: 1e-4) }; + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.CosPi), new Func(T.CosPi), Helpers.DetermineTolerance(doubleTolerance: 1e-4, floatTolerance: 1e-4f) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.DegreesToRadians), new Func(T.DegreesToRadians) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Exp), new Func(T.Exp) }; // TODO https://github.com/dotnet/runtime/issues/98861 @@ -388,11 +388,9 @@ public static IEnumerable SpanDestinationFunctionsToTest() yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Log10P1), new Func(T.Log10P1) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.RadiansToDegrees), new Func(T.RadiansToDegrees) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Reciprocal), new Func(f => T.One / f) }; - // TODO https://github.com/dotnet/runtime/issues/98861 - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalEstimate), new Func(T.ReciprocalEstimate), Helpers.DetermineTolerance(doubleTolerance: 1e-3, floatTolerance: 1e-3f) }; + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalEstimate), new Func(T.ReciprocalEstimate), T.CreateTruncating(Helpers.DefaultToleranceForEstimates) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalSqrt), new Func(f => T.One / T.Sqrt(f)) }; - // TODO https://github.com/dotnet/runtime/issues/98861 - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalSqrtEstimate), new Func(T.ReciprocalSqrtEstimate), Helpers.DetermineTolerance(doubleTolerance: 1e-3, floatTolerance: 1e-3f) }; + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalSqrtEstimate), new Func(T.ReciprocalSqrtEstimate), T.CreateTruncating(Helpers.DefaultToleranceForEstimates) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Round), new Func(T.Round) }; // TODO https://github.com/dotnet/runtime/issues/98861 yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sin), new Func(T.Sin), Helpers.DetermineTolerance(doubleTolerance: 1e-13) }; @@ -518,6 +516,7 @@ public static IEnumerable SpanSpanDestinationFunctionsToTest() yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Hypot), new Func(T.Hypot) }; yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Ieee754Remainder), new Func(T.Ieee754Remainder) }; yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Log), new Func(T.Log) }; + // TODO https://github.com/dotnet/runtime/issues/98861 yield return new object[] { new SpanSpanDestinationDelegate(TensorPrimitives.Pow), new Func(T.Pow), Helpers.DetermineTolerance(doubleTolerance: 1e-13, floatTolerance: 1e-4f) }; } @@ -581,7 +580,7 @@ public void SpanSpanDestination_SpecialValues(SpanSpanDestinationDelegate tensor tensorPrimitivesMethod(x.Span, y.Span, destination.Span); for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i]); + AssertEqualTolerance(expectedMethod(x[i], y[i]), destination[i], tolerance); } }, y); }); @@ -746,13 +745,14 @@ public static IEnumerable ScalarSpanFloatDestinationFunctionsToTest() { yield return new object[] { new ScalarSpanDestinationDelegate(TensorPrimitives.Atan2), new Func(T.Atan2) }; yield return new object[] { new ScalarSpanDestinationDelegate(TensorPrimitives.Atan2Pi), new Func(T.Atan2Pi) }; - yield return new object[] { new ScalarSpanDestinationDelegate(TensorPrimitives.Pow), new Func(T.Pow) }; + // TODO https://github.com/dotnet/runtime/issues/98861 + yield return new object[] { new ScalarSpanDestinationDelegate(TensorPrimitives.Pow), new Func(T.Pow), Helpers.DetermineTolerance(floatTolerance: 1e-4f)}; yield return new object[] { new ScalarSpanDestinationDelegate(TensorPrimitives.Ieee754Remainder), new Func(T.Ieee754Remainder) }; } [Theory] [MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))] - public void SpanScalarFloatDestination_AllLengths(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanScalarFloatDestination_AllLengths(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -763,14 +763,14 @@ public void SpanScalarFloatDestination_AllLengths(ScalarSpanDestinationDelegate tensorPrimitivesMethod(x, y, destination); for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(x, y[i]), destination[i]); + AssertEqualTolerance(expectedMethod(x, y[i]), destination[i], tolerance); } }); } [Theory] [MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))] - public void SpanScalarFloatDestination_InPlace(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void SpanScalarFloatDestination_InPlace(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengthsIncluding0, tensorLength => { @@ -782,14 +782,14 @@ public void SpanScalarFloatDestination_InPlace(ScalarSpanDestinationDelegate ten for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(x, yOrig[i]), y[i]); + AssertEqualTolerance(expectedMethod(x, yOrig[i]), y[i], tolerance); } }); } [Theory] [MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))] - public void ScalarSpanDestination_SpecialValues(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod) + public void ScalarSpanDestination_SpecialValues(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { Assert.All(Helpers.TensorLengths, tensorLength => { @@ -802,7 +802,7 @@ public void ScalarSpanDestination_SpecialValues(ScalarSpanDestinationDelegate te tensorPrimitivesMethod(x, y.Span, destination.Span); for (int i = 0; i < tensorLength; i++) { - AssertEqualTolerance(expectedMethod(x, y[i]), destination[i]); + AssertEqualTolerance(expectedMethod(x, y[i]), destination[i], tolerance); } }, y); }); @@ -810,8 +810,11 @@ public void ScalarSpanDestination_SpecialValues(ScalarSpanDestinationDelegate te [Theory] [MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))] - public void SpanScalarFloatDestination_ThrowsForTooShortDestination(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanScalarFloatDestination_ThrowsForTooShortDestination(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + Assert.All(Helpers.TensorLengths, tensorLength => { T x = NextRandom(); @@ -824,8 +827,11 @@ public void SpanScalarFloatDestination_ThrowsForTooShortDestination(ScalarSpanDe [Theory] [MemberData(nameof(ScalarSpanFloatDestinationFunctionsToTest))] - public void SpanScalarFloatDestination_ThrowsForOverlapppingInputsWithOutputs(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func _) + public void SpanScalarFloatDestination_ThrowsForOverlapppingInputsWithOutputs(ScalarSpanDestinationDelegate tensorPrimitivesMethod, Func expectedMethod, T? tolerance = null) { + _ = expectedMethod; + _ = tolerance; + T[] array = new T[10]; AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(default, array.AsSpan(1, 2), array.AsSpan(0, 2))); AssertExtensions.Throws("destination", () => tensorPrimitivesMethod(default, array.AsSpan(1, 2), array.AsSpan(2, 2))); diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs index 1617327f7b670d..3721becb3bb5b2 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs @@ -3170,7 +3170,7 @@ public void Sum_AllLengths() public void SumOfMagnitudes_AllLengths() { // TODO https://github.com/dotnet/runtime/issues/98861 - T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-12); + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-12, floatTolerance: 1e-6f); Assert.All(Helpers.TensorLengths, tensorLength => { @@ -3192,7 +3192,7 @@ public void SumOfMagnitudes_AllLengths() public void SumOfSquares_AllLengths() { // TODO https://github.com/dotnet/runtime/issues/98861 - T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-12); + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-12, floatTolerance: 1e-6f); Assert.All(Helpers.TensorLengths, tensorLength => { From 1ce4bd40ded553bb0d6d5e5d3ef864a79593f858 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 29 Feb 2024 15:10:47 +0000 Subject: [PATCH 3/4] Use the same Half baseline as with scalar tests. --- src/libraries/System.Numerics.Tensors/tests/Helpers.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Numerics.Tensors/tests/Helpers.cs b/src/libraries/System.Numerics.Tensors/tests/Helpers.cs index eb9622b57ff4bf..729cacda351695 100644 --- a/src/libraries/System.Numerics.Tensors/tests/Helpers.cs +++ b/src/libraries/System.Numerics.Tensors/tests/Helpers.cs @@ -14,9 +14,10 @@ internal static class Helpers // Tolerances taken from testing in the scalar math routines: // cf. https://github.com/dotnet/runtime/blob/89f7ad3b276fb0b48f20cb4e8408bdce85c2b415/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/Math.cs + // and https://github.com/dotnet/runtime/blob/fd48b6f5d1ff81a81d09e9d72982cc9e8d139852/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/HalfTests.cs public const double DefaultDoubleTolerance = 8.8817841970012523e-16; public const float DefaultFloatTolerance = 4.76837158e-07f; - public const float DefaultHalfTolerance = 1e-4f; + public const float DefaultHalfTolerance = 3.90625e-03f; public const double DefaultToleranceForEstimates = 1.171875e-02; #if NETCOREAPP From 38c23c07d6876ba0238ba56c530a1087f328a5c9 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 29 Feb 2024 18:16:17 +0000 Subject: [PATCH 4/4] Increase tolerance for tests failing in CI hardware. --- .../tests/TensorPrimitives.Generic.cs | 6 +++--- .../System.Numerics.Tensors/tests/TensorPrimitivesTests.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs index 1f46f2f2adc2dd..9328ab9e9c0515 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitives.Generic.cs @@ -362,7 +362,7 @@ public static IEnumerable SpanDestinationFunctionsToTest() yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cbrt), new Func(T.Cbrt), Helpers.DetermineTolerance(doubleTolerance: 1e-13) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Ceiling), new Func(T.Ceiling) }; // TODO https://github.com/dotnet/runtime/issues/98861 - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cos), new Func(T.Cos), Helpers.DetermineTolerance(doubleTolerance: 1e-4) }; + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cos), new Func(T.Cos), Helpers.DetermineTolerance(doubleTolerance: 1e-4, floatTolerance: 1e-4f) }; // TODO https://github.com/dotnet/runtime/issues/98861 yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Cosh), new Func(T.Cosh), Helpers.DetermineTolerance(doubleTolerance: 1e-14) }; // TODO https://github.com/dotnet/runtime/issues/98861 @@ -393,14 +393,14 @@ public static IEnumerable SpanDestinationFunctionsToTest() yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.ReciprocalSqrtEstimate), new Func(T.ReciprocalSqrtEstimate), T.CreateTruncating(Helpers.DefaultToleranceForEstimates) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Round), new Func(T.Round) }; // TODO https://github.com/dotnet/runtime/issues/98861 - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sin), new Func(T.Sin), Helpers.DetermineTolerance(doubleTolerance: 1e-13) }; + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sin), new Func(T.Sin), Helpers.DetermineTolerance(doubleTolerance: 1e-13, floatTolerance: 1e-4f) }; // TODO https://github.com/dotnet/runtime/issues/98861 yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sinh), new Func(T.Sinh), Helpers.DetermineTolerance(doubleTolerance: 1e-14) }; // TODO https://github.com/dotnet/runtime/issues/98861 yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.SinPi), new Func(T.SinPi), Helpers.DetermineTolerance(doubleTolerance: 1e-13, floatTolerance: 1e-4f) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Sqrt), new Func(T.Sqrt) }; // TODO https://github.com/dotnet/runtime/issues/98861 - yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Tan), new Func(T.Tan), Helpers.DetermineTolerance(doubleTolerance: 1e-10) }; + yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Tan), new Func(T.Tan), Helpers.DetermineTolerance(doubleTolerance: 1e-10, floatTolerance: 1e-4f) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Tanh), new Func(T.Tanh) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.TanPi), new Func(T.TanPi) }; yield return new object[] { new SpanDestinationDelegate(TensorPrimitives.Truncate), new Func(T.Truncate) }; diff --git a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs index 3721becb3bb5b2..173e57649cf8ee 100644 --- a/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs +++ b/src/libraries/System.Numerics.Tensors/tests/TensorPrimitivesTests.cs @@ -3149,7 +3149,7 @@ public void Subtract_TensorScalar_ThrowsForOverlapppingInputsWithOutputs() public void Sum_AllLengths() { // TODO https://github.com/dotnet/runtime/issues/98861 - T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-14, floatTolerance: 1e-5f); + T? tolerance = Helpers.DetermineTolerance(doubleTolerance: 1e-13, floatTolerance: 1e-5f); Assert.All(Helpers.TensorLengths, tensorLength => {