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,