Skip to content

fix: keep all sample dims in the base sample_and_log_prob - #2126

Draft
janfb wants to merge 1 commit into
mainfrom
fix/sample-and-log-prob-sample-shape
Draft

janfb wants to merge 1 commit into
mainfrom
fix/sample-and-log-prob-sample-shape

Conversation

@janfb

@janfb janfb commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Summary

sample_and_log_prob returns wrong shapes or fails for the MDN, the mixed estimator of MNLE and MNPE, and the categorical estimator. These estimators inherit the generic method of ConditionalDensityEstimator. It hands the samples to log_prob, which accepts a single sample dimension.

The fix flattens the sample dimensions for log_prob and restores them on the log probabilities. The nflows, zuko and TabPFN overrides already do this.

 sample_and_log_prob(sample_shape, condition)
   samples = sample(sample_shape, condition)        # (*sample_shape, batch, *event)
-  log_probs = log_prob(samples, condition)          # breaks for len(sample_shape) > 1
+  log_probs = log_prob(samples.reshape(n, batch, *event), condition)
+  log_probs = log_probs.reshape(*sample_shape, batch)
   return samples, log_probs

Posteriors are unaffected: they draw sample_shape.numel() samples and reshape the result themselves. The bug hits users who call the estimator API directly, for example in their own sampling or training code.

Evidence

Shape of the log probabilities with four conditions:

Call Before After
MDN, sample_shape=(2, 3) RuntimeError (broadcasting) (2, 3, 4)
MNLE, MNPE or categorical, sample_shape=() (1, 4) (4,)

The new test runs MDN, MNLE, MNPE and the categorical estimator with sample shapes (), (3,), (2, 3) and one or four conditions. It checks the shapes and that each log probability belongs to its sample. On main, 13 of its 24 cases fail; on this branch all pass.

Merge Danger

Door: two-way

Blast Radius: small

Only the generic method changes. For the MDN, calls with a single sample dimension give the same result as before. Code that relied on the extra leading dimension of MNLE log probabilities with sample_shape=() sees (batch,) now, as for every other estimator.

ConditionalDensityEstimator.sample_and_log_prob passed samples of shape (*sample_shape, batch, *event) straight to log_prob, which takes a single sample dimension. With a multi-dimensional sample_shape and more than one condition this raised a broadcasting error, e.g. for the MDN with sample_shape (2, 3) and four conditions. With sample_shape () the MNLE, MNPE and categorical estimators returned log probabilities of shape (1, batch) instead of (batch,).

Flatten the sample dimensions for log_prob and restore them on the log probabilities, as the nflows, zuko and TabPFN overrides already do. The estimators that inherit the base method get the fix: the MDN, its NPE-A wrapper ProposalCorrectedMDN, the mixed estimator of MNLE and MNPE, and the categorical estimator. Posteriors are unaffected: they draw sample_shape.numel() samples and never call this method with several sample dimensions.

The new test runs the inheriting builders (MDN, MNLE, MNPE, categorical) and checks the shapes and that each log probability belongs to its sample; 13 of its 24 cases fail without the fix.
@coderabbitai

coderabbitai Bot commented Oct 30, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

Comment @coderabbitai help to get the list of available commands.

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