Skip to content

Make run_ad return both primal and gradient time #1002

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

Draft
wants to merge 7 commits into
base: breaking
Choose a base branch
from

Conversation

penelopeysm
Copy link
Member

run_ad would calculate both and then return only the ratio, which seemed a bit wasteful.

With this change we also get to use run_ad in the CI benchmarking workflow.

@penelopeysm penelopeysm changed the base branch from main to breaking July 29, 2025 15:07
Comment on lines -40 to +49
("Smorgasbord", smorgasbord_instance, :typed, :forwarddiff, false),
("Smorgasbord", smorgasbord_instance, :simple_namedtuple, :forwarddiff, true),
("Smorgasbord", smorgasbord_instance, :untyped, :forwarddiff, true),
("Smorgasbord", smorgasbord_instance, :simple_dict, :forwarddiff, true),
("Smorgasbord", smorgasbord_instance, :typed, :reversediff, true),
("Smorgasbord", smorgasbord_instance, :typed, :mooncake, true),
("Loop univariate 1k", loop_univariate1k, :typed, :mooncake, true),
("Multivariate 1k", multivariate1k, :typed, :mooncake, true),
("Loop univariate 10k", loop_univariate10k, :typed, :mooncake, true),
("Multivariate 10k", multivariate10k, :typed, :mooncake, true),
("Dynamic", Models.dynamic(), :typed, :mooncake, true),
("Submodel", Models.parent(randn(rng)), :typed, :mooncake, true),
("LDA", lda_instance, :typed, :reversediff, true),
("Smorgasbord", smorgasbord_instance, :typed, :forwarddiff),
("Smorgasbord", smorgasbord_instance, :simple_namedtuple, :forwarddiff),
("Smorgasbord", smorgasbord_instance, :untyped, :forwarddiff),
("Smorgasbord", smorgasbord_instance, :simple_dict, :forwarddiff),
("Smorgasbord", smorgasbord_instance, :typed, :reversediff),
("Smorgasbord", smorgasbord_instance, :typed, :mooncake),
("Loop univariate 1k", loop_univariate1k, :typed, :mooncake),
("Multivariate 1k", multivariate1k, :typed, :mooncake),
("Loop univariate 10k", loop_univariate10k, :typed, :mooncake),
("Multivariate 10k", multivariate10k, :typed, :mooncake),
("Dynamic", Models.dynamic(), :typed, :mooncake),
("Submodel", Models.parent(randn(rng)), :typed, :mooncake),
Copy link
Member Author

@penelopeysm penelopeysm Jul 29, 2025

Choose a reason for hiding this comment

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

This PR also removes the option to set varinfo to be linked or unlinked. Going forward everything is linked. In practice there really aren't any cases where AD is run with unlinked varinfo (indeed running with unlinked is a recipe for bugs when you have constraints like in Dirichlet or LKJCholesky, see e.g. TuringLang/ADTests#7) so I don't think that we should do it here.

Copy link
Contributor

DynamicPPL.jl documentation for PR #1002 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR1002/

BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Chairmarks = "0ca39b1e-fe0b-4e98-acfc-b1656634c4de"
Copy link
Member Author

Choose a reason for hiding this comment

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

note Chairmarks is already a strong dep of DPPL

Comment on lines -76 to +57
retvals = model(rng)
vns = [VarName{k}() for k in keys(retvals)]
SimpleVarInfo{Float64}(Dict(zip(vns, values(retvals))))
vi = DynamicPPL.typed_varinfo(rng, model)
vals = DynamicPPL.values_as(vi, Dict)
SimpleVarInfo{Float64}(vals)
Copy link
Member Author

@penelopeysm penelopeysm Jul 29, 2025

Choose a reason for hiding this comment

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

The old code only works if the model explicitly returns a NamedTuple with only plain Symbols (which they happen to do). I am aware that this pattern is peppered all over the code base (e.g. with demo models too) but I think we should try to avoid having magic return values and instead rely on functionality that is designed to work on all models.

Copy link

codecov bot commented Jul 29, 2025

Codecov Report

❌ Patch coverage is 28.57143% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.87%. Comparing base (5d9e934) to head (63bb81f).

Files with missing lines Patch % Lines
src/test_utils/ad.jl 28.57% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           breaking    #1002      +/-   ##
============================================
- Coverage     81.91%   81.87%   -0.05%     
============================================
  Files            38       38              
  Lines          4025     4027       +2     
============================================
  Hits           3297     3297              
- Misses          728      730       +2     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@penelopeysm penelopeysm marked this pull request as draft July 29, 2025 16:38
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.

1 participant