-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2059a35
commit 71d9202
Showing
3 changed files
with
46 additions
and
46 deletions.
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,14 +1,14 @@ | ||
using ChainRulesCore | ||
|
||
@Base.aggressive_constprop accum(a, b) = a + b | ||
@Base.aggressive_constprop accum(a::Tuple, b::Tuple) = map(accum, a, b) | ||
@Base.aggressive_constprop @generated function accum(x::NamedTuple, y::NamedTuple) | ||
@Base.constprop aggressive accum(a, b) = a + b | ||
@Base.constprop aggressive accum(a::Tuple, b::Tuple) = map(accum, a, b) | ||
@Base.constprop aggressive @generated function accum(x::NamedTuple, y::NamedTuple) | ||
fnames = union(fieldnames(x), fieldnames(y)) | ||
gradx(f) = f in fieldnames(x) ? :(getfield(x, $(quot(f)))) : :(ZeroTangent()) | ||
grady(f) = f in fieldnames(y) ? :(getfield(y, $(quot(f)))) : :(ZeroTangent()) | ||
Expr(:tuple, [:($f=accum($(gradx(f)), $(grady(f)))) for f in fnames]...) | ||
end | ||
@Base.aggressive_constprop accum(a, b, c, args...) = accum(accum(a, b), c, args...) | ||
@Base.aggressive_constprop accum(a::NoTangent, b) = b | ||
@Base.aggressive_constprop accum(a, b::NoTangent) = a | ||
@Base.aggressive_constprop accum(a::NoTangent, b::NoTangent) = NoTangent() | ||
@Base.constprop aggressive accum(a, b, c, args...) = accum(accum(a, b), c, args...) | ||
@Base.constprop aggressive accum(a::NoTangent, b) = b | ||
@Base.constprop aggressive accum(a, b::NoTangent) = a | ||
@Base.constprop aggressive accum(a::NoTangent, b::NoTangent) = NoTangent() |
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