JuliaGPU / GPUArrays.jl

Reusable array functionality for Julia's various GPU backends.
MIT License
313 stars 74 forks source link

Support for qr factorizations #545

Open kmp5VT opened 6 days ago

kmp5VT commented 6 days ago

Hello,

I am trying to convert the output packed Q output of the QR decomposition of a JLArray back into a JLArray but there is a constructor error being thrown.

 julia> using JLArrays, LinearAlgebra
 julia> A = JLArray(randn(8,4))
 julia> Q,R = qr(A)
 julia> convert(typeof(A), Q)
 ERROR: MethodError: no method matching JLArray{Float64, 2}(::LinearAlgebra.QRCompactWYQ{Float64, JLArray{Float64, 2}, 
 JLArray{Float64, 2}})

 Closest candidates are:
  JLArray{T, N}(::UndefInitializer, ::Tuple{Vararg{Int64, N}}) where {T, N}
   @ JLArrays ~/.julia/packages/JLArrays/hD5YX/src/JLArrays.jl:168
  JLArray{T, N}(::UndefInitializer, ::Tuple{Vararg{Integer, N}}) where {T, N}
   @ JLArrays ~/.julia/packages/JLArrays/hD5YX/src/JLArrays.jl:207
  JLArray{T, N}(::UndefInitializer, Integer...) where {T, N}
   @ JLArrays ~/.julia/packages/JLArrays/hD5YX/src/JLArrays.jl:209
  ...

Stacktrace:
 [1] convert(::Type{JLArray{Float64, 2}}, Q::LinearAlgebra.QRCompactWYQ{Float64, JLArray{Float64, 2}, JLArray{Float64, 2}})
   @ LinearAlgebra ~/.julia/juliaup/julia-1.10.4+0.aarch64.apple.darwin14/share/julia/stdlib/v1.10/LinearAlgebra/src/abstractq.jl:49
 [2] top-level scope
   @ ~/.julia/dev/testing.jl:187
maleadt commented 6 days ago

GPUArrays.jl does not support factorizations. It's interesting that qr(A) doesn't error straight away.

This may be easy to fix, since it seems like the factorization was computed correctly, but I'm not familiar enough to be sure. In any case, CUDA.jl carries quite a bit of code for QR factorizations: https://github.com/JuliaGPU/CUDA.jl/blob/14de0097ff7c26932cc4a175840961cc7d3f396e/lib/cusolver/linalg.jl#L141-L237. Quite a bit of those rely on CUSOLVER APIs, so I'm not sure it's possible to add this here.

kmp5VT commented 6 days ago

Yeah it actually has no issue running the QR and gets the right answer

julia> A = JLArray(randn(5,5))
5×5 JLArray{Float64, 2}:
 -1.72275   -1.71712   -0.633798   0.177743   -1.08403
  0.478877   0.395843  -1.02957    0.645991   -1.37382
 -0.410473   0.871103   0.245303   0.538019   -1.31581
 -0.190147  -1.46851   -0.183525  -0.216322    0.793294
 -0.626786  -2.15311    0.591278   0.0816701   1.23045

julia> Q,R = qr(A)
LinearAlgebra.QRCompactWY{Float64, JLArray{Float64, 2}, JLArray{Float64, 2}}
Q factor: 5×5 LinearAlgebra.QRCompactWYQ{Float64, JLArray{Float64, 2}, JLArray{Float64, 2}}
R factor:
5×5 JLArray{Float64, 2}:
 1.948  2.26845   0.0833889  -0.116918   0.42488
 0.0    2.34746  -0.127072    0.351965  -1.96242
 0.0    0.0       1.37189    -0.366869   1.51315
 0.0    0.0       0.0         0.720891  -0.781938
 0.0    0.0       0.0         0.0       -0.067345

julia> Q * R ≈ A
true

I can look into this issue though. Thanks!