-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMatrixAlgebraKitAMDGPUExt.jl
More file actions
190 lines (167 loc) · 7.2 KB
/
MatrixAlgebraKitAMDGPUExt.jl
File metadata and controls
190 lines (167 loc) · 7.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
module MatrixAlgebraKitAMDGPUExt
using MatrixAlgebraKit
using MatrixAlgebraKit: @algdef, Algorithm, check_input
using MatrixAlgebraKit: one!, zero!, uppertriangular!, lowertriangular!
using MatrixAlgebraKit: diagview, sign_safe
using MatrixAlgebraKit: ROCSOLVER, LQViaTransposedQR, TruncationStrategy, NoTruncation, TruncationByValue, AbstractAlgorithm
using MatrixAlgebraKit: default_qr_algorithm, default_lq_algorithm, default_svd_algorithm, default_eigh_algorithm
import MatrixAlgebraKit: geqrf!, ungqr!, unmqr!, gesvd!, gesvdj!
import MatrixAlgebraKit: heevj!, heevd!, heev!, heevx!
import MatrixAlgebraKit: _sylvester, svd_rank
using AMDGPU
using LinearAlgebra
using LinearAlgebra: BlasFloat
include("yarocsolver.jl")
MatrixAlgebraKit.default_driver(::Type{TA}) where {TA <: StridedROCVecOrMat{<:BlasFloat}} = ROCSOLVER()
function MatrixAlgebraKit.default_svd_algorithm(::Type{T}; kwargs...) where {T <: StridedROCVecOrMat{<:BlasFloat}}
return QRIteration(; kwargs...)
end
function MatrixAlgebraKit.default_eigh_algorithm(::Type{T}; kwargs...) where {T <: StridedROCVecOrMat{<:BlasFloat}}
return DivideAndConquer(; kwargs...)
end
for f in (:geqrf!, :ungqr!, :unmqr!)
@eval $f(::ROCSOLVER, args...) = YArocSOLVER.$f(args...)
end
MatrixAlgebraKit.supports_svd_full(::ROCSOLVER, f::Symbol) = f in (:qr_iteration, :jacobi)
function gesvd!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
m, n = size(A)
m >= n && return YArocSOLVER.gesvd!(A, S, U, Vᴴ)
return MatrixAlgebraKit.svd_via_adjoint!(gesvd!, ROCSOLVER(), A, S, U, Vᴴ; kwargs...)
end
function gesvdj!(::ROCSOLVER, A::StridedROCMatrix, S::StridedROCVector, U::StridedROCMatrix, Vᴴ::StridedROCMatrix; kwargs...)
m, n = size(A)
m >= n && return YArocSOLVER.gesvdj!(A, S, U, Vᴴ; kwargs...)
return MatrixAlgebraKit.svd_via_adjoint!(gesvdj!, ROCSOLVER(), A, S, U, Vᴴ; kwargs...)
end
heevj!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =
YArocSOLVER.heevj!(A, Dd, V; kwargs...)
heevd!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =
YArocSOLVER.heevd!(A, Dd, V; kwargs...)
heev!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =
YArocSOLVER.heev!(A, Dd, V; kwargs...)
heevx!(::ROCSOLVER, A::StridedROCMatrix, Dd::StridedROCVector, V::StridedROCMatrix; kwargs...) =
YArocSOLVER.heevx!(A, Dd, V; kwargs...)
function MatrixAlgebraKit.findtruncated_svd(values::StridedROCVector, strategy::TruncationByValue)
return MatrixAlgebraKit.findtruncated(values, strategy)
end
# COV_EXCL_START
function _project_hermitian_offdiag_kernel(Au, Al, Bu, Bl, ::Val{true})
m, n = size(Au)
j = workitemIdx().x + (workgroupIdx().x - 1) * workgroupDim().x
j > n && return
for i in 1:m
@inbounds begin
val = (Au[i, j] - adjoint(Al[j, i])) / 2
Bu[i, j] = val
Bl[j, i] = -adjoint(val)
end
end
return
end
function _project_hermitian_offdiag_kernel(Au, Al, Bu, Bl, ::Val{false})
m, n = size(Au)
j = workitemIdx().x + (workgroupIdx().x - 1) * workgroupDim().x
j > n && return
for i in 1:m
@inbounds begin
val = (Au[i, j] + adjoint(Al[j, i])) / 2
Bu[i, j] = val
Bl[j, i] = adjoint(val)
end
end
return
end
function _project_hermitian_diag_kernel(A, B, ::Val{true})
n = size(A, 1)
j = workitemIdx().x + (workgroupIdx().x - 1) * workgroupDim().x
j > n && return
@inbounds begin
for i in 1:(j - 1)
val = (A[i, j] - adjoint(A[j, i])) / 2
B[i, j] = val
B[j, i] = -adjoint(val)
end
B[j, j] = MatrixAlgebraKit._imimag(A[j, j])
end
return
end
function _project_hermitian_diag_kernel(A, B, ::Val{false})
n = size(A, 1)
j = workitemIdx().x + (workgroupIdx().x - 1) * workgroupDim().x
j > n && return
@inbounds begin
for i in 1:(j - 1)
val = (A[i, j] + adjoint(A[j, i])) / 2
B[i, j] = val
B[j, i] = adjoint(val)
end
B[j, j] = real(A[j, j])
end
return
end
# COV_EXCL_STOP
const SupportedROCMatrix{T} = Union{AnyROCMatrix{T}, SubArray{T, 2, <:AnyROCMatrix{T}}}
function MatrixAlgebraKit._project_hermitian_offdiag!(
Au::SupportedROCMatrix, Al::SupportedROCMatrix, Bu::SupportedROCMatrix, Bl::SupportedROCMatrix, ::Val{anti}
) where {anti}
thread_dim = 512
block_dim = cld(size(Au, 2), thread_dim)
@roc groupsize = thread_dim gridsize = block_dim _project_hermitian_offdiag_kernel(Au, Al, Bu, Bl, Val(anti))
return nothing
end
function MatrixAlgebraKit._project_hermitian_diag!(A::SupportedROCMatrix, B::SupportedROCMatrix, ::Val{anti}) where {anti}
thread_dim = 512
block_dim = cld(size(A, 1), thread_dim)
@roc groupsize = thread_dim gridsize = block_dim _project_hermitian_diag_kernel(A, B, Val(anti))
return nothing
end
# avoids calling the `StridedMatrix` specialization to avoid scalar indexing,
# use (allocating) fallback instead until we write a dedicated kernel
MatrixAlgebraKit.ishermitian_exact(A::StridedROCMatrix) = A == A'
MatrixAlgebraKit.ishermitian_approx(A::StridedROCMatrix; atol, rtol, kwargs...) =
norm(project_antihermitian(A; kwargs...)) ≤ max(atol, rtol * norm(A))
MatrixAlgebraKit.isantihermitian_exact(A::StridedROCMatrix) = A == -A'
MatrixAlgebraKit.isantihermitian_approx(A::StridedROCMatrix; atol, rtol, kwargs...) =
norm(project_hermitian(A; kwargs...)) ≤ max(atol, rtol * norm(A))
function MatrixAlgebraKit._avgdiff!(A::StridedROCMatrix, B::StridedROCMatrix)
axes(A) == axes(B) || throw(DimensionMismatch())
# COV_EXCL_START
function _avgdiff_kernel(A, B)
j = workitemIdx().x + (workgroupIdx().x - 1) * workgroupDim().x
j > length(A) && return
@inbounds begin
a = A[j]
b = B[j]
A[j] = (a + b) / 2
B[j] = b - a
end
return
end
# COV_EXCL_STOP
thread_dim = 512
block_dim = cld(length(A), thread_dim)
@roc groupsize = thread_dim gridsize = block_dim _avgdiff_kernel(A, B)
return A, B
end
# avoids calling the BlasMat specialization that assumes syrk! or herk! is called
# TODO: remove once syrk! or herk! is defined
function MatrixAlgebraKit._mul_herm!(C::StridedROCMatrix{T}, A::StridedROCMatrix{T}) where {T <: BlasFloat}
mul!(C, A, A')
project_hermitian!(C)
return C
end
# TODO: intersect/union don't work on GPU
MatrixAlgebraKit._ind_intersect(A::ROCVector{Int}, B::ROCVector{Int}) =
MatrixAlgebraKit._ind_intersect(collect(A), collect(B))
MatrixAlgebraKit._ind_union(A::AbstractVector{<:Integer}, B::ROCVector{Int}) =
MatrixAlgebraKit._ind_union(A, collect(B))
MatrixAlgebraKit._ind_union(A::ROCVector{Int}, B::AbstractVector{<:Integer}) =
MatrixAlgebraKit._ind_union(collect(A), B)
MatrixAlgebraKit._ind_union(A::ROCVector{Int}, B::ROCVector{Int}) =
MatrixAlgebraKit._ind_union(collect(A), collect(B))
function _sylvester(A::AnyROCMatrix, B::AnyROCMatrix, C::AnyROCMatrix)
hX = sylvester(collect(A), collect(B), collect(C))
return ROCArray(hX)
end
svd_rank(S::AnyROCVector, rank_atol) = findlast(s -> s ≥ rank_atol, S)
end