Skip to content

Commit

Permalink
Refactor tangent testing utils (#109)
Browse files Browse the repository at this point in the history
* remove redundancy from tangent testing

* Bump patch
  • Loading branch information
willtebbutt authored Apr 1, 2024
1 parent 513f57d commit aed1aec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 83 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tapir"
uuid = "07d77754-e150-4737-8c94-cd238a1fb45b"
authors = ["Will Tebbutt and contributors"]
version = "0.1.1"
version = "0.1.2"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
96 changes: 14 additions & 82 deletions src/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ function test_tangent_consistency(rng::AbstractRNG, p::P; interface_only=false)
test_equality_comparison(p)
test_equality_comparison(t)

# Check that zero_tangent isn't obviously non-deterministic.
@test has_equal_data(z, Tapir.zero_tangent(p))

# Check that ismutabletype(P) => ismutabletype(T)
if ismutabletype(P) && !(T == NoTangent)
@test ismutabletype(T)
Expand Down Expand Up @@ -774,99 +777,28 @@ necessary but not sufficient conditions for the correctness of your code.
function test_tangent(rng::AbstractRNG, p::P, z_target::T, x::T, y::T) where {P, T}
@nospecialize rng p z_target x y

# This basic functionality must run in order to be able to check everything else.
@test tangent_type(P) isa Type
@test tangent_type(P) == T
@test zero_tangent(p) isa T
@test randn_tangent(rng, p) isa T
test_equality_comparison(p)
test_equality_comparison(x)

# Verify that interface `tangent_type` runs.
Tt = tangent_type(P)
t = randn_tangent(rng, p)
z = zero_tangent(p)

# Check that user-provided tangents have the same type as `tangent_type` expects.
@test T == Tt
# Check the interface.
test_tangent_consistency(rng, p; interface_only=false)

# Check that ismutabletype(P) => ismutabletype(T)
if ismutabletype(P) && !(Tt == NoTangent)
@test ismutabletype(Tt)
end

# Check that tangents are of the correct type.
@test Tt == _typeof(t)
@test Tt == _typeof(z)

# Check that zero_tangent is deterministic.
@test has_equal_data(z, Tapir.zero_tangent(p))
# Is the tangent_type of `P` what we expected?
@test tangent_type(P) == T

# Check that zero_tangent infers.
@test has_equal_data(z, @inferred Tapir.zero_tangent(p))
@inferred Tapir.zero_tangent(p)

# Verify that the zero tangent is zero via its action.
zc = deepcopy(z)
tc = deepcopy(t)
@test has_equal_data(@inferred(increment!!(zc, zc)), zc)
@test has_equal_data(increment!!(zc, tc), tc)
@test has_equal_data(increment!!(tc, zc), tc)

if ismutabletype(P)
@test increment!!(zc, zc) === zc
@test increment!!(tc, zc) === tc
@test increment!!(zc, tc) === zc
@test increment!!(tc, tc) === tc
end
z = zero_tangent(p)
t = randn_tangent(rng, p)
@test has_equal_data(@inferred(increment!!(z, z)), z)
@test has_equal_data(increment!!(z, t), t)
@test has_equal_data(increment!!(t, z), t)

# Verify that adding together `x` and `y` gives the value the user expected.
z_pred = increment!!(x, y)
@test has_equal_data(z_pred, z_target)
if ismutabletype(P)
@test z_pred === x
end

# If t isn't the zero element, then adding it to itself must change its value.
if !has_equal_data(t, z)
if !ismutabletype(P)
tc′ = increment!!(tc, tc)
@test tc === tc′ || !has_equal_data(tc′, tc)
end
end

# Adding things preserves types.
@test increment!!(zc, zc) isa Tt
@test increment!!(zc, tc) isa Tt
@test increment!!(tc, zc) isa Tt

# Setting to zero equals zero.
@test has_equal_data(set_to_zero!!(tc), z)
if ismutabletype(P)
@test set_to_zero!!(tc) === tc
end

z = zero_tangent(p)
r = randn_tangent(rng, p)

# Verify that operations required for finite difference testing to run, and produce the
# correct output type.
@test _add_to_primal(p, t) isa P
@test _diff(p, p) isa T
@test _dot(t, t) isa Float64
@test _scale(11.0, t) isa T
@test populate_address_map(p, t) isa AddressMap

# Run some basic numerical sanity checks on the output the functions required for finite
# difference testing. These are necessary but insufficient conditions.
@test has_equal_data(_add_to_primal(p, z), p)
if !has_equal_data(z, r)
@test !has_equal_data(_add_to_primal(p, r), p)
end
@test has_equal_data(_diff(p, p), zero_tangent(p))
@test _dot(t, t) >= 0.0
@test _dot(t, zero_tangent(p)) == 0.0
@test _dot(t, increment!!(deepcopy(t), t)) 2 * _dot(t, t)
@test has_equal_data(_scale(1.0, t), t)
@test has_equal_data(_scale(2.0, t), increment!!(deepcopy(t), t))
end

function test_equality_comparison(x)
Expand Down

2 comments on commit aed1aec

@willtebbutt
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 register()

@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/104013

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.1.2 -m "<description of version>" aed1aec4515a6d5cecd548773fdeb0310a3d7235
git push origin v0.1.2

Please sign in to comment.