Skip to content

Commit

Permalink
Check that if only one adjoint exists, it is not thunked (#53)
Browse files Browse the repository at this point in the history
* Test no thunking when unnecessary

* Remove test that now fails by design

* Add note that InplaceableThunk is allowed

* Increment version number
  • Loading branch information
sethaxen authored Jul 13, 2020
1 parent 0233e62 commit b59aa7b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRulesTestUtils"
uuid = "cdddcdb0-9152-4a09-a978-84456f9df70a"
version = "0.4.3"
version = "0.5.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
10 changes: 9 additions & 1 deletion src/testers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ function rrule_test(f, ȳ, xx̄s::Tuple{Any, Any}...; rtol=1e-9, atol=1e-9, fdm
x̄s_ad = ∂s[2:end]
@test ∂self === NO_FIELDS # No internal fields

x̄s_is_dne = x̄s .== nothing
# Correctness testing via finite differencing.
x̄s_fd = _make_fdm_call(fdm, (xs...) -> f(xs...; fkwargs...), ȳ, xs, x̄s .== nothing)
x̄s_fd = _make_fdm_call(fdm, (xs...) -> f(xs...; fkwargs...), ȳ, xs, x̄s_is_dne)
for (x̄_ad, x̄_fd) in zip(x̄s_ad, x̄s_fd)
if x̄_fd === nothing
# The way we've structured the above, this tests the propagator is returning a DoesNotExist
Expand All @@ -202,4 +203,11 @@ function rrule_test(f, ȳ, xx̄s::Tuple{Any, Any}...; rtol=1e-9, atol=1e-9, fdm
@test isapprox(x̄_ad, x̄_fd; rtol=rtol, atol=atol, kwargs...)
end
end

if count(!, x̄s_is_dne) == 1
# for functions with pullbacks that only produce a single non-DNE adjoint, that
# single adjoint should not be `Thunk`ed. InplaceableThunk is fine.
i = findfirst(!, x̄s_is_dne)
@test !(isa(x̄s_ad[i], Thunk))
end
end
5 changes: 1 addition & 4 deletions test/testers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ primalapprox(x) = x
# define rrule using ChainRulesCore's v0.9.0 convention, conjugating the derivative
# in the rrule
function ChainRulesCore.rrule(::typeof(sinconj), x)
# usually we would not thunk for a single output, because it will of course be
# used, but we do here to ensure that test_scalar works even if a scalar rrule
# thunks
sinconj_pullback(ΔΩ) = (NO_FIELDS, @thunk(conj(cos(x)) * ΔΩ))
sinconj_pullback(ΔΩ) = (NO_FIELDS, conj(cos(x)) * ΔΩ)
return sin(x), sinconj_pullback
end

Expand Down

2 comments on commit b59aa7b

@sethaxen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/17865

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.0 -m "<description of version>" b59aa7b92a47bda972a6f3f7d6290440ba7f8ada
git push origin v0.5.0

Please sign in to comment.