Repository navigation
Description
Bug description
Several posterior classes appear to ignore sampling limits configured during initialization when the corresponding arguments are omitted from a sampling-method call.
For example, a DirectPosterior initialized with max_sampling_batch_size=500 appears to forward 10000 to the downstream sampler when sample() is called without explicitly passing max_sampling_batch_size.
The constructor value is stored on the instance, but the sampling method's default argument appears to take precedence instead of falling back to the instance configuration.
This behavior seems inconsistent with RejectionPosterior.sample(), which uses the constructor-configured value when the method-level argument is omitted.
Expected behavior
- If a sampling limit is configured during initialization, it should be used when the corresponding method argument is omitted.
- An explicitly supplied method-level argument should override the constructor value.
- Existing defaults should remain unchanged when neither value is configured.
- Related sampling and batched-sampling paths should follow the same contract where applicable.
Initial code inspection and targeted runtime checks suggest this may affect DirectPosterior, ImportanceSamplingPosterior, VectorFieldPosterior, and FilteredDirectPosterior. NPE_A_Posterior may inherit the same behavior through DirectPosterior.
The runtime checks used stubbed downstream sampling functions; I have not verified every affected path through the full integration test suite.
I would like to confirm the intended configuration contract with the maintainers before implementing a narrowly scoped fix and regression tests.
Minimal code example
# Illustrative example; requires a valid prior and posterior setup.
posterior = DirectPosterior(
..., # required constructor arguments
max_sampling_batch_size=500,
)
# Observed in a targeted harness: downstream sampler receives 10000.
posterior.sample(...)
# An explicit override appears to work.
posterior.sample(..., max_sampling_batch_size=777)
Expected: the first sampling call should use `500`; the second should use `777`.Error message or traceback
No exception or traceback. This appears to be a configuration-handling bug: the sampling method may silently use its default instead of the value supplied during initialization.
sbi version
0.27.0 (exit=0)
Python version
Python 3.13.14
Device
CPU
Inference method
Not method-specific
Single-round or multi-round
Single-round (amortized)
Additional context
The repository was inspected without making source changes. A targeted runtime harness with stubbed downstream sampling functions reproduced the behavior for DirectPosterior and ImportanceSamplingPosterior, and the RejectionPosterior implementation appeared to honor its constructor-configured value.
The full test suite could not be run because the audit environment lacked some project dependencies. Therefore, the affected paths listed above are initial findings and should be confirmed with regression tests.
The proposed fix would distinguish omitted method arguments from explicit overrides, use the instance configuration when an argument is omitted, and preserve existing defaults, batch-size safeguards, and warnings.
I have not made code changes or opened a pull request. I am requesting feedback on the expected behavior and scope before implementation.
Activity
BHARATH0153 commentedon Oct 18, 2026
The bug is true.I Verified for DirectPosterior with a runtime harness: constructing with
max_sampling_batch_size=500and callingsample()without overriding passes10000to the downstream rejection sampler. Explicit override (max_sampling_batch_size=777) still works.cause
sample()andsample_batched()default the parameter to10000instead ofNone. The fallbackself.max_sampling_batch_size if max_sampling_batch_size is None else max_sampling_batch_sizenever runs when the argument is omitted, so the constructor value is ignored. ImportanceSamplingPosterior has the same pattern.Affected:
sbi/inference/posteriors/direct_posterior.py:sample()line ~146 default10000,sample_batched()line ~233 default10000sbi/inference/posteriors/importance_posterior.py:sample()line ~193 default10000RejectionPosterior already uses
Optional[int] = Noneand is correct. Minimal fix: change those defaults toNone. The instance default (10_000) remains when neither is provided.shivansh023023 commentedon Oct 18, 2026
Thanks for verifying this and confirming the root cause! I'll investigate the affected sampling paths and prepare a focused PR that preserves explicit method-level overrides while honoring constructor-configured values when arguments are omitted. I'll also add regression tests for both behaviors and check the existing defaults and batch-size safeguards.
fix: use constructor sampling arguments when sample() omits them (sbi…