Repository navigation
Compute double TensorPrimitives.Exp10 and Exp2 without rounding x * ln(b) - #136059
Draft
pcshrosbree wants to merge 1 commit into
Draft
pcshrosbree wants to merge 1 commit into
pcshrosbree wants to merge 1 commit into
Conversation
For double, the vectorized Exp10 and Exp2 computed Exp(x * ln(b)). Rounding ln(b) and the product leaves an absolute error in the argument that grows with |x|, and Exp turns it into the same relative error in the result: in my measurements, up to 1115 ulps for Exp10 and 726 for Exp2. Exp2(1024) also returned a finite value instead of infinity. Add dedicated kernels for double that reduce the argument in the base's own terms. For 2^x, n = round(x) and x - n is exact. For 10^x, n = round(x * log2(10)), and x - n * log10(2) is formed with log10(2) split into a 40-bit high part and a low part, so the reduction is accurate whether or not MultiplyAddEstimate fuses. Both then evaluate the polynomial of VectorMath.ExpDouble. When every lane is within conservative bounds (|x| <= 1020 for 2^x, |x| <= 307 for 10^x), every result is a normal double and 2^n is applied by adding n to the exponent field. Otherwise a separate path scales by two powers of two, so subnormal results round once and large ones become infinity. For 2^20 random inputs across the whole range, plus inputs next to the overflow threshold and special values, both functions were within 2 ulps of a 40-digit decimal reference rounded to double. That held with Vector512, Vector256 and Vector128 on x64, with Vector128 on x64 without FMA, and with Vector128 on arm64. Exp10 was measured through TensorPrimitives on 4,096 doubles. On x64 with Vector512, the new code takes 0.36 ns per element against 0.46 for the shipped code. On arm64 with Vector128, it takes 0.80 ns per element against 0.93.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-numerics |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #136058.
I opened this as a draft for feedback on the two questions in #136058: whether to take this approach, and where the kernels should live.
The bug
For
double, the vectorizedTensorPrimitives.Exp10andExp2computeExp(x * ln(b)). Rounding ln(b) to a double, and then rounding the product, leaves an absolute error in the argument that grows with |x|.Expturns that argument error into the same relative error in the result; the measured errors are under Accuracy below.Exp2(1024)also returns 1.7976931348622732E+308 instead of +∞, which #136011 lists under "Not changed here".Exp10M1andExp2M1use the same operators.The fix
The new
ExpDoubleCorekernels reduce the argument in the base's own terms instead of formingx * ln(b):MultiplyAddEstimatefuses.Both evaluate e^r with the polynomial of
VectorMath.ExpDouble, copied with its AMD attribution. The fast path uses conservative bounds: |x| ≤ 1020 forExp2and |x| ≤ 307 forExp10in every lane. Within these bounds, every result is a normal double, and 2^n is applied by adding n to the exponent field. Otherwise a separate, non-inlined path clamps the input and scales by 2^n1 · 2^n2 with n1 + n2 = n, so a subnormal result is rounded once and an overflowing one becomes +∞. Some vectors whose results are all normal also take this path; for example, an input lane of 1023 exceeds the fast-path bound forExp2. NaN inputs are returned unchanged. The shipped code falls back to scalar code for every vector with a NaN lane or a lane where |x · ln(b)| > 708, which is whereExpDoublestops using its vector path.Exp10OperatorandExp2Operatorcall these kernels fordoubleonly; thefloatpaths are unchanged. #136011 changes the same operators forfloat, so whichever of the two PRs merges second will need a small rebase.Accuracy
I compared
TensorPrimitives.Exp10andExp2from the built library with a reference computed in 40-digit decimal arithmetic and rounded to double. The inputs were 2^20 random values from [-323, 308.25) forExp10and [-1074, 1024) forExp2, 2^18 from [-1, 1), the 2^15 doubles around the overflow threshold, and 28 edge cases including NaN, ±∞ and -0.Exp10Exp2In every configuration I tested, the largest error after the change is 2 ulps, and every tested result agrees with the reference on whether it is NaN, infinite or finite. I tested Vector512, Vector256 and Vector128 on x64; Vector128 on x64 with AVX2 (and so FMA) disabled; and Vector128 on arm64. Before the change,
Exp2(1024)was finite instead of +∞.Cost
The table reports timings of
TensorPrimitives.Exp10andExp2over 4,096 doubles, in ns per element. I used a simple timing loop, alternating the shipped package and this PR's library built for net10.0, and report the fastest of repeated runs. Both ran on .NET 10.0.12. The x64 runs were pinned to two cores on a shared Ryzen 9 9950X machine; the arm64 runs used an Apple M5 Max. The inputs are in [-300, 300] forExp10and [-1000, 1000] forExp2, where every result is a normal double.Exp10shippedExp10this PRExp2shippedExp2this PRWith inputs spread over [-323, 308) for
Exp10and [-1074, 1024) forExp2, where some vectors have lanes outside the fast path's bounds, this PR is 1.4 to 4.2 times as fast as the shipped code.Tests
The new
DoubleGenericTensorPrimitives.Exp10AndExp2_WholeRange_WithinUlpsOfScalartest compares each result withdouble.Exp10ordouble.Exp2, allowing 4 ulps. It compares in ulps rather than with a relative tolerance so that subnormal results are judged correctly. If either the result or the scalar method's value is NaN or infinite, the two must agree: both NaN, or the same infinity. The test does not check NaN payloads. The inputs are 2^16 random values across the whole range, the 64 doubles around each threshold where the result overflows, becomes subnormal or rounds to zero, every integer from -1080 to 1030, and special values. Without the fix, it fails for both functions; with the fix, it passes.The System.Numerics.Tensors tests pass on linux-x64 Release, with 6268 tests and 0 failures. I ran them five times: by default (Vector512), with
DOTNET_PreferredVectorBitWidth=256, withDOTNET_PreferredVectorBitWidth=128, withDOTNET_EnableHWIntrinsic=0, and withDOTNET_EnableAVX2=0.