Skip to content

Replace VectorXx.Exp's edge case fallback with scalar processing #107886

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

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ public static Vector512<T> Invoke(Vector512<T> x)
private const ulong V_ARG_MAX = 0x40862000_00000000; private const ulong V_ARG_MAX = 0x40862000_00000000;
private const ulong V_DP64_BIAS = 1023; private const ulong V_DP64_BIAS = 1023;


private const double V_EXPF_MIN = -709.782712893384;
private const double V_EXPF_MAX = +709.782712893384;

private const double V_EXPF_HUGE = 6755399441055744; private const double V_EXPF_HUGE = 6755399441055744;
private const double V_TBL_LN2 = 1.4426950408889634; private const double V_TBL_LN2 = 1.4426950408889634;


Expand All @@ -182,6 +179,9 @@ public static Vector512<T> Invoke(Vector512<T> x)
public static double Invoke(double x) => double.Exp(x); public static double Invoke(double x) => double.Exp(x);


public static Vector128<double> Invoke(Vector128<double> x) public static Vector128<double> Invoke(Vector128<double> x)
{
// Check if -709 < vx < 709
if (Vector128.LessThanOrEqualAll(Vector128.Abs(x).AsUInt64(), Vector128.Create(V_ARG_MAX)))
{ {
// x * (64.0 / ln(2)) // x * (64.0 / ln(2))
Vector128<double> z = x * Vector128.Create(V_TBL_LN2); Vector128<double> z = x * Vector128.Create(V_TBL_LN2);
Expand Down Expand Up @@ -211,28 +211,22 @@ public static Vector128<double> Invoke(Vector128<double> x)


// m = (n - j) / 64 // m = (n - j) / 64
// result = polynomial * 2^m // result = polynomial * 2^m
Vector128<double> ret = poly * ((n + Vector128.Create(V_DP64_BIAS)) << 52).AsDouble(); return poly * ((n + Vector128.Create(V_DP64_BIAS)) << 52).AsDouble();

}
// Check if -709 < vx < 709 else
if (Vector128.GreaterThanAny(Vector128.Abs(x).AsUInt64(), Vector128.Create(V_ARG_MAX)))
{ {
// (x > V_EXPF_MAX) ? double.PositiveInfinity : x return ScalarFallback(x);
Vector128<double> infinityMask = Vector128.GreaterThan(x, Vector128.Create(V_EXPF_MAX));

ret = Vector128.ConditionalSelect(
infinityMask,
Vector128.Create(double.PositiveInfinity),
ret
);


// (x < V_EXPF_MIN) ? 0 : x static Vector128<double> ScalarFallback(Vector128<double> x) =>
ret = Vector128.AndNot(ret, Vector128.LessThan(x, Vector128.Create(V_EXPF_MIN))); Vector128.Create(Math.Exp(x.GetElement(0)),
Math.Exp(x.GetElement(1)));
} }

return ret;
} }


public static Vector256<double> Invoke(Vector256<double> x) public static Vector256<double> Invoke(Vector256<double> x)
{
// Check if -709 < vx < 709
if (Vector256.LessThanOrEqualAll(Vector256.Abs(x).AsUInt64(), Vector256.Create(V_ARG_MAX)))
{ {
// x * (64.0 / ln(2)) // x * (64.0 / ln(2))
Vector256<double> z = x * Vector256.Create(V_TBL_LN2); Vector256<double> z = x * Vector256.Create(V_TBL_LN2);
Expand Down Expand Up @@ -262,28 +256,24 @@ public static Vector256<double> Invoke(Vector256<double> x)


// m = (n - j) / 64 // m = (n - j) / 64
// result = polynomial * 2^m // result = polynomial * 2^m
Vector256<double> ret = poly * ((n + Vector256.Create(V_DP64_BIAS)) << 52).AsDouble(); return poly * ((n + Vector256.Create(V_DP64_BIAS)) << 52).AsDouble();

}
// Check if -709 < vx < 709 else
if (Vector256.GreaterThanAny(Vector256.Abs(x).AsUInt64(), Vector256.Create(V_ARG_MAX)))
{ {
// (x > V_EXPF_MAX) ? double.PositiveInfinity : x return ScalarFallback(x);
Vector256<double> infinityMask = Vector256.GreaterThan(x, Vector256.Create(V_EXPF_MAX));

ret = Vector256.ConditionalSelect(
infinityMask,
Vector256.Create(double.PositiveInfinity),
ret
);


// (x < V_EXPF_MIN) ? 0 : x static Vector256<double> ScalarFallback(Vector256<double> x) =>
ret = Vector256.AndNot(ret, Vector256.LessThan(x, Vector256.Create(V_EXPF_MIN))); Vector256.Create(Math.Exp(x.GetElement(0)),
Math.Exp(x.GetElement(1)),
Math.Exp(x.GetElement(2)),
Math.Exp(x.GetElement(3)));
} }

return ret;
} }


public static Vector512<double> Invoke(Vector512<double> x) public static Vector512<double> Invoke(Vector512<double> x)
{
// Check if -709 < vx < 709
if (Vector512.LessThanOrEqualAll(Vector512.Abs(x).AsUInt64(), Vector512.Create(V_ARG_MAX)))
{ {
// x * (64.0 / ln(2)) // x * (64.0 / ln(2))
Vector512<double> z = x * Vector512.Create(V_TBL_LN2); Vector512<double> z = x * Vector512.Create(V_TBL_LN2);
Expand Down Expand Up @@ -313,25 +303,22 @@ public static Vector512<double> Invoke(Vector512<double> x)


// m = (n - j) / 64 // m = (n - j) / 64
// result = polynomial * 2^m // result = polynomial * 2^m
Vector512<double> ret = poly * ((n + Vector512.Create(V_DP64_BIAS)) << 52).AsDouble(); return poly * ((n + Vector512.Create(V_DP64_BIAS)) << 52).AsDouble();

}
// Check if -709 < vx < 709 else
if (Vector512.GreaterThanAny(Vector512.Abs(x).AsUInt64(), Vector512.Create(V_ARG_MAX)))
{ {
// (x > V_EXPF_MAX) ? double.PositiveInfinity : x return ScalarFallback(x);
Vector512<double> infinityMask = Vector512.GreaterThan(x, Vector512.Create(V_EXPF_MAX));


ret = Vector512.ConditionalSelect( static Vector512<double> ScalarFallback(Vector512<double> x) =>
infinityMask, Vector512.Create(Math.Exp(x.GetElement(0)),
Vector512.Create(double.PositiveInfinity), Math.Exp(x.GetElement(1)),
ret Math.Exp(x.GetElement(2)),
); Math.Exp(x.GetElement(3)),

Math.Exp(x.GetElement(4)),
// (x < V_EXPF_MIN) ? 0 : x Math.Exp(x.GetElement(5)),
ret = Vector512.AndNot(ret, Vector512.LessThan(x, Vector512.Create(V_EXPF_MIN))); Math.Exp(x.GetElement(6)),
Math.Exp(x.GetElement(7)));
} }

return ret;
} }
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -163,9 +163,17 @@ protected T NextRandom(T avoid)
/// the value is stored into a random position in <paramref name="x"/>, and the original /// the value is stored into a random position in <paramref name="x"/>, and the original
/// value is subsequently restored. /// value is subsequently restored.
/// </summary> /// </summary>
protected void RunForEachSpecialValue(Action action, BoundedMemory<T> x) protected void RunForEachSpecialValue(Action action, BoundedMemory<T> x) =>
RunForEachSpecialValue(action, x, GetSpecialValues());

/// <summary>
/// Runs the specified action for each special value. Before the action is invoked,
/// the value is stored into a random position in <paramref name="x"/>, and the original
/// value is subsequently restored.
/// </summary>
protected void RunForEachSpecialValue(Action action, BoundedMemory<T> x, IEnumerable<T> specialValues)
{ {
Assert.All(GetSpecialValues(), value => Assert.All(specialValues, value =>
{ {
int pos = Random.Next(x.Length); int pos = Random.Next(x.Length);
T orig = x[pos]; T orig = x[pos];
Expand Down Expand Up @@ -1021,14 +1029,25 @@ public void Exp_SpecialValues()
using BoundedMemory<T> x = CreateAndFillTensor(tensorLength); using BoundedMemory<T> x = CreateAndFillTensor(tensorLength);
using BoundedMemory<T> destination = CreateTensor(tensorLength); using BoundedMemory<T> destination = CreateTensor(tensorLength);


T[] additionalSpecialValues =
[
typeof(T) == typeof(float) ? (T)(object)-709.7f :
typeof(T) == typeof(double) ? (T)(object)-709.7 :
default,

typeof(T) == typeof(float) ? (T)(object)709.7f :
typeof(T) == typeof(double) ? (T)(object)709.7 :
default,
];

RunForEachSpecialValue(() => RunForEachSpecialValue(() =>
{ {
Exp(x, destination); Exp(x, destination);
for (int i = 0; i < tensorLength; i++) for (int i = 0; i < tensorLength; i++)
{ {
AssertEqualTolerance(Exp(x[i]), destination[i]); AssertEqualTolerance(Exp(x[i]), destination[i]);
} }
}, x); }, x, GetSpecialValues().Concat(additionalSpecialValues));
}); });
} }


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -391,9 +391,6 @@ public static TVectorDouble ExpDouble<TVectorDouble, TVectorUInt64>(TVectorDoubl
const ulong V_ARG_MAX = 0x40862000_00000000; const ulong V_ARG_MAX = 0x40862000_00000000;
const ulong V_DP64_BIAS = 1023; const ulong V_DP64_BIAS = 1023;


const double V_EXPF_MIN = -709.782712893384;
const double V_EXPF_MAX = +709.782712893384;

const double V_EXPF_HUGE = 6755399441055744; const double V_EXPF_HUGE = 6755399441055744;
const double V_TBL_LN2 = 1.4426950408889634; const double V_TBL_LN2 = 1.4426950408889634;


Expand All @@ -411,6 +408,9 @@ public static TVectorDouble ExpDouble<TVectorDouble, TVectorUInt64>(TVectorDoubl
const double C11 = 2.7632293298250954E-07; const double C11 = 2.7632293298250954E-07;
const double C12 = 2.499430431958571E-08; const double C12 = 2.499430431958571E-08;


// Check if -709 < vx < 709
if (TVectorUInt64.LessThanOrEqualAll(Unsafe.BitCast<TVectorDouble, TVectorUInt64>(TVectorDouble.Abs(x)), TVectorUInt64.Create(V_ARG_MAX)))
{
// x * (64.0 / ln(2)) // x * (64.0 / ln(2))
TVectorDouble dn = TVectorDouble.MultiplyAddEstimate(x, TVectorDouble.Create(V_TBL_LN2), TVectorDouble.Create(V_EXPF_HUGE)); TVectorDouble dn = TVectorDouble.MultiplyAddEstimate(x, TVectorDouble.Create(V_TBL_LN2), TVectorDouble.Create(V_EXPF_HUGE));


Expand Down Expand Up @@ -452,25 +452,25 @@ public static TVectorDouble ExpDouble<TVectorDouble, TVectorUInt64>(TVectorDoubl


// m = (n - j) / 64 // m = (n - j) / 64
// result = polynomial * 2^m // result = polynomial * 2^m
TVectorDouble result = poly * Unsafe.BitCast<TVectorUInt64, TVectorDouble>((n + TVectorUInt64.Create(V_DP64_BIAS)) << 52); return poly * Unsafe.BitCast<TVectorUInt64, TVectorDouble>((n + TVectorUInt64.Create(V_DP64_BIAS)) << 52);

}
// Check if -709 < vx < 709 else
if (TVectorUInt64.GreaterThanAny(Unsafe.BitCast<TVectorDouble, TVectorUInt64>(TVectorDouble.Abs(x)), TVectorUInt64.Create(V_ARG_MAX)))
{ {
// (x > V_EXPF_MAX) ? double.PositiveInfinity : x return ScalarFallback(x);
TVectorDouble infinityMask = TVectorDouble.GreaterThan(x, TVectorDouble.Create(V_EXPF_MAX));


result = TVectorDouble.ConditionalSelect( static TVectorDouble ScalarFallback(TVectorDouble x)
infinityMask, {
TVectorDouble.Create(double.PositiveInfinity), TVectorDouble expResult = TVectorDouble.Zero;
result
);


// (x < V_EXPF_MIN) ? 0 : x for (int i = 0; i < TVectorDouble.Count; i++)
result = TVectorDouble.AndNot(result, TVectorDouble.LessThan(x, TVectorDouble.Create(V_EXPF_MIN))); {
double expScalar = double.Exp(x[i]);
expResult = expResult.WithElement(i, expScalar);
} }


return result; return expResult;
}
}
} }


public static TVectorSingle ExpSingle<TVectorSingle, TVectorUInt32, TVectorDouble, TVectorUInt64>(TVectorSingle x) public static TVectorSingle ExpSingle<TVectorSingle, TVectorUInt32, TVectorDouble, TVectorUInt64>(TVectorSingle x)
Expand Down