-
-
Notifications
You must be signed in to change notification settings - Fork 212
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
Use clamptype
mechanism to project onto cotangent space
#965
Closed
Closed
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5b90288
one clamp design
mcabbott 1b3a8b0
simpler
mcabbott c1d04d2
simpler
mcabbott 7006f0f
some tests, some skips
mcabbott 46fddeb
a change to getindex gradient to preserve less structure
mcabbott b7159a8
test
mcabbott 08c5126
move logic for mapping clamptype here from ZygoteRules
mcabbott 63e1395
tidy
mcabbott 5bf8d82
not yet UnitUpperTriangular
mcabbott d085ef6
interact with ChainRules and accum, fix some tests
mcabbott eb3c2d9
skip many cholesky etc. tests
mcabbott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
import ZygoteRules: clamptype | ||
# This sees a tuple of argument types, and can modify the resulting tuple of tangents | ||
|
||
clamptype(Ts::Tuple{}, dxs::Tuple{}) = () | ||
clamptype(Ts::Tuple, dxs::Tuple) = | ||
first(Ts) === GlobalRef ? clamptype(Base.tail(Ts), dxs) : | ||
(clamptype(first(Ts), first(dxs)), clamptype(Base.tail(Ts), Base.tail(dxs))...) | ||
|
||
clamptype(Ts::Tuple{}, dxs::Tuple) = (@error "mismatch!" dxs; dxs) | ||
clamptype(Ts::Tuple, dxs::Tuple{}) = (@error "mismatch!" Ts; ()) | ||
|
||
# Bool, Real, Complex | ||
|
||
# clamptype(::Type{Bool}, dx) = nothing | ||
# clamptype(::Type{Bool}, dx::Complex) = nothing # ambiguity | ||
# clamptype(::Type{<:AbstractArray{Bool}}, dx::AbstractArray) = nothing | ||
# clamptype(::Type{<:AbstractArray{Bool}}, dx::AbstractArray) = (@info "bool array" summary(dx); nothing) | ||
|
||
# clamptype(::Type{Bool}, dx) = (@info "bool, dropping" typeof(dx); nothing) | ||
# clamptype(::Type{Bool}, dx::Complex) = (@info "bool, dropping" typeof(dx); nothing) | ||
# clamptype(::Type{<:AbstractArray{Bool}}, dx::AbstractArray) = (@info "bool array, disabled" summary(dx); dx) | ||
|
||
clamptype(::Type{<:Real}, dx::Complex) = real(dx) | ||
clamptype(::Type{<:AbstractArray{<:Real}}, dx::AbstractArray) = real(dx) | ||
|
||
using LinearAlgebra: Diagonal, UpperTriangular, UnitUpperTriangular, LowerTriangular, UnitLowerTriangular | ||
using LinearAlgebra: AdjointAbsVec, TransposeAbsVec, AdjOrTransAbsVec | ||
|
||
# LinearAlgebra's matrix types | ||
|
||
for Wrap in [:Diagonal, :UpperTriangular, :LowerTriangular] | ||
@eval begin | ||
clamptype(::Type{<:$Wrap{T,PT}}, dx::$Wrap) where {T,PT} = | ||
clamptype(PT, dx) | ||
clamptype(::Type{<:$Wrap{T,PT}}, dx::AbstractMatrix) where {T,PT} = | ||
clamptype(PT, $Wrap(dx)) | ||
# not right for :UnitUpperTriangular, :UnitLowerTriangular | ||
end | ||
end | ||
|
||
for (trans, Wrap) in [(transpose, :Symmetric), (Base.adjoint, :Hermitian)] | ||
@eval begin | ||
clamptype(::Type{<:$Wrap{T,PT}}, dx::$Wrap) where {T,PT} = | ||
clamptype(PT, dx) | ||
clamptype(::Type{<:$Wrap{T,PT}}, dx::AbstractMatrix) where {T,PT} = | ||
clamptype(PT, $Wrap(_twofold($trans, dx))) | ||
end | ||
end | ||
|
||
_twofold(trans, dx) = (dx .+ trans(dx)) ./ 2 | ||
function _twofold(trans, dx::Array{<:AbstractFloat}) | ||
@inbounds for i in axes(dx,1) | ||
for j in i+1:lastindex(dx,2) | ||
dx[i,j] = (dx[i,j] + trans(dx[j,i])) / 2 | ||
end | ||
end | ||
dx | ||
end | ||
|
||
clamptype(::Type{<:AdjOrTransAbsVec{T,PT}}, dx::AdjOrTransAbsVec) where {T,PT} = | ||
clamptype(PT, dx) | ||
clamptype(::Type{<:AdjOrTransAbsVec{T,PT}}, dx::AbstractMatrix) where {T,PT} = | ||
clamptype(PT, transpose(vec(dx))) # sometimes wrong wrapper but avoids conjugation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using ZygoteRules: clamptype | ||
using LinearAlgebra | ||
|
||
@info "----- starting type clamp tests" | ||
|
||
@testset "clamptype" begin | ||
|
||
# Real & Complex | ||
@test clamptype(Float32, 1+im) === 1 | ||
@test clamptype(ComplexF64, 1+im) === 1+im | ||
|
||
TA = typeof(rand(3)) | ||
@test clamptype(TA, 1:3) === 1:3 | ||
@test clamptype(TA, (1:3) .+ im) isa Vector{Int} | ||
|
||
# Boolean | ||
# @test clamptype(Bool, 1+im) === nothing | ||
# TB = typeof(rand(3) .> 0.5) | ||
# @test clamptype(TB, rand(3)) === nothing | ||
# @test clamptype(TB, Diagonal(1:3)) === nothing | ||
|
||
# Structured, I | ||
TD = typeof(Diagonal(1:3)) | ||
@test clamptype(TD, reshape(1:9, 3, 3)) isa Diagonal{Int,<:Vector} | ||
@test clamptype(TD, Diagonal((1:3) .+ im)) == Diagonal(1:3) | ||
|
||
# Structured, II | ||
TH = typeof(Hermitian(rand(3,3) .+ im)) | ||
TS = typeof(Symmetric(rand(3,3))) | ||
@test clamptype(TS, reshape(1:4,2,2) .+ im) == [1 2.5; 2.5 4] | ||
AH = clamptype(TH, reshape(1:4,2,2) .+ im) | ||
@test AH == [1 2.5; 2.5 4] | ||
@test AH isa Hermitian{ComplexF64} | ||
@test clamptype(TH, reshape(1:4,2,2)) isa Hermitian{Float64} | ||
|
||
# Row vectors | ||
TA = typeof((1:3)') | ||
TT = typeof(transpose(1:3)) | ||
TC = typeof(adjoint(rand(3) .+ im)) | ||
@test clamptype(TA, permutedims(1:3)) isa LinearAlgebra.AdjOrTransAbsVec | ||
@test clamptype(TA, ones(1,3) .+ im) isa LinearAlgebra.AdjOrTrans{Float64,<:Vector} | ||
@test clamptype(TC, ones(1,3) .+ im) == [1+im 1+im 1+im] | ||
|
||
# Tricky | ||
# TDB = typeof(Diagonal(rand(3) .> 0.5)) | ||
# @test clamptype(TDB, rand(3,3)) === nothing | ||
# @test clamptype(TDB, rand(ComplexF32, 3,3)) === nothing | ||
# TAB = typeof(transpose([true, false])) | ||
# @test clamptype(TAB, rand(3)') === nothing | ||
end | ||
|
||
@testset "clamped gradients" begin # only the marked tests pass on master | ||
|
||
# Real & Complex | ||
@test gradient(x -> abs2(x+im), 2) == (4,) | ||
@test gradient(x -> abs2(x+im), 2+0im) == (4 + 2im,) # as before | ||
|
||
@test gradient(x -> abs2(sum(x .+ im)), [1, 2])[1] == [6, 6] | ||
@test gradient(x -> abs2(sum(x .+ im)), Any[1, 2])[1] == [6, 6] | ||
@test gradient(x -> abs2(sum(x .+ im)), [1, 2+0im])[1] == [6 + 4im, 6 + 4im] # as before | ||
|
||
# Structured, some zeros | ||
# (if rules improve, these will end up testing them not the projection) | ||
@test gradient(x -> sum(x .+ 1), Diagonal(rand(3)))[1] == Diagonal([1,1,1]) | ||
@test gradient(x -> sum(sqrt.(x .+ 1)./2), Diagonal(rand(3)))[1] isa Diagonal | ||
@test gradient(x -> sum(x .+ 1), UpperTriangular(rand(3,3)))[1] == UpperTriangular(ones(3,3)) | ||
|
||
@test gradient(x -> x[1,2], LowerTriangular(rand(3,3)))[1] == zeros(3,3) | ||
@test_broken gradient(x -> x[1,2], UnitLowerTriangular(rand(3,3)))[1] == zeros(3,3) | ||
|
||
ld = gradient((x,y) -> sum(x * y), LowerTriangular(ones(3,3)), Diagonal(ones(3,3))) | ||
@test ld[1] isa LowerTriangular | ||
@test_broken ld[2] isa Diagonal | ||
|
||
# Structured, some symmetry | ||
@test gradient(x -> sum(x .+ 1), Symmetric(rand(3,3)))[1] isa Symmetric | ||
@test gradient(x -> x[1,2], Symmetric(rand(3,3)))[1] == [0 1/2 0; 1/2 0 0; 0 0 0] | ||
|
||
@test_broken gradient(x -> sum(x * x'), Symmetric(ones(3,3)))[1] isa Symmetric | ||
|
||
# Row vector restoration | ||
@test pullback(x -> x.+1, rand(3)')[2](ones(1,3))[1] isa LinearAlgebra.AdjOrTransAbsVec | ||
@test pullback(x -> x.+1, rand(3)')[2]([1 2 3+im])[1] == [1 2 3] | ||
@test pullback(x -> x.+1, rand(ComplexF64, 3)')[2]([1 2 3+im])[1] == [1 2 3+im] # as before | ||
|
||
@test gradient(x -> x[1,2], rand(3)')[1] isa LinearAlgebra.AdjOrTransAbsVec # worked, broken by _zero change | ||
end | ||
|
||
@info "----- done type clamp tests" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice this:
If you use
_twofold(trans, dx) = dx .+ trans(dx) .- Diagonal(dx)
, then the Zygote results will double, and the 1st & 3rd will be quite obviously wrong. And this isn't a projection operator.Is the FiniteDifferences result the right thing for a tangent not cotangent? Or is it a bug? As far as I can tell it's doing something like this:
Looking in ChainRules, there seem to be few tests using this behaviour of FiniteDifferences. There is a rule for
Matrix
, which applies(dx .+ dx' .- Diagonal(dx)
to match:Defined here https://github.com/JuliaDiff/ChainRules.jl/blob/4e3164a3a48d4da35e0112d30be7ea9dbdaf3920/src/rulesets/LinearAlgebra/symmetric.jl#L71 where
_symmetric_back
is also the gradient ofSymmetric
where it makes more sense IMO. (Originally from Zygote, I think.)