-
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
Fix rrule for * and add support for constant operations #211
Open
blegat
wants to merge
2
commits into
master
Choose a base branch
from
bl/chain_rule_constant
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,30 +1,108 @@ | ||
# The publlback depends on the scalar product on the polynomials | ||
# With the scalar product `LinearAlgebra.dot(p, q) = p * q`, there is no pullback for `differentiate` | ||
# With the scalar product `_dot(p, q)` of `test/chain_rules.jl`, there is a pullback for `differentiate` | ||
# and the pullback for `*` changes. | ||
# We give the one for the scalar product `_dot`. | ||
|
||
import ChainRulesCore | ||
|
||
ChainRulesCore.@scalar_rule +(x::APL) true | ||
ChainRulesCore.@scalar_rule -(x::APL) -1 | ||
|
||
ChainRulesCore.@scalar_rule +(x::APL, y::APL) (true, true) | ||
function plusconstant1_pullback(Δ) | ||
return ChainRulesCore.NoTangent(), Δ, coefficient(Δ, constantmonomial(Δ)) | ||
end | ||
function ChainRulesCore.rrule(::typeof(plusconstant), p::APL, α) | ||
return plusconstant(p, α), plusconstant1_pullback | ||
end | ||
function plusconstant2_pullback(Δ) | ||
return ChainRulesCore.NoTangent(), coefficient(Δ, constantmonomial(Δ)), Δ | ||
end | ||
function ChainRulesCore.rrule(::typeof(plusconstant), α, p::APL) | ||
return plusconstant(α, p), plusconstant2_pullback | ||
end | ||
ChainRulesCore.@scalar_rule -(x::APL, y::APL) (true, -1) | ||
|
||
function ChainRulesCore.frule((_, Δp, Δq), ::typeof(*), p::APL, q::APL) | ||
return p * q, MA.add_mul!!(p * Δq, q, Δp) | ||
end | ||
|
||
function _mult_pullback(op::F, ts, p, Δ) where {F<:Function} | ||
for t in terms(p) | ||
c = coefficient(t) | ||
m = monomial(t) | ||
for δ in Δ | ||
if divides(m, δ) | ||
coef = op(c, coefficient(δ)) | ||
mono = _div(monomial(δ), m) | ||
push!(ts, term(coef, mono)) | ||
end | ||
end | ||
end | ||
return polynomial(ts) | ||
end | ||
function adjoint_mult_left(p, Δ) | ||
ts = MA.promote_operation(*, MA.promote_operation(adjoint, termtype(p)), termtype(Δ))[] | ||
return _mult_pullback(ts, p, Δ) do c, d | ||
c' * d | ||
end | ||
end | ||
function adjoint_mult_right(p, Δ) | ||
ts = MA.promote_operation(*, termtype(Δ), MA.promote_operation(adjoint, termtype(p)))[] | ||
return _mult_pullback(ts, p, Δ) do c, d | ||
d * c' | ||
end | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(*), p::APL, q::APL) | ||
function times_pullback2(ΔΩ̇) | ||
#ΔΩ = ChainRulesCore.unthunk(Ω̇) | ||
#return (ChainRulesCore.NoTangent(), ChainRulesCore.ProjectTo(p)(ΔΩ * q'), ChainRulesCore.ProjectTo(q)(p' * ΔΩ)) | ||
# This is for the scalar product `_dot`: | ||
return (ChainRulesCore.NoTangent(), adjoint_mult_right(q, ΔΩ̇), adjoint_mult_left(p, ΔΩ̇)) | ||
# For the scalar product `dot`, it would be instead: | ||
return (ChainRulesCore.NoTangent(), ΔΩ̇ * q', p' * ΔΩ̇) | ||
end | ||
return p * q, times_pullback2 | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(multconstant), α, p::APL) | ||
function times_pullback2(ΔΩ̇) | ||
# TODO we could make it faster, don't need to compute `Δα` entirely if we only care about the constant term. | ||
Δα = adjoint_mult_right(p, ΔΩ̇) | ||
return (ChainRulesCore.NoTangent(), coefficient(Δα, constantmonomial(Δα)), α' * ΔΩ̇) | ||
end | ||
return multconstant(α, p), times_pullback2 | ||
end | ||
|
||
function ChainRulesCore.rrule(::typeof(multconstant), p::APL, α) | ||
function times_pullback2(ΔΩ̇) | ||
# TODO we could make it faster, don't need to compute `Δα` entirely if we only care about the constant term. | ||
Δα = adjoint_mult_left(p, ΔΩ̇) | ||
return (ChainRulesCore.NoTangent(), ΔΩ̇ * α', coefficient(Δα, constantmonomial(Δα))) | ||
end | ||
return multconstant(p, α), times_pullback2 | ||
end | ||
|
||
notangent3(Δ) = ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent(), ChainRulesCore.NoTangent() | ||
function ChainRulesCore.rrule(::typeof(^), mono::AbstractMonomialLike, i::Integer) | ||
return mono^i, notangent3 | ||
end | ||
|
||
function ChainRulesCore.frule((_, Δp, _), ::typeof(differentiate), p, x) | ||
return differentiate(p, x), differentiate(Δp, x) | ||
end | ||
function pullback(Δdpdx, x) | ||
# This is for the scalar product `_dot`, there is no pullback for the scalar product `dot` | ||
function differentiate_pullback(Δdpdx, x) | ||
return ChainRulesCore.NoTangent(), x * differentiate(x * Δdpdx, x), ChainRulesCore.NoTangent() | ||
end | ||
function ChainRulesCore.rrule(::typeof(differentiate), p, x) | ||
dpdx = differentiate(p, x) | ||
return dpdx, Base.Fix2(pullback, x) | ||
return dpdx, Base.Fix2(differentiate_pullback, x) | ||
end | ||
|
||
function coefficient_pullback(Δ, m::AbstractMonomialLike) | ||
return ChainRulesCore.NoTangent(), polynomial(term(Δ, m)), ChainRulesCore.NoTangent() | ||
end | ||
function ChainRulesCore.rrule(::typeof(coefficient), p::APL, m::AbstractMonomialLike) | ||
return coefficient(p, m), Base.Fix2(coefficient_pullback, m) | ||
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.
The previous definition was correct for the scalar product
dot(p, q) = p * q
, the current one is correct for the scalar productdot(p, q) = dot(coefficients(p), coefficients(q))
(assuming they have the same monomials).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.
This second scalar product corresponds to the
_dot
we use for testingdifferentiate
. This means that fordifferentiate
we assume the other scalar product