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 55 commits into
base: breaking
Choose a base branch
from

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

@mhauru
Copy link
Member

mhauru commented May 20, 2025

The reason different AD backends give different results is numerical instability in HMCDA (maybe other HMC methods too). Here's the absolute value of the difference between ForwardDiff and ReverseDiff in the first 1000 values sampled by HMCDA
image
The instability may in particular have something to do with the adaption phase (which is where these samples are from). Unsure about that. With a shorter adaption period the different AD backends don't diverge.

It's not a very satisfying conclusion, but I think it does mean that we can relax about different AD backends giving different results, and also about a couple of them missing our target tolerance by a small amount. I'll increase the tolerance.

I wonder if test models with nicer posteriors would not have this issue.

@mhauru
Copy link
Member

mhauru commented May 20, 2025

@Red-Portal, hopefully if you merge main into this, the mcmc/Inference test failures will go away. I'll leave the merge to you though, since there are conflicts.

@Red-Portal Red-Portal requested a review from yebai May 21, 2025 21:02
@Red-Portal
Copy link
Member Author

@yebai I think the code is good to go on my end. How should we proceed in terms of documentation? Is this tutorial all that I need to update?

@yebai
Copy link
Member

yebai commented May 21, 2025

Is this tutorial all that I need to update?

I think so, cc @penelopeysm, who might know other related docs pages requiring update.

Copy link
Member

@penelopeysm penelopeysm left a comment

Choose a reason for hiding this comment

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

The main TuringLang docs can't be edited until you've released a version of Turing that contains these fixes, so that's going to be a later PR.

First of all the API docs in docs/src/api.md will need to be edited. This is pretty much just a table that lists the identifiers that Turing re-exports, plus a link to the original docs (in this case AdvancedVI docs).

Then, this should be a minor version release, right? For Turing and DynamicPPL, we keep a breaking branch that we merge breaking changes into - the point is to aggregate breaking changes so that we don't have an overly frequent stream of minor releases.

Could you change the base branch of this PR to point to that, and edit the changelog accordingly? It might be nice to give a bit of a summary of what's new in AdvancedVI 0.4, although feel free to just point people to the API docs for the details.

@yebai yebai changed the base branch from main to breaking May 23, 2025 09:15
Red-Portal and others added 11 commits May 23, 2025 17:34
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>
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
5 participants