Skip to content

refactor: import configs only from sbi.neural_nets, remove build_zuko_cnf, merge z-score embedding helpers - #2162

Open
dgedon wants to merge 4 commits into
sbi-dev:mainfrom
dgedon:refactor/nn-builder-leftovers
Open

dgedon wants to merge 4 commits into
sbi-dev:mainfrom
dgedon:refactor/nn-builder-leftovers

Conversation

@dgedon

@dgedon dgedon commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Removes leftovers of the NN builder refactor before 0.28. The four commits are independent, so each can be dropped on its own.

  1. Configs only from sbi.neural_nets. The per-model configs were also re-exported from sbi.neural_nets.net_builders. The docs, the API reference and sbi itself only use sbi.neural_nets, and the second path was never released. net_builders now exports only build functions.
  2. Remove build_zuko_cnf. CNF left the registry during Zuko density estimators (#1088) #1116, but its builder stayed. posterior_nn("zuko_cnf") raises NotImplementedError at build time, and nothing calls the builder. build_zuko_flow("CNF", ...) builds the same network (same weights and log-probs) and stays tested.
  3. Make build_context internal. ZScoreConfig, ZScoreStats, BuildContext and compute_z_score_stats have no consumer yet. Following Decision 10 of the NN builder design, they stay private until the planned z-score pre-pass uses them: the module becomes sbi.neural_nets._build_context. It was never released. @satwiksps, this settles the "unused seam" item of your post-GSoC plan. If you'd rather remove the module, that is one commit.
  4. One z-score embedding helper. Seven places prepended a z-scoring layer to an embedding net with the same code. build_z_scored_embedding_net moves unchanged to sbi.utils.sbiutils, because the restriction estimator can't import from sbi.neural_nets, and every builder now calls it. Each call stays where the old code was. The old classifier import path and build_input_layer keep working.

Release note: build_zuko_cnf (shipped since v0.23) is removed; use sbi.neural_nets.net_builders.flow.build_zuko_flow("CNF", ...). No other behaviour changes.

Does this close any issues?

No issue. It is cleanup from the NN builder refactor (#1872 to #1997).

Anything else we should know?

Checklist

  • I have read the contributing guide.
  • uv run pytest -n auto -m "not slow and not gpu" passes.
  • uv run pre-commit run --all-files passes (ruff and formatting).
  • uv run pyright sbi passes.
  • I added or updated tests for the changed behavior.
  • I used Google-style docstrings for new or changed public functions.
  • (If applicable) I reported how long new tests run and marked slow ones with pytest.mark.slow. (Not applicable: the new tests take milliseconds.)

🤖 Generated with Claude Code

dgedon and others added 4 commits November 10, 2026 15:30
The per-model config classes were re-exported from both
`sbi.neural_nets` and `sbi.neural_nets.net_builders`. The docs, the API
reference and sbi itself only use `sbi.neural_nets`, and the second
path was never released. `net_builders` now exports only the build
functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CNF was dropped from the model registry when the Zuko flows were added
in sbi-dev#1116, but its builder stayed. `posterior_nn("zuko_cnf")` raises
`NotImplementedError` when it builds, and nothing in sbi calls the
builder. `build_zuko_flow("CNF", ...)` builds the same network and stays
tested.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`ZScoreConfig`, `ZScoreStats`, `BuildContext` and `compute_z_score_stats`
have no consumer yet. As decided in the NN builder design (Decision 10),
they stay internal until the planned z-score pre-pass uses them. Rename
the module to `sbi.neural_nets._build_context`, so 0.28 does not ship
them as public API. The module was never released.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Seven places prepended a z-scoring layer to an embedding net with the
same code: `build_z_scored_embedding_net` in the classifier builders,
`_prepare_y_embedding` in the flow builders, `build_input_layer` in the
restriction estimator, and inline copies in the MDN, categorical, mixed
and vector-field builders.

Move `build_z_scored_embedding_net` unchanged into `sbi.utils.sbiutils`,
next to `standardizing_net`, and use it everywhere. It lives in
`sbi.utils` because the restriction estimator cannot import from
`sbi.neural_nets` without a circular import. The classifier module
imports it back, and `build_input_layer` keeps its signature, so both
released names keep working. Every call keeps its position, so the
embedding net still runs at the same points of each build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The pull request adds a shared helper for z-score embedding setup and updates builder and restriction-estimator call sites to use it. It also removes selected configuration re-exports and the Zuko CNF builder.

Changes: Z-score embedding setup

Layer / File(s) Summary
Shared helper and validation
sbi/utils/sbiutils.py, sbi/neural_nets/net_builders/classifier.py, tests/sbiutils_test.py, sbi/neural_nets/_build_context.py
build_z_scored_embedding_net now parses z-score modes and composes a standardizing network with the supplied embedding network when enabled. Tests cover supported modes and compatibility imports. The build-context documentation now describes the types as internal and not yet used by builders.
Builder and input-layer integration
sbi/neural_nets/net_builders/categorial.py, sbi/neural_nets/net_builders/flow.py, sbi/neural_nets/net_builders/mdn.py, sbi/neural_nets/net_builders/mixed_nets.py, sbi/neural_nets/net_builders/vector_field_nets.py, sbi/utils/restriction_estimator.py, tests/sbiutils_test.py
These builder and input-layer paths now use the shared helper instead of local z-score parsing and network composition.
Builder API removals
sbi/neural_nets/net_builders/__init__.py, sbi/neural_nets/net_builders/flow.py
The package no longer re-exports estimator and vector-field configuration classes. The Zuko CNF builder was removed.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

  • sbi-dev/sbi#1987: Documents estimator configuration classes and builder APIs related to the exports and builder removed here.

Suggested reviewers: janfb, satwiksps, anastasiakrouglova

Merge Risk: Minimal · up to dc1f4

The intended API cleanup has no identified break in repository workflows. The PR is mergeable after normal checks.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title names the main changes: config exports, removal of build_zuko_cnf, and consolidation of z-score embedding helpers. It is longer than preferred but remains clear and specific.
Description check Passed The description follows the template and explains the changes, issue status, test results, compatibility details, and AI use. It completes the checklist and explains why the test-timing item does not …
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description follows the template and explains the changes, issue status, test results, compatibility details, and AI use. It completes the checklist and explains why the test-timing item does not apply.

Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 12 functions across 11 files.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 134d7b46-fe75-4901-9c22-7db0f348f29e
Commits

Reviewing files that changed from the base of the PR and between c36f471 and dc1f46e.

Files selected for processing: 12

  • sbi/neural_nets/_build_context.py
  • sbi/neural_nets/net_builders/__init__.py
  • sbi/neural_nets/net_builders/categorial.py
  • sbi/neural_nets/net_builders/classifier.py
  • sbi/neural_nets/net_builders/flow.py
  • sbi/neural_nets/net_builders/mdn.py
  • sbi/neural_nets/net_builders/mixed_nets.py
  • sbi/neural_nets/net_builders/vector_field_nets.py
  • sbi/utils/restriction_estimator.py
  • sbi/utils/sbiutils.py
  • tests/build_context_test.py
  • tests/sbiutils_test.py
Files with no reviewable changes: 1

  • sbi/neural_nets/net_builders/init.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.59%. Comparing base (c36f471) to head (dc1f46e).

Files with missing lines Patch % Lines
sbi/neural_nets/net_builders/flow.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2162      +/-   ##
==========================================
+ Coverage   90.38%   90.59%   +0.21%     
==========================================
  Files         142      143       +1     
  Lines       14884    14899      +15     
==========================================
+ Hits        13453    13498      +45     
+ Misses       1431     1401      -30     
Flag Coverage Δ
fast 87.47% <94.44%> (?)

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

Files with missing lines Coverage Δ
sbi/neural_nets/_build_context.py 97.56% <ø> (ø)
sbi/neural_nets/net_builders/__init__.py 100.00% <ø> (ø)
sbi/neural_nets/net_builders/categorial.py 95.65% <100.00%> (-0.35%) ⬇️
sbi/neural_nets/net_builders/classifier.py 100.00% <ø> (ø)
sbi/neural_nets/net_builders/mdn.py 96.15% <100.00%> (-0.28%) ⬇️
sbi/neural_nets/net_builders/mixed_nets.py 95.55% <100.00%> (-0.28%) ⬇️
sbi/neural_nets/net_builders/vector_field_nets.py 95.07% <100.00%> (+0.81%) ⬆️
sbi/utils/restriction_estimator.py 80.41% <100.00%> (-3.03%) ⬇️
sbi/utils/sbiutils.py 90.31% <100.00%> (+0.11%) ⬆️
sbi/neural_nets/net_builders/flow.py 98.58% <83.33%> (+1.28%) ⬆️

... and 36 files with indirect coverage changes

This branch has not been deployed

No deployments
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