← Back to Pull requests

fix: flatten sample/batch dims and embed context once in CategoricalMADE.sample (#2237) #2238

Open
unknown opened this pull request with 350 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 26, 2026 06:25 UTC
Author

What does this PR do?

Fixes CategoricalMADE.sample() (and CategoricalMADE.forward()) in sbi/neural_nets/estimators/categorical_net.py so that:

1. Multi-dimensional conditions with batch_dim=1 work during sampling (e.g., MNPE / MNLE / CategoricalMassEstimator with CNNEmbedding when calling posterior.sample(..., x=x_o) for a single observation). Previously, repeat_rows repeated (1, H, W) along axis 0 to (num_samples, H, W), producing a 2D embedding (num_samples, emb_dim) that failed to broadcast with 3D samples (num_samples, 1, num_variables) inside MADE.forward().
2. use_batch_norm=True works during sampling. Previously, passing 3D (num_samples, batch_dim, num_variables) into MADE caused nn.BatchNorm1d to treat batch_dim as the channel axis and crash.
3. Unconditional CategoricalMADE (num_context_features=None, context=None) works. Previously, sample() created a dummy CPU zero tensor and passed it to MADE.forward(), crashing with AttributeError: 'CategoricalMADE' object has no attribute 'context_layer'.
4. Zero-sized sample_shape (e.g., (0,)) and device/dtype preservation are handled cleanly, and embedding_net is evaluated once per sample() call at its native (batch_dim, *condition_shape) shape instead of once per autoregressive variable.

This mirrors CategoricalMassEstimator.log_prob(), which already flattens (sample_dim, batch_dim) into a single batch axis before calling MADE.

Does this close any issues?

Fixes #2237.

Anything else we should know?

  • All 41 mixed/categorical tests in tests/density_estimator_test.py (including the new regression tests) run in 0.62 s total (< 0.01 s per test); full tests/density_estimator_test.py, tests/mnle_test.py, and tests/mnpe_test.py (non-slow, non-gpu) pass locally.

  • Local pyright could not bootstrap nodeenv in this environment, so type annotations in categorical_net.py were verified against existing buffer/method types and will be checked by CI.

  • AI assistance was used to help diagnose the root cause, draft the fix and regression tests, and run local verification.
  • Checklist

  • I have read the contributing guide.

  • uv run pytest -n auto -m "not slow and not gpu" passes (verified on density_estimator_test.py, mnle_test.py, mnpe_test.py).

  • uv run pre-commit run --all-files passes (ruff and formatting).

  • uv run pyright sbi passes (deferred to CI; local nodeenv unavailable).

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

    Diff: 350 lines (+262 / -88) across 4 files Reviewer: patelshivani2283-lab Review duration: 4252m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.42) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 350 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.351
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #2238"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.4% (MEDIUM) · Higher = larger blast radius
    42.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.6% · Safe to merge
    22.6%
    Repository