fix: flatten sample/batch dims and embed context once in CategoricalMADE.sample (#2237) #2238
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
patelshivani2283-lab
What does this PR do?
Fixes
CategoricalMADE.sample()(andCategoricalMADE.forward()) insbi/neural_nets/estimators/categorical_net.pyso that:1. Multi-dimensional conditions with
batch_dim=1work during sampling (e.g.,MNPE/MNLE/CategoricalMassEstimatorwithCNNEmbeddingwhen callingposterior.sample(..., x=x_o)for a single observation). Previously,repeat_rowsrepeated(1, H, W)along axis 0 to(num_samples, H, W), producing a 2D embedding(num_samples, emb_dim)that failed to broadcast with 3Dsamples(num_samples, 1, num_variables)insideMADE.forward().2.
use_batch_norm=Trueworks during sampling. Previously, passing 3D(num_samples, batch_dim, num_variables)intoMADEcausednn.BatchNorm1dto treatbatch_dimas 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 toMADE.forward(), crashing withAttributeError: 'CategoricalMADE' object has no attribute 'context_layer'.4. Zero-sized
sample_shape(e.g.,(0,)) and device/dtype preservation are handled cleanly, andembedding_netis evaluated once persample()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 callingMADE.Does this close any issues?
Fixes #2237.
Anything else we should know?
tests/density_estimator_test.py(including the new regression tests) run in 0.62 s total (< 0.01 s per test); fulltests/density_estimator_test.py,tests/mnle_test.py, andtests/mnpe_test.py(non-slow, non-gpu) pass locally.pyrightcould not bootstrapnodeenvin this environment, so type annotations incategorical_net.pywere verified against existing buffer/method types and will be checked by CI.Checklist
uv run pytest -n auto -m "not slow and not gpu"passes (verified ondensity_estimator_test.py,mnle_test.py,mnpe_test.py).uv run pre-commit run --all-filespasses (ruff and formatting).uv run pyright sbipasses (deferred to CI; localnodeenvunavailable).with
pytest.mark.slow.