Skip to content

fix: reset the SMCABC budget, kernel and simulator on every call - #2128

Merged
janfb merged 3 commits into
sbi-dev:mainfrom
aniketpandey05:fix/smcabc-reset-state-per-call
Nov 15, 2026
Merged

janfb merged 3 commits into
sbi-dev:mainfrom
aniketpandey05:fix/smcabc-reset-state-per-call

Conversation

@aniketpandey05

Copy link
Copy Markdown
Contributor

What does this PR do?

SMCABC kept state from earlier calls on the same object:

  • The simulation budget. simulation_counter was set to 0 only in __init__. A
    second call started with the budget of the first already used up, so it ran the
    initial population and then stopped, without a warning.
  • The SASS simulator. With sass=True, the call replaced self._simulate_with_budget
    by a simulator that applies the SASS transform, and nothing restored it. Later calls
    kept that transform even with sass=False, and a second SASS call ran its pilot
    simulations through the old transform.
  • The kernel variance of the last population.

The budget counter is now a method instead of a closure built in __init__, and
__call__ resets the counter, the kernel variance and the simulator before it starts.

Does this close any issues?

No issue was opened, as this is a clear bug fix.

Anything else we should know?

Same object, same call twice (num_particles=100, num_initial_pop=500,
num_simulations=3000, epsilon_decay=0.5, simulator theta + 0.05 * noise):

populations final epsilon posterior std
main, call 1 5 0.061 0.048
main, call 2 1 0.986 0.501
this PR, call 2 5 0.062 0.048

On main, the second call returns the initial rejection-ABC population. MCABC does not
have this problem, because it keeps its simulator local to the call.

The new test checks a behaviour: a second call on the same object returns exactly the
samples of a fresh object with the same seed, after a first call with and without SASS.

PR #2123 also edits the start of SMCABC.__call__, so whichever of the two is merged
second will need a small rebase.

Checklist

  • I have read the contributing guide.
  • pytest -m "not slow and not gpu" tests/abc_test.py passes (32 passed). Full
    suite not run locally.
  • ruff check and ruff format pass on the changed files.
  • uv run pyright sbi passes. My local environment could not resolve torch for
    pyright; relying on CI.
  • I added test_smcabc_second_call_matches_a_fresh_instance (with and without SASS
    in the first call). Both cases fail on main and pass here.
  • I used Google-style docstrings for new or changed public functions.
  • The new tests take about 2 s each.

I used Claude Code to find this bug

SMCABC kept simulation_counter, kernel_variance and the SASS simulator
of earlier calls. A second call on the same object therefore skipped all
populations after the initial one, as its budget was already used up,
and kept applying the SASS transform of an earlier call, even with
sass=False.

Count simulations in a method instead of a closure, and reset the
counter, the kernel variance and the simulator at the start of
__call__.
@coderabbitai

coderabbitai Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

SMCABC resets its simulation counter and kernel variance at the start of each inference call. It counts parameter rows through _count_and_simulate. A regression test compares seeded results from a reused instance with results from a fresh instance.

Changes: SMCABC instance reuse

Layer / File(s) Summary
Reset call state and verify reuse
sbi/inference/abc/smcabc.py, tests/abc_test.py
Each call resets the simulation counter and kernel variance, then uses _count_and_simulate to count parameter rows before simulation. The SASS simulator also uses this method. A regression test checks that a reused instance produces the same seeded samples as a fresh instance, with or without SASS on the first call.

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

Change: Bug fix

Suggested reviewers: janfb, dgedon

Merge Risk: Minimal · up to 4f9ed

This fixes SMCABC reusing budget, kernel and simulator state across repeated calls on the same object, and a regression test covers it. No concrete merge-blocking risk was found. Rebase if PR #2123 merges first.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: SMCABC state is reset on every call, covering the simulation budget, kernel variance, and simulator. The PR objectives and code summary describe the same change, so th…
Description check Passed The description covers the problem, the fix, the reproduction numbers, the test added, and the rebase note with PR #2123. It follows the template's sections: What does this PR do, Does this close any …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: SMCABC state is reset on every call, covering the simulation budget, kernel variance, and simulator. The PR objectives and code summary describe the same change, so the title is specific and accurate. It is 64 characters, within a reasonable length for a title.

Full details: Description check

Explanation

The description covers the problem, the fix, the reproduction numbers, the test added, and the rebase note with PR #2123. It follows the template's sections: What does this PR do, Does this close any issues, Anything else we should know, and Checklist. The checklist items are filled in, and the pyright item is left unchecked with a reason given. The AI-use disclosure line appears, as the template requests. The 'Does this close any issues' section states no issue was opened, which satisfies the template's request for a link.

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

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: 9689753c-1067-4b8b-b605-7c61fc713668
Commits

Reviewing files that changed from the base of the PR and between d392685 and 4f9ed53.

Files selected for processing: 2

  • sbi/inference/abc/smcabc.py
  • tests/abc_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.

@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.28%. Comparing base (f2dc994) to head (4f9ed53).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2128      +/-   ##
==========================================
- Coverage   91.30%   91.28%   -0.03%     
==========================================
  Files         148      148              
  Lines       14970    14972       +2     
==========================================
- Hits        13669    13667       -2     
- Misses       1301     1305       +4     
Flag Coverage Δ
fast 87.66% <100.00%> (?)

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

Files with missing lines Coverage Δ
sbi/inference/abc/smcabc.py 94.19% <100.00%> (+0.04%) ⬆️

... and 5 files with indirect coverage changes

@janfb

janfb commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

@aniketpandey05 thanks for the PR! I will review it after November 9. Until then, please keep it concise, with as few changed lines as possible.

Thanks, Jan

@manuelgloeckler
manuelgloeckler self-requested a review November 11, 2026 19:25
The SASS wrapper repeated the simulation counting of _count_and_simulate; it now calls that method, so the budget is counted in one place. The reset comment is shortened to one line.

@janfb janfb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@aniketpandey05 good catch, and thanks for the clear numbers in the description! The fix looks good. I will just add a small commit on top as review capacity is limited at the moment:

  • I changed the SASS simulator to reuse your new budget counter, so the simulations are counted in one place.
  • I shortened the comment on the reset to one line.
  • I merged main into the branch.

Thanks for fixing this, Jan

@janfb
janfb merged commit cebc38c into sbi-dev:main Nov 15, 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.

2 participants