Repository navigation
Integrate DensityEstimatorBuilder into NPE trainers - #1882
Conversation
3a6ffc5 to
914091c
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## gsoc-2026 #1882 +/- ##
============================================
Coverage ? 87.98%
============================================
Files ? 144
Lines ? 13470
Branches ? 0
============================================
Hits ? 11851
Misses ? 1619
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more.
|
janfb
left a comment
There was a problem hiding this comment.
Really good progress and the first internal integration of the new API @satwiksps 🎉
I have some typing suggestions and we need to handle MNPE a bit differently. Tests can be a bit leaner again, made some comments. But looks very good overall! 🙌
| Literal["nsf", "maf", "mdn", "made"], | ||
| _EstimatorBuilderBase, | ||
| ConditionalEstimatorBuildFn[ConditionalDensityEstimator], | ||
| ] = "maf", |
There was a problem hiding this comment.
when we leave this as string ="maf", the warning below will fire all the time becuase it's the default.
I suggest we set this to None here (and in all suclasses) and then set the default below in the if-else.
| NPE_C(prior, density_estimator="maf", show_progress_bars=False) | ||
|
|
||
|
|
||
| def test_npe_callable_no_warning(): |
There was a problem hiding this comment.
this one could be extended to cover both, the default (NPE_C()) and the build_fn approach.
| build_fn = posterior_nn(model="maf") | ||
| # Should not warn. | ||
| inference = NPE_C(prior, density_estimator=build_fn, show_progress_bars=False) | ||
| assert inference._build_neural_net is build_fn |
There was a problem hiding this comment.
the test promises to test for no warning, but then does not check it. you can do so using a filter:
import warnings
with warnings.catch_warnings():
warnings.simplefilter("error", FutureWarning) # turn any FutureWarning into a failure
inference = NPE_C(prior, density_estimator=builder, show_progress_bars=False)|
|
||
|
|
||
| @pytest.mark.parametrize("model", ("maf", "nsf")) | ||
| def test_npe_build_posterior_with_builder(model): |
There was a problem hiding this comment.
this test can be combined with the one above, just construct the posterior as well and draw samples, that way we train only once.
|
|
||
| def test_check_estimator_arg_rejects_module(): | ||
| """check_estimator_arg should reject raw nn.Module instances.""" | ||
| with pytest.raises(AssertionError): |
There was a problem hiding this comment.
should be a TypeError is you implement the above comment. please change.
janfb
left a comment
There was a problem hiding this comment.
All good! Thanks for the quick fixes 💯 @satwiksps
…v#1882) * accept DensityEstimatorBuilder in PosteriorEstimatorTrainer * add warning * update NPE_C type annotation for builder support * update NPE_B type annotation for builder support * update MNPE type annotation for builder support * add input validation and deprecation tests * add integration tests for training and sampling with estimator builders * remove BuildContext from npe_b and npe_c docstrings * BuildContext related changes in npe_base * allow _EstimatorBuilderBase in check_estimator_arg * minor import cleanup in npe_base.py * trigger ci * add an explicit loss check * default density estimator to None and update type hints in npe base * update density estimator type hints and default in npe b and c * raise Type error instead of Assertion error in check_estimator_arg * not implemented error in mnpe * assertion error to type error and adjust in one line * merge test * final cleanup for tests
What does this PR do?
This is the third PR for for the Neural Network (NN) Builder API refactor project under GSoC 2026. It integrates the
DensityEstimatorBuilder(introduced in PR #1877) into the NPE trainer hierarchy. Users can now pass a builder object toPosteriorEstimatorTrainer,NPE_B,NPE_C, andMNPEas thedensity_estimatorargument instead of a string or callable.Files Changed
Core integration (
npe_base.py)density_estimatortype union to include_EstimatorBuilderBase_wrap_builder()static method that adapts a builder object to the existing_build_neural_net(theta, x)callable interface, keeping_initialize_neural_networkunchangedFutureWarningwhen a string is passed todensity_estimator(deprecation path)Type annotation updates
npe_b.py,npe_c.py,mnpe.py: Updateddensity_estimatorparameter type annotations and docstrings to include_EstimatorBuilderBaseValidation (
user_input_checks.py)check_estimator_argto accept_EstimatorBuilderBaseinstances alongside strings and callablesTests (
npe_builder_integration_test.py) this is a new file againtest_npe_accepts_builder: parametrized over 4 modelstest_npe_string_emits_deprecation_warningtest_npe_callable_no_warningtest_npe_train_with_builder: parametrized over 3 models, trains for 2 epochstest_npe_build_posterior_with_builder: end-to-end: builder, then train, then sampletest_check_estimator_arg_accepts_valid_inputs: parametrized (builder/str/callable)test_check_estimator_arg_rejects_moduleBackward compatibility
density_estimator="maf"still works but now emits aFutureWarning. No behavior change.posterior_nn(...)or custom function works without any warning.AI Usage
Does this close any issues?
N/A
Any relevant code examples, logs, or error messages?
N/A