diff --git a/AGENTS.md b/AGENTS.md index 5d55f69c8..840497c45 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1263,3 +1263,19 @@ this section verbatim). Each entry is one bullet starting with `- `. production transport defaults must stay unchanged. Every test worker addition must use the same setting: both transports reject mixing lazy and eager workers in one cluster. + +114. **LU singularity is an exact zero-pivot condition, not an epsilon test.** + The fallback pivot search marked a nonzero column maximum at or below + `eps(T)` as singular, rejecting an invertible matrix merely because it + was scaled down. The CPU LAPACK panel path already uses exact zeros and + therefore hides this error in ordinary CPU factorization tests. Exercise + the fallback search directly with tiny nonzero and true-zero pivots, + including off-diagonal candidates and later panels; retain the first + global zero column once it has been recorded. + +115. **A version fallback must preserve Dagger's keyword contract.** + Julia 1.9/1.10 lack LinearAlgebra's `_check_lu_success`, but replacing it + with unconditional `checknonsingular` also discards `allowsingular=true`. + Honor that flag before raising a positive pivot error, and retain the + global singular-column metadata. Check the public distributed LU call + on the older runtime: modern-runtime tests only exercise the other branch. diff --git a/src/array/lu.jl b/src/array/lu.jl index 3c0cc0f0a..0933cb1ad 100644 --- a/src/array/lu.jl +++ b/src/array/lu.jl @@ -88,8 +88,7 @@ function search_and_update_ipiv!(ipiv_chunk::AbstractVector{Int}, info::Ref{Int} end end - Tf = real(float(T)) - if info[] == 0 && best_mag <= eps(Tf) + if info[] == 0 && iszero(best_mag) info[] = (k-1)*mb + p end @@ -377,7 +376,7 @@ function LinearAlgebra.lu!(A::DMatrix{T}, ::LinearAlgebra.RowMaximum; check::Boo @static if VERSION >= v"1.11-" LinearAlgebra._check_lu_success(panel_info, allowsingular) else - LinearAlgebra.checknonsingular(panel_info) + allowsingular || LinearAlgebra.checknonsingular(panel_info) end end return panel_info diff --git a/test/array/linalg/lu.jl b/test/array/linalg/lu.jl index 0de5bab03..b0fc84925 100644 --- a/test/array/linalg/lu.jl +++ b/test/array/linalg/lu.jl @@ -13,6 +13,48 @@ function factorization_rtol(T, pivot) end end +@testset "Fallback LU pivot singularity" begin + for T in (Float32, Float64, ComplexF32, ComplexF64) + for tiny in (T(eps(real(T)) / 100), T(eps(real(T)))) + # Exercise the non-LAPACK search directly: a CPU compute scope + # normally selects the panel fast path and hides this fallback. + for off_diagonal in (false, true) + info = Ref(0) + ipiv = zeros(Int, 2) + diagonal = T[tiny 0; 0 1] + if off_diagonal + diagonal[1, 1] = 0 + off = T[tiny 0; 0 1] + Dagger.search_and_update_ipiv!(ipiv, info, diagonal, 1, 1, 2, 4, 1, off) + @test ipiv[1] == 3 + else + Dagger.search_and_update_ipiv!(ipiv, info, diagonal, 1, 1, 2, 2, 0) + @test ipiv[1] == 1 + end + @test info[] == 0 + end + end + + # A true zero records its global column once, including later panels. + for (k, p, initial, expected) in ((1, 1, 0, 1), (1, 2, 0, 2), + (2, 1, 0, 3), (2, 2, 0, 4), (2, 1, 1, 1)) + info = Ref(initial) + ipiv = zeros(Int, 2) + Dagger.search_and_update_ipiv!(ipiv, info, zeros(T, 2, 2), k, p, 2, 4, 0) + @test info[] == expected + end + end +end + +@testset "Pivoted LU allowsingular across Julia versions" begin + for T in (Float32, Float64, ComplexF32, ComplexF64) + matrix = Matrix(Diagonal(T[1, 0, 2, 0])) + @test_throws LinearAlgebra.SingularException lu(distribute(matrix, Blocks(2, 2)), RowMaximum()) + @test lu(distribute(matrix, Blocks(2, 2)), RowMaximum(); check=false).info == 2 + @test lu(distribute(matrix, Blocks(2, 2)), RowMaximum(); allowsingular=true).info == 2 + end +end + function test_lu_pivot_blocks(::Type{T}, part, pivot) where T A = Matrix{T}(I, 13, 13) + rand(T, 13, 13) / 20 F = lu(distribute(A, part), pivot)