Skip to content

Add MixedDensityEstimatorBuilder for MNLE and MNPE - #1912

Merged
janfb merged 10 commits into
sbi-dev:gsoc-2026from
satwiksps:nn-builder-refactor-pr-four
Jul 4, 2026
Merged

janfb merged 10 commits into
sbi-dev:gsoc-2026from
satwiksps:nn-builder-refactor-pr-four

Conversation

@satwiksps

@satwiksps satwiksps commented Jul 1, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

This PR continues the GSoC 2026 Neural Network Builder API refactor by introducing MixedDensityEstimatorBuilder (a sibling to DensityEstimatorBuilder) and wiring it into the MNLE and MNPE trainers with string deprecation.

Our main design choice is introducing the continuous_model field instead of relying on model or flow_model. The old string API named the method rather than the estimator, burying the flow model in kwargs. Now, we can explicitly pass MixedDensityEstimatorBuilder(continuous_model="nsf"). We chose the name continuous_model because the supported architectures include non-flow models like MDN and MADE.

Files Changed

estimator_configs.py

  • Added _VALID_MIXED_CONTINUOUS_MODELS to mirror mixed_nets.model_builders.
  • Introduced the MixedDensityEstimatorBuilder dataclass, which defaults continuous_model to "nsf" and handles validation in __post_init__.
  • It captures mixed-specific fields (like num_categories_per_variable and log_transform_x) and forwards flow-specific fields to the continuous sub-net.
  • The build() method translates continuous_model to flow_model and dispatches to _build_mixed_density_estimator.

mnpe.py

  • Removed the NotImplementedError for builder instances and updated the density_estimator default from "mnpe" to None.
  • Added MixedDensityEstimatorBuilder to the type union and set up dispatching: passing None builds silently, while passing "mnpe" raises a FutureWarning and falls back to posterior_nn.
  • The builder now routes through super().__init__() to assign proper NPE roles (input=θ, condition=x).

mnle.py

  • Updated the density_estimator default to None and added MixedDensityEstimatorBuilder to the type union.
  • Follows the same dispatch pattern as MNPE: passing "mnle" triggers a FutureWarning and falls back to likelihood_nn.
  • Routes the builder through super().__init__() to assign the correct NLE roles (input=x, condition=θ).

npe_nle_builder_integration_test.py

We preserved all existing tests and added several new ones for the mixed builder:

  • Verified that using the default None or passing a builder instance runs without warnings, while passing old string arguments correctly emits a FutureWarning.
  • Added a ValueError check for invalid continuous_model inputs.
  • Included end-to-end training tests for both MNLE and MNPE to ensure they successfully build and assert the MixedDensityEstimator.
  • Updated test_check_estimator_arg_accepts_valid_inputs to explicitly include the new builder.

AI Usage

  • Used Grammarly for PR description refinement and VS Code Copilot for code and bug fix suggestions.
  • GPT OSS via Antigravity added docstrings, comments and handeled many cosmetic changes.

Does this close any issues?

N/A

Any relevant code examples, logs, or error messages?

N/A

@satwiksps
satwiksps force-pushed the nn-builder-refactor-pr-four branch from 575564a to 3541dca Compare July 2, 2026 05:20
@satwiksps
satwiksps marked this pull request as ready for review July 2, 2026 06:45
@codecov

codecov Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.03%. Comparing base (587f922) to head (5eb1153).
⚠️ Report is 19 commits behind head on gsoc-2026.

Additional details and impacted files
@@              Coverage Diff              @@
##           gsoc-2026    #1912      +/-   ##
=============================================
+ Coverage      87.99%   88.03%   +0.03%     
=============================================
  Files            144      144              
  Lines          13499    13563      +64     
=============================================
+ Hits           11879    11940      +61     
- Misses          1620     1623       +3     
Flag Coverage Δ
fast 81.62% <100.00%> (?)

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

Files with missing lines Coverage Δ
sbi/inference/trainers/nle/mnle.py 97.05% <100.00%> (+4.46%) ⬆️
sbi/inference/trainers/npe/mnpe.py 97.05% <100.00%> (+7.05%) ⬆️
sbi/neural_nets/net_builders/estimator_configs.py 100.00% <100.00%> (ø)

@janfb
janfb self-requested a review July 2, 2026 07:16
@satwiksps

Copy link
Copy Markdown
Collaborator Author

Hi @janfb
Apologies for adding changes after marking PR Ready for Review.
These recent 2 commits parametrize some existing tests and minor import style fixes only.

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

Looks good! Only one (unlikely) user path is not covered yet, I made a suggestion to fix it, please double check.

Well done! 🚀

Comment thread sbi/inference/trainers/npe/mnpe.py Resolved
return d


_VALID_MIXED_CONTINUOUS_MODELS = frozenset({

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.

this is good to have, but it could drift away from the actually supported models we have in the factory. I suggest adding a short tests that fails if they drift, e.g., sth like:

def test_valid_continuous_models_match_builders():
    from sbi.neural_nets.net_builders.mixed_nets import model_builders
    from sbi.neural_nets.net_builders.estimator_configs import (
        _VALID_MIXED_CONTINUOUS_MODELS,
    )
    assert _VALID_MIXED_CONTINUOUS_MODELS == frozenset(model_builders)


def build(
self, batch_input: Tensor, batch_condition: Tensor
) -> ConditionalDensityEstimator:

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.

the return type here should be narrower because the factories will aways return a MixedDensityEstimator.

@satwiksps
satwiksps requested a review from janfb July 2, 2026 17:09

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

Looks good, well done! 👍

mixed-type data. The continuous-component model is selected via
``continuous_model``; there is no ``model`` field.
"""

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.

add a comment here: density-estimator model for the continuous variables.

@janfb
janfb merged commit 9373587 into sbi-dev:gsoc-2026 Jul 4, 2026
17 checks passed
Jocho-Smith pushed a commit to Jocho-Smith/sbi that referenced this pull request Jul 20, 2026
…#1912)

* add MixedDensityEstimatorBuilder without build

* add build() and changes related to updated plan

* integrate new builder to mnle and mnpe

* add new tests to existing npe nle test file

* make imports single line

* some test parametrizations and cleanup

* add TypeError to fail fast

* add TypeError test

* add test to catch drift

* narrow down the return type
janfb pushed a commit that referenced this pull request Aug 5, 2026
Ports #1912, which was reviewed and merged into the `gsoc-2026`
branch. See that pull request for the full description.

(cherry picked from commit 9373587)
janfb pushed a commit that referenced this pull request Aug 5, 2026
Ports #1912, which was reviewed and merged into the `gsoc-2026`
branch. See that pull request for the full description.

(cherry picked from commit 9373587)
@satwiksps
satwiksps deleted the nn-builder-refactor-pr-four branch August 7, 2026 10:48
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