Skip to content

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

Open
aniketpandey05 wants to merge 1 commit into
sbi-dev:mainfrom
aniketpandey05:fix/smcabc-reset-state-per-call
Open

aniketpandey05 wants to merge 1 commit 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

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: aa8a076e-b123-467e-ab22-10056aed4ee7
Commits

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

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.

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

Priority: Normal

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

Change: Bug fix

Suggested reviewers: janfb, manuelgloeckler, dgedon

Merge Risk: Minimal · up to d3926

No actionable merge-blocking risk is established; the changed reuse behavior is ready for normal validation.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: resetting SMCABC state on every call, including the budget, kernel, and simulator.
Description check Passed The description follows the repository template. It explains the bug, implementation, testing, checklist status, known verification limitation, and AI assistance. The unchecked pyright item is explici…
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 with cyclomatic complexity of at least 2. Documentat…
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: Description check

Explanation

The description follows the repository template. It explains the bug, implementation, testing, checklist status, known verification limitation, and AI assistance. The unchecked pyright item is explicitly explained, and the full suite limitation is disclosed.

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 with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 2 files.

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 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.70%. Comparing base (a36c5f5) to head (d392685).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2128      +/-   ##
==========================================
+ Coverage   90.68%   90.70%   +0.01%     
==========================================
  Files         142      142              
  Lines       14864    14867       +3     
==========================================
+ Hits        13480    13485       +5     
+ Misses       1384     1382       -2     
Flag Coverage Δ
fast 86.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/abc/smcabc.py 94.23% <100.00%> (+0.07%) ⬆️

... and 1 file 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

This branch has not been deployed

No deployments
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