Skip to content

Scale complex elements before taking magnitudes in the rescaled p-norm - #867

Merged
maleadt merged 1 commit into
mainfrom
tb/complex-norm-subnormal
Nov 27, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/complex-norm-subnormal

Conversation

@maleadt

@maleadt maleadt commented Nov 25, 2026

Copy link
Copy Markdown
Member

When the unscaled sum of a p-norm underflows, generic_norm falls 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. norm of fill(complex(u, u), 4) with u = nextfloat(0f0) returns 2u, while the exact value is 2√2·u ≈ 2.83u, which rounds to 3u. The error is not limited to subnormal results: 2^20 copies of complex(M/1024, M/1024) next to one M = floatmin(Float32) give a normal norm that is off by 198 ulp, and a million copies of complex(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's norm to this implementation instead of cuBLAS nrm2.

A finite sum that fails the acceptance threshold is below 1, so every component is too. Multiplying by 1/eps is 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 large p, and the result is divided by the factor at the end. This adds no reduction. An overflowing sum keeps the old path. LAPACK's dznrm2 and LinearAlgebra's newer generic_norm2 for numeric arrays also scale the components rather than the magnitudes. The CPU norm in Julia 1.13 still rounds the magnitudes first, as does OpenBLAS nrm2 for small inputs, so the new test only runs on GPU arrays and calls GPUArrays.generic_norm directly, because back-ends may send norm to 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/norm testsuite passes for JLArray and Array, and for CuArray once norm is routed to generic_norm. The new tests fail on master (8 of 8).

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.
@maleadt
maleadt merged commit a5eacb3 into main Nov 27, 2026
8 of 9 checks passed
@maleadt
maleadt deleted the tb/complex-norm-subnormal branch November 27, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant