-
-
Notifications
You must be signed in to change notification settings - Fork 30
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
[WIP] Add ChainRules differentiation rules for expv #51
Open
sethaxen
wants to merge
4
commits into
SciML:master
Choose a base branch
from
sethaxen:chainrules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,24 @@ authors = ["Chris Rackauckas <[email protected]>"] | |
version = "1.8.0" | ||
|
||
[deps] | ||
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" | ||
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" | ||
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" | ||
Requires = "ae029012-a4dd-5104-9daa-d747884805df" | ||
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" | ||
|
||
[compat] | ||
ChainRulesCore = "0.9" | ||
FiniteDifferences = "0.11" | ||
Requires = "1.0" | ||
julia = "1" | ||
|
||
[extras] | ||
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" | ||
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" | ||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["ForwardDiff", "Test", "SafeTestsets", "Random"] | ||
test = ["FiniteDifferences", "ForwardDiff", "Test", "SafeTestsets", "Random"] |
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,42 @@ | ||
function ChainRulesCore.frule((_, Δt, ΔA, Δb), ::typeof(expv), t, A, b; kwargs...) | ||
w = expv(t, A, b; kwargs...) | ||
∂w = similar(w) | ||
mul!(∂w, A, w) | ||
∂w .*= Δt | ||
if !isa(Δb, AbstractZero) | ||
∂w .+= expv(t, A, Δb; kwargs...) | ||
end | ||
# TODO: handle ΔA | ||
ΔA isa AbstractZero || error("ΔA currently cannot be pushed forward") | ||
return w, ∂w | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(expv), t, A, b; kwargs...) | ||
w = expv(t, A, b; kwargs...) | ||
function expv_pullback(Δw) | ||
∂t = Thunk() do | ||
t̄ = A isa AbstractMatrix ? conj(dot(Δw, A, w)) : dot(mul!(similar(w), A, w), Δw) | ||
return t isa Real ? real(t̄) : t̄ | ||
end | ||
# TODO: handle ∂A | ||
∂A = @thunk error("Adjoint wrt A not yet implemented") | ||
∂b = Thunk() do | ||
# using similar is necessary to ensure type-stability | ||
b̄ = similar(b) | ||
_copyto!(b̄, expv(t', A', Δw; kwargs...)) | ||
return b̄ | ||
end | ||
return (NO_FIELDS, ∂t, ∂A, ∂b) | ||
end | ||
expv_pullback(::Zero) = (NO_FIELDS, Zero(), Zero(), Zero()) | ||
return w, expv_pullback | ||
end | ||
|
||
function _copyto!(x, y) | ||
if eltype(x) <: Real && !(eltype(y) <: Real) | ||
x .= real.(y) | ||
else | ||
copyto!(x, y) | ||
end | ||
return x | ||
end |
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.
adjoint
is currently not part of the required interface for genericA
. How should we handle this?A
(i.,e. "if you want to work with Zygote, you also need to defineadjoint
")rrule
toA::AbstractMatrix
(this may be necessary anyways, as for non-matrixA
, its differential type is unknown.).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.
I think that's entirely reasonable.