Repository navigation
Scale complex elements before taking magnitudes in the rescaled p-norm - #867
Merged
Merged
Conversation
When the unscaled sum underflows, the p-norm divides each magnitude by the largest one. For complex elements with subnormal components, norm(x) is rounded to the subnormal grid before that division, so most of its bits are already gone: norm of four copies of complex(u, u), u = nextfloat(0f0), gave 2u instead of 3u, and normal-range results built from many such elements were off by hundreds of ulps. A finite sum that fails the threshold is below 1, so every component is too, and multiplying by 1/eps is exact and moves subnormal components into the normal range before the magnitudes are taken. This costs no extra reduction.
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.
When the unscaled sum of a p-norm underflows,
generic_normfalls back to dividing every magnitude by the largest one. For complex elements whose components are subnormal,norm(x)is already rounded to the subnormal grid at that point, so most of its bits are gone before the scaling can help.normoffill(complex(u, u), 4)withu = nextfloat(0f0)returns2u, while the exact value is2√2·u ≈ 2.83u, which rounds to3u. The error is not limited to subnormal results: 2^20 copies ofcomplex(M/1024, M/1024)next to oneM = floatmin(Float32)give a normal norm that is off by 198 ulp, and a million copies ofcomplex(u, u)are off by 29% (414 ulp) in both Float32 and Float64. Real inputs are not affected. This came up while reviewing JuliaGPU/CUDA.jl#3633, which routes CUDA'snormto this implementation instead of cuBLASnrm2.A finite sum that fails the acceptance threshold is below 1, so every component is too. Multiplying by
1/epsis then exact and moves subnormal components into the normal range before the magnitudes are taken. The maximum is taken over the same scaled magnitudes, so the largest ratio is still exactly 1 for largep, and the result is divided by the factor at the end. This adds no reduction. An overflowing sum keeps the old path. LAPACK'sdznrm2and LinearAlgebra's newergeneric_norm2for numeric arrays also scale the components rather than the magnitudes. The CPUnormin Julia 1.13 still rounds the magnitudes first, as does OpenBLASnrm2for small inputs, so the new test only runs on GPU arrays and callsGPUArrays.generic_normdirectly, because back-ends may sendnormto a vendor library.With the fix, the 2-norm on the inputs above, and on random subnormal and normal complex data, is within 0.5 ulp of the correctly rounded result on JLArray and on an RTX 5080. The
linalg/normtestsuite passes for JLArray and Array, and for CuArray oncenormis routed togeneric_norm. The new tests fail on master (8 of 8).