Skip to content

ForwardDiff -> 1 #871

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

Merged
merged 4 commits into from
May 29, 2025
Merged

ForwardDiff -> 1 #871

merged 4 commits into from
May 29, 2025

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Mar 28, 2025

Closes #864
Closes #874
Closes #876

Copy link
Contributor

github-actions bot commented Mar 28, 2025

Benchmark Report for Commit ac22eb8

Computer Information

Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

|                 Model | Dimension |  AD Backend |      VarInfo Type | Linked | Eval Time / Ref Time | AD Time / Eval Time |
|-----------------------|-----------|-------------|-------------------|--------|----------------------|---------------------|
| Simple assume observe |         1 | forwarddiff |             typed |  false |                  9.7 |                 1.6 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                729.7 |                36.6 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                302.8 |                68.3 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |               1263.6 |                28.1 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               3350.8 |                23.5 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1473.3 |                29.4 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |                955.3 |                 5.3 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               5677.1 |                 4.0 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |               1042.8 |                 8.7 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              62593.0 |                 3.8 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               9568.9 |                 9.6 |
|               Dynamic |        10 |    mooncake |             typed |   true |                132.1 |                13.2 |
|              Submodel |         1 |    mooncake |             typed |   true |                 13.4 |                 6.6 |
|                   LDA |        12 | reversediff |             typed |   true |                453.8 |                 5.7 |

Copy link

codecov bot commented Mar 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.92%. Comparing base (bb0c857) to head (ac22eb8).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #871   +/-   ##
=======================================
  Coverage   82.92%   82.92%           
=======================================
  Files          36       36           
  Lines        3964     3964           
=======================================
  Hits         3287     3287           
  Misses        677      677           

☔ 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.

@coveralls
Copy link

coveralls commented Mar 28, 2025

Pull Request Test Coverage Report for Build 14134335858

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1213 unchanged lines in 26 files lost coverage.
  • Overall coverage decreased (-0.6%) to 84.414%

Files with Coverage Reduction New Missed Lines %
src/DynamicPPL.jl 1 90.91%
src/submodel_macro.jl 2 82.35%
src/extract_priors.jl 5 53.57%
src/test_utils/model_interface.jl 5 22.22%
src/test_utils/varinfo.jl 5 78.26%
src/values_as_in_model.jl 6 43.9%
src/logdensityfunction.jl 7 64.44%
src/model_utils.jl 7 7.14%
src/distribution_wrappers.jl 13 9.76%
src/model.jl 13 80.0%
Totals Coverage Status
Change from base Build 14127923718: -0.6%
Covered Lines: 3217
Relevant Lines: 3811

💛 - Coveralls

@coveralls
Copy link

coveralls commented Mar 28, 2025

Pull Request Test Coverage Report for Build 15324866261

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage remained the same at 83.089%

Files with Coverage Reduction New Missed Lines %
src/logdensityfunction.jl 2 76.6%
Totals Coverage Status
Change from base Build 15273778338: 0.0%
Covered Lines: 3287
Relevant Lines: 3956

💛 - Coveralls

@coveralls
Copy link

coveralls commented Mar 28, 2025

Pull Request Test Coverage Report for Build 14134335858

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1213 unchanged lines in 26 files lost coverage.
  • Overall coverage remained the same at 84.965%

Files with Coverage Reduction New Missed Lines %
src/DynamicPPL.jl 1 90.91%
src/submodel_macro.jl 2 82.35%
src/extract_priors.jl 5 53.57%
src/test_utils/model_interface.jl 5 22.22%
src/test_utils/varinfo.jl 5 78.26%
src/values_as_in_model.jl 6 43.9%
src/logdensityfunction.jl 7 64.44%
src/model_utils.jl 7 7.14%
src/distribution_wrappers.jl 13 9.76%
src/model.jl 13 80.0%
Totals Coverage Status
Change from base Build 14127923718: 0.0%
Covered Lines: 3238
Relevant Lines: 3811

💛 - Coveralls

@penelopeysm
Copy link
Member Author

penelopeysm commented Apr 1, 2025

CI is not resolving to ForwardDiff=1, so the green tick is not indicative of the compatibility. CI seems to resolve to ForwardDiff=1 on 1.11, but not on 1.10.

Also it's likely we need to fix Bijectors upstream first: TuringLang/Bijectors.jl#378 Fixed.

@penelopeysm penelopeysm marked this pull request as draft April 1, 2025 00:09
Copy link
Contributor

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

@penelopeysm penelopeysm marked this pull request as ready for review May 29, 2025 10:52
Comment on lines -1 to -29
@testset "ad.jl" begin
@testset "logp" begin
# Hand-written log probabilities for vector `x = [s, m]`.
function logp_gdemo_default(x)
s = x[1]
m = x[2]
dist = Normal(m, sqrt(s))

return logpdf(InverseGamma(2, 3), s) +
logpdf(Normal(0, sqrt(s)), m) +
logpdf(dist, 1.5) +
logpdf(dist, 2.0)
end

test_model_ad(gdemo_default, logp_gdemo_default)

@model function wishart_ad()
return v ~ Wishart(7, [1 0.5; 0.5 1])
end

# Hand-written log probabilities for `x = [v]`.
function logp_wishart_ad(x)
dist = Wishart(7, [1 0.5; 0.5 1])
return logpdf(dist, reshape(x, 2, 2))
end

test_model_ad(wishart_ad(), logp_wishart_ad)
end
end
Copy link
Member Author

@penelopeysm penelopeysm May 29, 2025

Choose a reason for hiding this comment

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

For full transparency, part of the motivation for deleting this file is that it's a bit pointless given that we have quite extensive AD testing now, but part of it is also because ForwardDiff@1 broke this test.

Specifically, the change to Dual equality in JuliaDiff/ForwardDiff.jl#481 means that FD cannot differentiate through cholesky -- already reported here: JuliaDiff/ForwardDiff.jl#606 and thus also logp of Wishart.

(For context, that ForwardDiff PR was merged in a 0.10 version, then yanked, and rereleased in 1.0, which is why it's coming back again in this PR)

This bug is trivial to reproduce with just ForwardDiff + Distributions:

using ForwardDiff, Distributions
d = Wishart(7, [1 0.5; 0.5 1])
x = rand(d)
function logp(x)
    return logpdf(d, reshape(x, 2, 2))
end
ForwardDiff.gradient(logp, x)

and thus has nothing to do with DynamicPPL, and I don't consider it our responsibility to work around it in our test suite.

Given all of the above, I don't mind just deleting this test.

Copy link
Member Author

@penelopeysm penelopeysm May 29, 2025

Choose a reason for hiding this comment

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

BTW, it still works for linked varinfo so in practice won't be a problem because AD only really runs on linked varinfo:

# make sure to use DynamicPPL with this PR and ForwardDiff@1
using DynamicPPL, Distributions, ForwardDiff, ADTypes
using DynamicPPL.TestUtils.AD: run_ad

@model f() = x ~ Wishart(7, [1 0.5; 0.5 1])
model = f()
unlinked_vi = VarInfo(model)
linked_vi = DynamicPPL.link(unlinked_vi, model)

run_ad(f(), AutoForwardDiff(); varinfo=unlinked_vi) # errors
run_ad(f(), AutoForwardDiff(); varinfo=linked_vi) # fine

Copy link
Member

Choose a reason for hiding this comment

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

relieving to know it works with linked VarInfo

@penelopeysm penelopeysm requested a review from sunxd3 May 29, 2025 11:39
Copy link
Member

@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.

Looks good

@sunxd3
Copy link
Member

sunxd3 commented May 29, 2025

I know the answer probably is "no", but do we want to update the version of benchmark https://github.com/TuringLang/DynamicPPL.jl/blob/py/forwarddiff-1/benchmarks/Project.toml ?

@penelopeysm
Copy link
Member Author

update the version of benchmark

Oh, good spot! I totally forgot about that because CompatHelper didn't open a PR for it. Thanks thanks. Let me fix that.

@penelopeysm penelopeysm added this pull request to the merge queue May 29, 2025
Merged via the queue into main with commit 2a1b650 May 29, 2025
19 of 20 checks passed
@penelopeysm penelopeysm deleted the py/forwarddiff-1 branch May 29, 2025 14:20
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.

3 participants