-
Notifications
You must be signed in to change notification settings - Fork 18
RuleConfig and Zygote support #49
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
Changes from all commits
54365f8
0a0d815
ce1c0c3
ebbd71b
2fef1a1
1dd9762
07e8e33
6207218
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
ReverseRuleConfigBackend | ||
|
||
AD backend that uses reverse mode with any ChainRules-compatible reverse-mode AD package. | ||
""" | ||
struct ReverseRuleConfigBackend{RC <: RuleConfig} <: AbstractReverseMode | ||
ruleconfig::RC | ||
end | ||
|
||
AD.@primitive function pullback_function(ab::ReverseRuleConfigBackend, f, xs...) | ||
return (vs) -> begin | ||
_, back = rrule_via_ad(ab.ruleconfig, f, xs...) | ||
if vs isa Tuple && length(vs) === 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this compile away in the same way as, I would assume, |
||
return Base.tail(back(vs[1])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems this rules out support for functors There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, this is so. AbstractDifferentiation supports a strictly smaller set of applications than, say, Zygote. It's all about scalars and arrays. And even there, I'm pretty sure results will be inconsistent if one tries to, say, take the gradient of a structured array. And functors are not supported. |
||
else | ||
return Base.tail(back(vs)) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using AbstractDifferentiation | ||
using Test | ||
using Zygote | ||
|
||
@testset "ReverseRuleConfigBackend(ZygoteRuleConfig())" begin | ||
backends = [@inferred(AD.ZygoteBackend())] | ||
@testset for backend in backends | ||
@testset "Derivative" begin | ||
test_derivatives(backend) | ||
end | ||
@testset "Gradient" begin | ||
test_gradients(backend) | ||
end | ||
@testset "Jacobian" begin | ||
test_jacobians(backend) | ||
end | ||
@testset "jvp" begin | ||
test_jvp(backend) | ||
end | ||
@testset "j′vp" begin | ||
test_j′vp(backend) | ||
end | ||
@testset "Lazy Derivative" begin | ||
test_lazy_derivatives(backend) | ||
end | ||
@testset "Lazy Gradient" begin | ||
test_lazy_gradients(backend) | ||
end | ||
@testset "Lazy Jacobian" begin | ||
test_lazy_jacobians(backend) | ||
end | ||
end | ||
end |
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.
What's reason for moving this in the function body? Isn't it better to obtain
back
only once outside of the function instead of doing it at every invocation of the pullback function?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 is a good point.