Skip to content
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

Check that the correct AD backend is being used #2291

Merged
merged 5 commits into from
Aug 7, 2024

Conversation

mhauru
Copy link
Member

@mhauru mhauru commented Jul 18, 2024

Introduces tests that check that the ADType we specify is actually the one being used. This alleviates my worry that we might mess up passing the ADType around and accidentally fall back on the default.

I've added tests for calling sample and maximum_likelihood/maximum_a_posteriori. Any other user-facing functions we should check to make sure they, too, use their adtype arguments correctly?

Closes #2235

@mhauru mhauru force-pushed the mhauru/adtypecheckcontext branch from c2561b7 to c0cab10 Compare July 19, 2024 10:21
@mhauru mhauru marked this pull request as ready for review July 19, 2024 10:23
@mhauru mhauru requested a review from sunxd3 July 19, 2024 10:23
@mhauru
Copy link
Member Author

mhauru commented Jul 19, 2024

@torfjelde pointed out in https://github.com/TuringLang/Turing.jl/pull/2221/files#r1608405934 that there's a similarity between this and DebugContext, in that they both call their child context as usual and just add an extra call before/after. Thus we could rather implement a more general CallbackContext, and unify the implementation of DebugContext and ADTypeCheckContext under it.

I think this is a great idea, but I chose not to do it in this PR because

  1. Would need to give a good think as to what powers the callbacks should have. Are they e.g. allowed to modify varinfo?
  2. Would want to check whether any other contexts we currently have fit this CallbackContext format, and bring those in as well.

Copy link

codecov bot commented Jul 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.77%. Comparing base (29a1342) to head (15701c2).
Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2291      +/-   ##
==========================================
- Coverage   85.82%   85.77%   -0.06%     
==========================================
  Files          24       24              
  Lines        1623     1617       -6     
==========================================
- Hits         1393     1387       -6     
  Misses        230      230              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@sunxd3 sunxd3 left a comment

Choose a reason for hiding this comment

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

The code looks good to me.

I don't think comparing the types of values in varinfo is particularly elegant, but also I don't see other ways to do it with Contexts.

@sunxd3
Copy link
Collaborator

sunxd3 commented Jul 19, 2024

Maybe @yebai, @torfjelde, or @devmotion can give a quick scan?

@coveralls
Copy link

coveralls commented Jul 19, 2024

Pull Request Test Coverage Report for Build 10268798219

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.05%) to 85.989%

Files with Coverage Reduction New Missed Lines %
src/mcmc/abstractmcmc.jl 2 94.55%
Totals Coverage Status
Change from base Build 9953741319: -0.05%
Covered Lines: 1387
Relevant Lines: 1613

💛 - Coveralls

@mhauru
Copy link
Member Author

mhauru commented Jul 19, 2024

I don't think comparing the types of values in varinfo is particularly elegant, but also I don't see other ways to do it with Contexts.

Agree on lack of elegance, better proposals welcome.

Copy link
Member

@torfjelde torfjelde left a comment

Choose a reason for hiding this comment

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

I like the idea very much:) Seems like something we should definitively test. And agree that we shouldn't do the CallbackContext in this PR.

I left a comment on a potentially way we can check Zygote.jl, Tapir.jl, and Enzyme.jl too.

),
# TODO(mhauru) Zygote.Dual is actually the same as ForwardDiff.Dual, so can't
# distinguish between the two.
Turing.AutoZygote => (Zygote.Dual,),
Copy link
Member

Choose a reason for hiding this comment

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

Yeah there's no way you can do this here unfortunately 😕

Possibly crazy idea, but it might make sense to specifically overload the adjoint computation for a given backend which doesn't use types, e.g.

Zygote.@adjoint function check_adtype(...)
    # Should only be hit if we're using Zygote.jl.
    ...
end

? Could do the same with Enzyme.jl and Tapir.jl, both of which would suffer from the same issue

Copy link
Member Author

Choose a reason for hiding this comment

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

I like this, and implemented it. Turns out there's Zygote.hook for exactly these sorts of things.

I thought a bit about switching to doing all checks like this, and giving up on the element type approach, but that seemed like it would get more complicated than is worth at this point. We now catch all cases except if one uses ForwardDiff when Zygote is expected, which seems sufficient to me.

Copy link
Member

Choose a reason for hiding this comment

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

Very, very nice @mhauru :)

test/test_utils/ad_utils.jl Outdated Show resolved Hide resolved
@mhauru
Copy link
Member Author

mhauru commented Aug 6, 2024

The only CI failure is a tiny, trivial loss of code coverage. @torfjelde, are you happy to have this merged as is?

Copy link
Member

@torfjelde torfjelde left a comment

Choose a reason for hiding this comment

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

Very, very nice @mhauru :)

@mhauru mhauru merged commit 803d2f5 into master Aug 7, 2024
59 of 60 checks passed
@mhauru mhauru deleted the mhauru/adtypecheckcontext branch August 7, 2024 09:09
@yebai yebai mentioned this pull request Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests to check that the correct AD method is actually used
4 participants