From 7811e5ecee52b374a9234af16d3688fb2fce11fb Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 25 Nov 2026 17:21:44 +0100 Subject: [PATCH] Scale complex elements before taking magnitudes in the rescaled p-norm 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. --- src/host/linalg.jl | 14 ++++++++++---- test/testsuite/linalg.jl | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/host/linalg.jl b/src/host/linalg.jl index c62a6c675..4373f6550 100644 --- a/src/host/linalg.jl +++ b/src/host/linalg.jl @@ -1278,15 +1278,21 @@ function generic_norm(v::AbstractArray{T}, p::Real) where {T} s = _sum_pow(promote_, v, p, spp, init) isfinite(s) && s >= length(v) * (floatmin(sum_type) / eps(sum_type)) && return convert(result_type, p == 2 ? sqrt(s) : s^inv(spp)) + # A finite sum below that threshold is below 1, so every component is too. Scaling + # by 1/eps is then exact and lifts subnormal components (where the back-end keeps + # them) into the normal range before taking magnitudes, which would otherwise round + # complex ones to the subnormal grid. + f = isfinite(s) ? inv(eps(sum_type)) : one(sum_type) + mag(x) = norm(promote_(x) * f) # take the maximum of the same magnitudes that get scaled, so the largest ratio is # exactly 1 even for large p. Divide rather than multiply by inv(scale), which # overflows for subnormal scales. - scale = maximum(x -> norm(promote_(x)), v) + scale = maximum(mag, v) if iszero(scale) || isinf(scale) - return convert(result_type, scale) + return convert(result_type, scale / f) end - s = _sum_pow(x -> norm(promote_(x)) / scale, v, p, spp, init) - scale * (p == 2 ? sqrt(s) : s^inv(spp)) + s = _sum_pow(x -> mag(x) / scale, v, p, spp, init) + scale * (p == 2 ? sqrt(s) : s^inv(spp)) / f elseif p > 1 || p < -1 # May need rescaling # Rescaling heuristic similar to Base, see LinearAlgebra/src/generic.jl infnorm = p > 1 ? maximum(norm, v) : minimum(norm, v) diff --git a/test/testsuite/linalg.jl b/test/testsuite/linalg.jl index 402026dd3..0cff5223c 100644 --- a/test/testsuite/linalg.jl +++ b/test/testsuite/linalg.jl @@ -1025,6 +1025,29 @@ end end end end + @testset "subnormal components x $T" for T in filter(T -> T <: Complex{<:AbstractFloat}, valid_eltypes) + # the magnitude of such an element keeps few bits unless it is scaled up first. + # Call the generic norm directly, since back-ends may dispatch `norm` to a library. + R = real(T) + u = nextfloat(zero(R)) + M = floatmin(R) + # some back-ends flush subnormal inputs to zero; widen like the norm does. On the + # CPU, `sum` adds the identical terms below sequentially, losing more than rtol. + S = promote_type(Float32, R) + if AT <: AbstractGPUArray && !iszero(Array(AT(fill(u, 1)) .* inv(eps(S)))[1]) + @testset "p=$p" for p in (1.5, 2) + # correctly rounded: 3u for p=2, where rounding magnitudes first gives 2u + exact = (4 * (sqrt(big(2)) * u)^p)^inv(big(p)) + @test GPUArrays.generic_norm(AT(fill(T(u, u), 4)), p) == R(exact) + # a normal norm, from many subnormal elements next to a normal one + n = 2^16 + a = 7M / 2048 + arr = [T(M); fill(T(a, a), n)] + exact = (big(M)^p + n * (sqrt(big(2)) * a)^p)^inv(big(p)) + @test GPUArrays.generic_norm(AT(arr), p) ≈ exact rtol=8eps(R) + end + end + end @testset "large p x $T" for T in filter(T -> T <: Complex{<:AbstractFloat}, valid_eltypes) # |z / norm(z)| rounds away from 1 for these, which a large p blows up @testset "$z" for z in (0.77018034f0 + 0.78051925f0im,