Skip to content

fix: restrict amortized VI to rKL only - #2074

Merged
janfb merged 2 commits into
sbi-dev:mainfrom
BHARATH0153:fix/amortized-vi-require-rkl
Oct 22, 2026
Merged

janfb merged 2 commits into
sbi-dev:mainfrom
BHARATH0153:fix/amortized-vi-require-rkl

Conversation

@BHARATH0153

@BHARATH0153 BHARATH0153 commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Restricts VIPosterior.train_amortized() to the reverse-KL objective. When self.vi_method is not "rKL", train_amortized() now raises NotImplementedError instead of silently fitting the reverse KL. The single-observation train() remains unchanged and continues to support all four vi_method values (rKL, fKL, IW, alpha).

Also updates docstrings to state that amortized training only supports rKL, and adds a regression test. This implements Option 1 as suggested by @janfb in #2073.

Does this close any issues?

Fixes #2073.

Anything else we should know?

The new test checks that train_amortized() raises NotImplementedError for fKL, IW, and alpha, while existing tests cover the rKL case.

thanks to @janfb for options

Checklist

  • I have read the contributing guide.
  • uv run pytest -n auto -m "not slow and not gpu" passes.
  • uv run pre-commit run --all-files passes (ruff and formatting).
  • uv run pyright sbi passes.
  • I added or updated tests for the changed behavior.
  • I used Google-style docstrings for new or changed public functions.

AI USAGE

Claude opus helped for the tests and for the right option
every thing tested locally

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5b9ca69e-bd65-41a0-8b43-2e9f0361f2e3
📥 Commits

Reviewing files that changed from the base of the PR and between c25a3c9 and f593bce.

📒 Files selected for processing: 4
  • docs/how_to_guide/26_variational_inference.ipynb
  • sbi/inference/posteriors/posterior_parameters.py
  • sbi/inference/posteriors/vi_posterior.py
  • tests/vi_test.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Walkthrough

train_amortized() now supports only rKL and raises NotImplementedError for other vi_method values. Documentation describes this restriction, and tests cover fKL, IW, and alpha.

Changes: Amortized VI method support

Layer / File(s) Summary
Document and enforce the supported method
sbi/inference/posteriors/vi_posterior.py, sbi/inference/posteriors/posterior_parameters.py, tests/vi_test.py, docs/how_to_guide/26_variational_inference.ipynb
The documentation identifies the reverse-KL ELBO objective and states that amortized VI supports only rKL. train_amortized() raises NotImplementedError for other methods. Tests check the error for fKL, IW, and alpha when configured on the posterior or through VIPosteriorParameters.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: janfb

Merge Risk: ⚪ Minimal · up to f593b

Amortized training now rejects unsupported VI methods as intended. No actionable merge-blocking risk is identified.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely states the main change: amortized VI is restricted to the rKL method.
Description check Passed The description includes the change summary, issue reference, implementation details, test coverage, checklist, and AI usage disclosure. It clearly notes that the full test suite and pyright checks ar…
Linked Issues check Passed Issue [#2073] requires Option 1: reject non-rKL methods in train_amortized() and document the limitation. The implementation checks the posterior method and params.vi_method, then raises `NotImp…
Out of Scope Changes check Passed The changes modify VIPosterior.train_amortized(), its regression test, and related VI documentation. These changes directly implement Issue [#2073]. No unrelated change is shown in the reviewed diff…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. (1 skipped: 1 …
Full details: Description check

Explanation

The description includes the change summary, issue reference, implementation details, test coverage, checklist, and AI usage disclosure. It clearly notes that the full test suite and pyright checks are not marked as passing.

Full details: Linked Issues check

Explanation

Issue [#2073] requires Option 1: reject non-rKL methods in train_amortized() and document the limitation. The implementation checks the posterior method and params.vi_method, then raises NotImplementedError unless the effective method is rKL. The test covers fKL, IW, and alpha through both configuration paths. The changed docstrings state the same restriction.

Full details: Out of Scope Changes check

Explanation

The changes modify VIPosterior.train_amortized(), its regression test, and related VI documentation. These changes directly implement Issue [#2073]. No unrelated change is shown in the reviewed diffs.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 3 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.04%. Comparing base (91c58f8) to head (f593bce).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2074      +/-   ##
==========================================
+ Coverage   90.03%   90.04%   +0.01%     
==========================================
  Files         142      142              
  Lines       14657    15704    +1047     
==========================================
+ Hits        13196    14141     +945     
- Misses       1461     1563     +102     
Flag Coverage Δ
fast 85.89% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sbi/inference/posteriors/posterior_parameters.py 83.33% <ø> (ø)
sbi/inference/posteriors/vi_posterior.py 85.45% <100.00%> (+0.16%) ⬆️

... and 15 files with indirect coverage changes

@BHARATH0153 BHARATH0153 changed the title fix: restrict amortized VI to rKL only (#2073) fix: restrict amortized VI to rKL only Oct 19, 2026
@BHARATH0153

Copy link
Copy Markdown
Contributor Author

@janfb could you please take a look at this PR when you have a moment thanks!

train_amortized(params=VIPosteriorParameters(vi_method="IW")) still fitted rKL silently, because params.vi_method was never read. The guard now checks it too, and the error message no longer claims the method was set at construction.
The test used the linear_gaussian_setup fixture, which trains NLE on 7000 simulations (80 s setup in fast CI) for a check that raises before any training. It now uses FakePotential and parametrizes over the method and over both ways of passing it.
Update the VIPosteriorParameters.vi_method docstring and the VI how-to guide, which still said non-rKL methods are ignored in amortized VI.
@janfb

janfb commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

@BHARATH0153 thanks for picking this up, and for posting your plan on the issue first. The check is in the right place, so I pushed one commit on top of yours to finish it.

  • There was one more silent path: passing params=VIPosteriorParameters(vi_method="IW") to train_amortized() still fitted reverse KL, because that field of params was never read. That case now raises as well.
  • The test used the linear_gaussian_setup fixture, which trains an NLE model on 7000 simulations. That adds more than a minute to every CI run, for an error that is raised before any training starts. When a check fires before training, the test does not need a trained model: the cheap FakePotential in the same file is enough. That is also why every other user of that fixture is marked slow.
  • I replaced the loop with pytest.mark.parametrize, so a failing method shows up by name in the test report.
  • I also updated the docstring of VIPosteriorParameters and the VI how-to guide, which still said that the other methods are ignored in amortized VI.

thanks for fixing this, Jan

@janfb
janfb merged commit 28cd4b3 into sbi-dev:main Oct 22, 2026
18 checks passed
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.

Amortized VI ignores vi_method

2 participants