Skip to content

Update to the [email protected] interface #2506

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

Open
wants to merge 43 commits into
base: main
Choose a base branch
from
Open

Conversation

Red-Portal
Copy link
Member

@Red-Portal Red-Portal commented Mar 14, 2025

This PR aims to update Turing's Variational module to match AdvancedVI's new interface starting from v0.3. I will try not to change the interface too much, but given the new features in AdvancedVI, I think breaking changes will be inevitable. Though the focus will be to provide a good default setting rather than to expose all the features.

Currently proposed interface:

using Turing
using AdvancedVI

d = randn(100)
Turing.@model function model()
   x ~ MvNormal(randn(100), 1)
   y ~ InverseGamma()
end
m = model()
q = Turing.Variational.q_fullrank_gaussian(Random.default_rng(), m)
n_iters = 1000
Turing.Variational.vi(m, q, n_iters)

Closes #2507
Closes #2508
Closes #2430

Red-Portal and others added 12 commits March 14, 2025 19:09
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@Red-Portal Red-Portal marked this pull request as draft March 14, 2025 23:23
Red-Portal and others added 5 commits March 14, 2025 19:29
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@yebai
Copy link
Member

yebai commented Mar 20, 2025

@Red-Portal, can you fix the tests before I take a look?

@Red-Portal
Copy link
Member Author

@yebai I marked the PR as a draft so that we can first agree on an interface, and then I flesh out the implementation and the tests. Do we wish we proceed in another way?

@yebai
Copy link
Member

yebai commented Mar 20, 2025

Let's address the interface later or in a separate PR since that might require more discussions. For this PR, let's try to keep the VI interface non-breaking where possible.

Red-Portal and others added 3 commits March 29, 2025 14:08
@yebai
Copy link
Member

yebai commented Apr 18, 2025

@Red-Portal can you take a look at the following error:

ERROR: LoadError: UndefVarError: `turnprogress` not defined in `AdvancedVI`

@Red-Portal Red-Portal marked this pull request as ready for review April 20, 2025 05:26
Copy link

codecov bot commented Apr 21, 2025

Codecov Report

Attention: Patch coverage is 60.29412% with 27 lines in your changes missing coverage. Please review.

Project coverage is 82.68%. Comparing base (05110bd) to head (f62e7b8).

Files with missing lines Patch % Lines
src/variational/deprecated.jl 0.00% 16 Missing ⚠️
src/variational/VariationalInference.jl 78.84% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2506      +/-   ##
==========================================
- Coverage   84.27%   82.68%   -1.59%     
==========================================
  Files          21       21              
  Lines        1456     1461       +5     
==========================================
- Hits         1227     1208      -19     
- Misses        229      253      +24     

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

Copy link
Member

@yebai yebai left a comment

Choose a reason for hiding this comment

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

Thanks @Red-Portal. I left some comments below.

One high-level comment: I suggest we unify vi_fullrank_gaussian and vi_meanfield_gaussian into a single function, q_distribution(...; gaussian=true|false, fullrank=true|false), to reduce code redundancy.

Note on CI errors:

  • CI compains about missing ADVI() and TruncatedADAGrad().

# Use linked `varinfo` to determine the correct number of parameters.
# TODO: Replace with `length` once this is implemented for `VarInfo`.
varinfo_linked = DynamicPPL.link(varinfo, model)
num_params = length(varinfo_linked[:])
Copy link
Member

@yebai yebai Apr 21, 2025

Choose a reason for hiding this comment

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

Can we get the dimentionality via num_params = length(varinfo_linked) instead of length(varinfo_linked[:])?

cc @mhauru

Copy link
Member Author

Choose a reason for hiding this comment

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

This currently doesn't work, so I'll leave it as is for now

Copy link
Member

Choose a reason for hiding this comment

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

Once VarInfo will be based on VarNamedVector we will have a function called length_internal for this purpose. We can swap that in here once the VarInfo makeover is done.

@yebai yebai removed the request for review from torfjelde April 21, 2025 11:13
@Red-Portal
Copy link
Member Author

Sorry for the delay! I've been traveling in the past weeks, but will start working on this now

@coveralls
Copy link

coveralls commented Apr 29, 2025

Pull Request Test Coverage Report for Build 15099815167

Details

  • 41 of 68 (60.29%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-1.6%) to 82.683%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/variational/VariationalInference.jl 41 52 78.85%
src/variational/deprecated.jl 0 16 0.0%
Totals Coverage Status
Change from base Build 14925677192: -1.6%
Covered Lines: 1208
Relevant Lines: 1461

💛 - Coveralls

yebai and others added 2 commits May 8, 2025 14:08
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link
Contributor

github-actions bot commented May 8, 2025

Turing.jl documentation for PR #2506 is available at:
https://TuringLang.github.io/Turing.jl/previews/PR2506/

@Red-Portal Red-Portal changed the title Update to the [email protected] interface Update to the [email protected] interface May 14, 2025
@mhauru
Copy link
Member

mhauru commented May 19, 2025

I'm confused at the CI failure of test/mcmc/Inference.jl. It seems unrelated to this PR. I'm unable to reproduce it locally. The checks are just a tiny bit outside our allowed margin of error, but I also just find it troubling that different AD backends are giving different results at all. Maybe our seed fixing is failing. I'll rerun CI to see if it's deterministic.

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.

AdvancedVI 0.3 and compatibility with Turing.jl
4 participants