|
| 1 | +using Adapt: adapt |
| 2 | +using JLArrays: JLArray, JLMatrix |
| 3 | +using LinearAlgebra: LinearAlgebra, Hermitian, Symmetric, qr, eigen |
| 4 | +using NDTensors: NDTensors |
| 5 | +using NDTensors.Expose: Expose, expose, qr, qr_positive, ql, ql_positive |
| 6 | +using NDTensors.GPUArraysCoreExtensions: cpu |
| 7 | +using NDTensors.TypeParameterAccessors: unwrap_array_type |
| 8 | + |
| 9 | +## TODO this function exists because of the same issue below. when |
| 10 | +## that issue is resolved we can rely on the abstractarray version of |
| 11 | +## this operation. |
| 12 | +function Expose.qr(A::Exposed{<:JLArray}) |
| 13 | + Q, L = qr(unexpose(A)) |
| 14 | + return adapt(unwrap_array_type(A), Matrix(Q)), adapt(unwrap_array_type(A), L) |
| 15 | +end |
| 16 | +## TODO this should work using a JLArray but there is an error converting the Q from its packed QR from |
| 17 | +## back into a JLArray see https://github.com/JuliaGPU/GPUArrays.jl/issues/545. To fix call cpu for now |
| 18 | +function Expose.qr_positive(A::Exposed{<:JLArray}) |
| 19 | + Q, L = qr_positive(expose(cpu(A))) |
| 20 | + return adapt(unwrap_array_type(A), copy(Q)), adapt(unwrap_array_type(A), L) |
| 21 | +end |
| 22 | + |
| 23 | +function Expose.ql(A::Exposed{<:JLMatrix}) |
| 24 | + Q, L = ql(expose(cpu(A))) |
| 25 | + return adapt(unwrap_array_type(A), copy(Q)), adapt(unwrap_array_type(A), L) |
| 26 | +end |
| 27 | +function Expose.ql_positive(A::Exposed{<:JLMatrix}) |
| 28 | + Q, L = ql_positive(expose(cpu(A))) |
| 29 | + return adapt(unwrap_array_type(A), copy(Q)), adapt(unwrap_array_type(A), L) |
| 30 | +end |
| 31 | + |
| 32 | +function LinearAlgebra.eigen(A::Exposed{<:JLMatrix,<:Symmetric}) |
| 33 | + q, l = (eigen(expose(cpu(A)))) |
| 34 | + return adapt.(unwrap_array_type(A), (q, l)) |
| 35 | +end |
| 36 | + |
| 37 | +function LinearAlgebra.eigen(A::Exposed{<:JLMatrix,<:Hermitian}) |
| 38 | + q, l = (eigen(expose(Hermitian(cpu(unexpose(A).data))))) |
| 39 | + return adapt.(JLArray, (q, l)) |
| 40 | +end |
0 commit comments