Skip to content

Add DensityEstimatorBuilder with build() dispatch#1877

Draft
satwiksps wants to merge 16 commits into
sbi-dev:gsoc-2026from
satwiksps:nn-builder-refactor-pr-two
Draft

Add DensityEstimatorBuilder with build() dispatch#1877
satwiksps wants to merge 16 commits into
sbi-dev:gsoc-2026from
satwiksps:nn-builder-refactor-pr-two

Conversation

@satwiksps

Copy link
Copy Markdown
Contributor

What does this PR do?

This is the second PR for for the Neural Network (NN) Builder API refactor project under GSoC 2026. It adds DensityEstimatorBuilder which is the first concrete builder class in the NN builder refactor. It provides a typed, inspectable replacement for the posterior_nn()/likelihood_nn() factory closures, covering all 14 continuous density estimator model types.

Builds on PR #1872 which introduced BuildContext, ZScoreConfig, ZScoreStats, and the _EstimatorBuilderBase contract.

Files Changed

sbi/neural_nets/net_builders/estimator_configs.py

  • Added DensityEstimatorBuilder(_EstimatorBuilderBase) dataclass with:
    • model: Literal[...] field selecting the architecture (maf, nsf, mdn, made, maf_rqs, and all Zuko variants)
    • Typed fields for all shared and model-specific parameters (hidden_features, num_transforms, num_bins, embedding_net, etc.)
    • __post_init__ validation: unknown model names fail immediately at construction
    • build(context, batch_theta, batch_x): dispatches to the appropriate existing build_* function
    • _build_kwargs(): collects non-None fields + extra_kwargs, excluding model (used only for dispatch)
  • Added DENSITY_MODELS type alias and _VALID_DENSITY_MODELS set for validation

sbi/neural_nets/net_builders/__init__.py

  • Exported DensityEstimatorBuilder

tests/density_estimator_builder_test.py (created new file, or may be we can also add to existing test files ?)

  • 43 tests covering construction, validation, to_dict, build dispatch for all 14 models, custom features, embedding nets, loss computation, and z-score options

AI Usage

  • GPT-OSS 120B (via Antigravity 2.0): Used to add docstrings and a few more tests.
  • Grammarly: Used to refine and improve the grammar of this PR description.
  • VS Code auto-complete: Used some suggested code and comments.
  • Gemini 3.1 Pro (via Antigravity 2.0): Used to scan for any bugs or architectural errors, code review prior push.

Does this close any issues?

N/A

Any relevant code examples, logs, or error messages?

N/A

@satwiksps

satwiksps commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

This is a stacked PR, so opening as a Draft. Once the PR #1872 is merged into a new GSoC specific branch, I will rebase this branch and mark it Ready for review.

@satwiksps satwiksps changed the base branch from main to gsoc-2026 June 9, 2026 13:00
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.

1 participant