Skip to content

Posterior sampling methods may ignore constructor-configured sampling limits #2075

Closed

Description

@shivansh023023

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

  1. added
    bug
    on Oct 17, 2026
  2. BHARATH0153 commented on Oct 18, 2026

    @BHARATH0153
    Contributor

    The bug is true.I Verified for DirectPosterior with a runtime harness: constructing with max_sampling_batch_size=500 and calling sample() without overriding passes 10000 to the downstream rejection sampler. Explicit override (max_sampling_batch_size=777) still works.
    cause sample() and sample_batched() default the parameter to 10000 instead of None. The fallback self.max_sampling_batch_size if max_sampling_batch_size is None else max_sampling_batch_size never 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 default 10000, sample_batched() line ~233 default 10000
    • sbi/inference/posteriors/importance_posterior.py: sample() line ~193 default 10000

    RejectionPosterior already uses Optional[int] = None and is correct. Minimal fix: change those defaults to None. The instance default (10_000) remains when neither is provided.

  3. shivansh023023 commented on Oct 18, 2026

    @shivansh023023
    ContributorAuthor

    The bug is true.I Verified for DirectPosterior with a runtime harness: constructing with max_sampling_batch_size=500 and calling sample() without overriding passes 10000 to the downstream rejection sampler. Explicit override (max_sampling_batch_size=777) still works. cause sample() and sample_batched() default the parameter to 10000 instead of None. The fallback self.max_sampling_batch_size if max_sampling_batch_size is None else max_sampling_batch_size never 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 default 10000, sample_batched() line ~233 default 10000
    • sbi/inference/posteriors/importance_posterior.py: sample() line ~193 default 10000

    RejectionPosterior already uses Optional[int] = None and is correct. Minimal fix: change those defaults to None. The instance default (10_000) remains when neither is provided.

    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.

  4. added a commit that references this issue on Oct 27, 2026
    a569524
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions