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.
Overview
This is a, perhaps somewhat overdue, PR to add the functionality which I first wrote in https://github.com/penelopeysm/ModelTests.jl.
It provides two main functions:
DynamicPPL.TestUtils.AD.ad_ldp(::Model, ::Vector{<:Real}, ::AbstractADType, ::AbstractVarInfo)
DynamicPPL.TestUtils.AD.ad_di
(same signature)which calculate the logdensity and its gradient of a given model at the specified parameters.
The former uses LogDensityProblemsAD.jl; the latter circumvents this and goes straight to DifferentiationInterface.jl. (The varinfo argument is used only to specify the type of varinfo used during the evaluation, its contents are ignored. I wish that there was a cleaner way to specify this, but as far as I can tell it's not possible, especially with SimpleVarInfo which often requires parameters to be initialised inside it.)
There are three auxiliary functions:
DynamicPPL.TestUtils.AD.make_function
andDynamicPPL.TestUtils.AD.make_params
generate a functionf
and an argumentx
, such thatf(x)
evaluates the logdensity of a model at the pointx
. These can, in theory, be passed to any autodiff library, even those which do not have integrations with LogDensityProblemsAD, DifferentiationInterface, or ADTypes.DynamicPPL.TestUtils.AD.test_correctness
provides a quick and easy wrapper to test a model plus a given set of AD backends (using the default VarInfo) for correctness.Testing
Unfortunately, I didn't manage to make much use of
test_correctness
in the current DynamicPPL test suite. The main reason is because we are testing all the demo models with pretty much all possible variations of VarInfo.I have made sure to not change the tests, but I'm not entirely convinced that we need to test AD with different combinations of VarInfo. The reason is because AD is used primarily during sampling, and there isn't really any way to actually call
AbstractMCMC.sample
on a model (cf. #606) with anything but the defaultVarInfo
.The use of non-default varinfos is, as far as I can tell, restricted to fairly small sections of the codebase (e.g. the
loglikelihood
/logjoint
/logprior
functions), and it's not clear to me that AD is used in any part of that. So, it seems to me that these are orthogonal concerns.I've left it versatile for now to be on the safe side, but if people agree then I would be very happy to remove the varinfo argument from the functions above.
Miscellaneous bits
The names of the functions can be changed, I'm not super happy with them, but also I've stared at this code for too long so I'm not the best person to suggest names 😉