Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast back svd/eigen of symmetric Float16 matrices to Float16 #54403

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,11 @@ function svd(A::RealHermSymComplexHerm; full::Bool=false)
end
return SVD(vecs, vals, V')
end
function svd(A::RealHermSymComplexHerm{Float16}; full::Bool = false)
T = eltype(A)
F = svd(eigencopy_oftype(A, eigtype(T)); full)
return SVD{T}(F)
end

function svdvals!(A::RealHermSymComplexHerm)
vals = eigvals!(A)
Expand Down
19 changes: 17 additions & 2 deletions stdlib/LinearAlgebra/src/symmetriceigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ function eigen(A::RealHermSymComplexHerm; sortby::Union{Function,Nothing}=nothin
S = eigtype(eltype(A))
eigen!(eigencopy_oftype(A, S), sortby=sortby)
end
function eigen(A::RealHermSymComplexHerm{Float16}; sortby::Union{Function,Nothing}=nothing)
S = eigtype(eltype(A))
E = eigen!(eigencopy_oftype(A, S), sortby=sortby)
values = convert(AbstractVector{Float16}, E.values)
vectors = convert(AbstractMatrix{isreal(E.vectors) ? Float16 : Complex{Float16}}, E.vectors)
return Eigen(values, vectors)
end

eigen!(A::RealHermSymComplexHerm{<:BlasReal,<:StridedMatrix}, irange::UnitRange) =
Eigen(LAPACK.syevr!('V', 'I', A.uplo, A.data, 0.0, 0.0, irange.start, irange.stop, -1.0)...)
Expand Down Expand Up @@ -295,8 +302,16 @@ function eigvals!(A::StridedMatrix{T}, F::LU{T,<:StridedMatrix}; sortby::Union{F
return eigvals!(A; sortby)
end


function eigen(A::Hermitian{Complex{T}, <:Tridiagonal}; kwargs...) where {T}
eigen(A::Hermitian{<:Complex, <:Tridiagonal}; kwargs...) =
_eigenhermtridiag(A; kwargs...)
# disambiguation
function eigen(A::Hermitian{Complex{Float16}, <:Tridiagonal}; kwargs...)
E = _eigenhermtridiag(A; kwargs...)
values = convert(AbstractVector{Float16}, E.values)
vectors = convert(AbstractMatrix{ComplexF16}, E.vectors)
return Eigen(values, vectors)
end
function _eigenhermtridiag(A::Hermitian{<:Complex,<:Tridiagonal}; kwargs...)
(; dl, d, du) = parent(A)
N = length(d)
if N <= 1
Expand Down
21 changes: 21 additions & 0 deletions stdlib/LinearAlgebra/test/svd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,27 @@ end
@test B.U ≈ B32.U
@test B.Vt ≈ B32.Vt
@test B.S ≈ B32.S
C = Symmetric(A'A)
D = svd(C)
D32 = svd(Symmetric(Float32.(C)))
@test D isa SVD{Float16, Float16, Matrix{Float16}}
@test D.U isa Matrix{Float16}
@test D.Vt isa Matrix{Float16}
@test D.S isa Vector{Float16}
@test D.U ≈ D32.U
@test D.Vt ≈ D32.Vt
@test D.S ≈ D32.S
A = randn(ComplexF16, 3, 3)
E = Hermitian(A'A)
F = svd(E)
F32 = svd(Hermitian(ComplexF32.(E)))
@test F isa SVD{ComplexF16, Float16, Matrix{ComplexF16}, Vector{Float16}}
@test F.U isa Matrix{ComplexF16}
@test F.Vt isa Matrix{ComplexF16}
@test F.S isa Vector{Float16}
@test F.U ≈ F32.U
@test F.Vt ≈ F32.Vt
@test F.S ≈ F32.S
end

end # module TestSVD
22 changes: 22 additions & 0 deletions stdlib/LinearAlgebra/test/symmetriceigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ end
HT = Hermitian(Tridiagonal(ev, dv, ev))
λ, V = eigen(HT)
@test HT * V ≈ V * Diagonal(λ)
HT = Hermitian(Tridiagonal(ComplexF16.(ev), ComplexF16.(dv), ComplexF16.(ev)))
F = eigen(HT)
@test F isa Eigen{ComplexF16, Float16, Matrix{ComplexF16}, Vector{Float16}}
λ, V = F
@test HT * V ≈ V * Diagonal(λ)
end

@testset "Float16" begin
A = rand(Float16, 3, 3)
A = Symmetric(A*A')
B = eigen(A)
B32 = eigen(Symmetric(Float32.(A)))
@test B isa Eigen{Float16, Float16, Matrix{Float16}, Vector{Float16}}
@test B.values ≈ B32.values
@test B.vectors ≈ B32.vectors
C = randn(ComplexF16, 3, 3)
C = Hermitian(C*C')
D = eigen(C)
D32 = eigen(Hermitian(ComplexF32.(C)))
@test D isa Eigen{ComplexF16, Float16, Matrix{ComplexF16}, Vector{Float16}}
@test D.values ≈ D32.values
@test D.vectors ≈ D32.vectors
end

end # module TestSymmetricEigen