Skip to content

vi_method="IW" silently runs the plain ELBO (IWElboOptimizer.loss_rsample is never called) #2065

Closed

Description

@BHARATH0153

Bug description

vi_method="IW" is supposed to minimize the importance-weighted ELBO (IWELBO). VIPosterior(..., vi_method="IW") (and the deprecated build_posterior(..., vi_method="IW") path) pass this through to IWElboOptimizer, whose docstring and arguments state that K samples per particle (n_particles * K in total) and dreg (doubly-reparameterized gradient) control that objective.

Instead the optimizer silently minimizes the plain reverse-KL ELBO. vi_method="IW" therefore behaves identically to vi_method="rKL", and both K and dreg have no effect.

Root cause: the training loop calls self.loss(x_o) -> self._loss(x_o) (sbi/samplers/vi/vi_divergence_optimizers.py:263 and :253). IWElboOptimizer (:496) defines the correct objective in loss_rsample (:542) but never overrides _loss, so _loss resolves to the inherited ElboOptimizer._loss (:451) -> _compute_elbo_loss (:454), the unweighted ELBO. loss_rsample (:542) and get_importance_weight (:560) have no callers anywhere in the package.

The sibling Renyi optimizer (:704) does override _loss and dispatches correctly, so only IW is affected.

Expected: vi_method="IW" minimizes the IWELBO of loss_rsample, honoring K and dreg.

Suggested fix: override _loss in IWElboOptimizer to return self.loss_rsample(x_o) (with a regression test that the IWELBO path is taken).

Minimal code example

from sbi.samplers.vi.vi_divergence_optimizers import ElboOptimizer, IWElboOptimizer

print("inherits the plain ELBO:", IWElboOptimizer._loss is ElboOptimizer._loss)
print("resolves to:", IWElboOptimizer._loss.__qualname__)
print("defines loss_rsample:", "loss_rsample" in IWElboOptimizer.__dict__)
print("defines its own _loss:", "_loss" in IWElboOptimizer.__dict__)

Observed output

inherits the plain ELBO: True
resolves to: ElboOptimizer._loss
defines loss_rsample: True
defines its own _loss: False

No exception is raised, so the switch from the IWELBO to the plain ELBO is silent. A repo-wide git grep -n "loss_rsample" returns only the definition at line 542, i.e. the IWELBO objective is never executed, and K/dreg are ignored.

sbi version

0.27.0

Python version

3.12.13

Device

CPU

Inference method

Other (Variational Inference)

Single-round or multi-round

Not applicable

Additional context

  • The affected file is unchanged on current main.
  • Affected entry points: VIPosterior(..., vi_method="IW") and the deprecated build_posterior(..., vi_method="IW"), for any likelihood-based method.
  • The dreg=True branch also never runs; it only sets stick_the_landing.

Activity

  1. BHARATH0153 commented on Oct 14, 2026

    @BHARATH0153
    ContributorAuthor

    Found: vi_method="IW" is broken — IWElboOptimizer never overrides _loss, so its loss_rsample (vi_divergence_optimizers.py:542) is dead code and training silently runs the plain ELBO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions