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

Adds some missing unthunks #282

Merged
merged 6 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion src/rulesets/LinearAlgebra/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ using LinearAlgebra.BLAS: gemv, gemv!, gemm!, trsm!, axpy!, ger!
function rrule(::typeof(svd), X::AbstractMatrix{<:Real})
F = svd(X)
function svd_pullback(Ȳ::Composite)
∂X = svd_rev(F, Ȳ.U, Ȳ.S, Ȳ.V)
# svd_rev does a lot of linear algebra, it is is efficient to unthunk before
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
∂X = svd_rev(F, unthunk(Ȳ.U), unthunk(Ȳ.S), unthunk(Ȳ.V))
return (NO_FIELDS, ∂X)
end
return F, svd_pullback
Expand Down
3 changes: 3 additions & 0 deletions src/rulesets/LinearAlgebra/structured.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ end
#####

function rrule(::Type{<:Diagonal}, d::AbstractVector)
function Diagonal_pullback(x::AbstractThunk)
return Diagonal_pullback(unthunk(x))
end
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
function Diagonal_pullback(ȳ::AbstractMatrix)
mzgubic marked this conversation as resolved.
Show resolved Hide resolved
return (NO_FIELDS, diag(ȳ))
end
Expand Down
24 changes: 22 additions & 2 deletions test/rulesets/LinearAlgebra/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ using ChainRules: level2partition, level3partition, chol_blocked_rev, chol_unblo
@test dself1 === NO_FIELDS
@test dp === DoesNotExist()

ΔF = unthunk(dF)
dself2, dX = dX_pullback(ΔF)
dself2, dX = dX_pullback(dF)
@test dself2 === NO_FIELDS
X̄_ad = unthunk(dX)
X̄_fd = only(j′vp(central_fdm(5, 1), X->getproperty(svd(X), p), Ȳ, X))
@test all(isapprox.(X̄_ad, X̄_fd; rtol=1e-6, atol=1e-6))

oxinabox marked this conversation as resolved.
Show resolved Hide resolved
end
@testset "Vt" begin
Y, dF_pullback = rrule(getproperty, F, :Vt)
Expand All @@ -27,6 +27,26 @@ using ChainRules: level2partition, level3partition, chol_blocked_rev, chol_unblo
end
end

@testset "Thunked inputs" begin
X = randn(4, 3)
F, dX_pullback = rrule(svd, X)
for p in [:U, :S, :V]
Y, dF_pullback = rrule(getproperty, F, p)
Ȳ = randn(size(Y)...)

_, dF_unthunked, _ = dF_pullback(Ȳ)

@assert !(getproperty(dF_unthunked, p) isa AbstractThunk)
dF_thunked = map(f->Thunk(()->f), dF_unthunked)
@assert getproperty(dF_thunked, p) isa AbstractThunk

dself_thunked, dX_thunked = dX_pullback(dF_thunked)
dself_unthunked, dX_unthunked = dX_pullback(dF_unthunked)
@test dself_thunked == dself_unthunked
mzgubic marked this conversation as resolved.
Show resolved Hide resolved
@test dX_thunked == dX_unthunked
end
end

@testset "+" begin
X = [1.0 2.0; 3.0 4.0; 5.0 6.0]
F, dX_pullback = rrule(svd, X)
Expand Down