Skip to content

Commit 29dc1a7

Browse files
author
Katharine Hyatt
committed
Add GPU-safe index selection for eig/eigh pullbacks
1 parent 2bf6038 commit 29dc1a7

4 files changed

Lines changed: 30 additions & 18 deletions

File tree

‎ext/MatrixAlgebraKitCUDAExt/MatrixAlgebraKitCUDAExt.jl‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,6 @@ function svd_pullback!(ΔA::AnyCuMatrix, A, USVᴴ, ΔUSVᴴ, ind::AnyCuVector;
205205
return svd_pullback!(ΔA, A, USVᴴ, ΔUSVᴴ, collect(ind); kwargs...)
206206
end
207207

208-
function eigh_pullback!(ΔA::AnyCuMatrix, A, DV, ΔDV, ind::AnyCuVector; kwargs...)
209-
return eigh_pullback!(ΔA, A, DV, ΔDV, collect(ind); kwargs...)
210-
end
211-
212-
function eig_pullback!(ΔA::AnyCuMatrix, A, DV, ΔDV, ind::AnyCuVector; kwargs...)
213-
return eig_pullback!(ΔA, A, DV, ΔDV, collect(ind); kwargs...)
214-
end
215-
216208
# have to override this as methods are missing in GPUArrays for the various
217209
# views of Diagonal of ΔA
218210
function svd_pushforward!(

‎src/common/pullbacks.jl‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,25 @@ iszerotangent(::Nothing) = true
1313

1414
# fallback
1515
_sylvester(A, B, C) = LinearAlgebra.sylvester(A, B, C)
16+
17+
"""
18+
select_indices(r::AbstractRange, ind)
19+
20+
Compute `r[ind]` without iterating over `ind`, so that this also works for an `ind` that
21+
lives on a device.
22+
"""
23+
select_indices(r::AbstractRange, ind) = r[ind]
24+
select_indices(r::AbstractRange, ind::AbstractRange{<:Integer}) = r[ind]
25+
function select_indices(r::AbstractRange, ind::AbstractVector{<:Integer})
26+
checkbounds(r, ind)
27+
return first(r) .+ step(r) .* (ind .- 1)
28+
end
29+
30+
"""
31+
is_leading_index(ind, p::Int)
32+
33+
Check whether `ind` selects the first `p` values in order, i.e. whether `ind == 1:p`, without
34+
iterating over `ind`, so that this also works for an `ind` that lives on a device.
35+
"""
36+
is_leading_index(ind::AbstractRange, p::Int) = ind == 1:p
37+
is_leading_index(ind::AbstractVector, p::Int) = length(ind) == p && all(ind .== 1:p)

‎src/pullbacks/eig.jl‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function check_and_prepare_eig_cotangents(
55
)
66

77
n, p = size(V)
8-
indD = axes(D, 1)[ind]
9-
indV = axes(V, 2)[ind]
8+
indD = select_indices(axes(D, 1), ind)
9+
indV = select_indices(axes(V, 2), ind)
1010
if !iszerotangent(ΔV)
1111
n == size(ΔV, 1) || throw(DimensionMismatch())
1212
length(indV) == size(ΔV, 2) || throw(DimensionMismatch())
13-
if indV == 1:p
13+
if is_leading_index(indV, p)
1414
ΔV₁ = copy(ΔV)
1515
else
1616
ΔV₁ = zero(V)
@@ -41,8 +41,7 @@ function check_and_prepare_eig_cotangents(
4141
if !iszerotangent(ΔDmat)
4242
ΔD = diagview(ΔDmat)
4343
length(indD) == length(ΔD) || throw(DimensionMismatch())
44-
# needed to avoid GPUCompiler errors
45-
VᴴAΔV[diagind(VᴴAΔV)[indD]] .+= ΔD
44+
VᴴAΔV[select_indices(diagind(VᴴAΔV), indD)] .+= ΔD
4645
else
4746
ΔD = nothing
4847
end

‎src/pullbacks/eigh.jl‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ function check_and_prepare_eigh_cotangents(
55
)
66

77
n, p = size(V)
8-
indD = axes(D, 1)[ind]
9-
indV = axes(V, 2)[ind]
8+
indD = select_indices(axes(D, 1), ind)
9+
indV = select_indices(axes(V, 2), ind)
1010
if !iszerotangent(ΔV)
1111
n == size(ΔV, 1) || throw(DimensionMismatch())
1212
length(indV) == size(ΔV, 2) || throw(DimensionMismatch())
13-
if indV == 1:p
13+
if is_leading_index(indV, p)
1414
ΔV₁ = copy(ΔV)
1515
else
1616
ΔV₁ = zero(V)
@@ -42,8 +42,7 @@ function check_and_prepare_eigh_cotangents(
4242
if !iszerotangent(ΔDmat)
4343
ΔD = diagview(ΔDmat)
4444
length(indD) == length(ΔD) || throw(DimensionMismatch())
45-
# needed to avoid GPUCompiler errors
46-
VᴴAΔV[diagind(VᴴAΔV)[indD]] .+= real.(ΔD)
45+
VᴴAΔV[select_indices(diagind(VᴴAΔV), indD)] .+= real.(ΔD)
4746
else
4847
ΔD = nothing
4948
end

0 commit comments

Comments
 (0)