Skip to content

Preserve single-element trees in diagonal normal helpers - #157

Merged
SamDuffield merged 2 commits into
normal-computing-oss:mainfrom
keshtib07-ghost:fix/gaussian-singleton-tree
Nov 14, 2026
Merged

SamDuffield merged 2 commits into
normal-computing-oss:mainfrom
keshtib07-ghost:fix/gaussian-singleton-tree

Conversation

@keshtib07-ghost

Copy link
Copy Markdown
Contributor

Fixes #156.

The diagonal normal helpers use total tree size to decide whether a mean or scale is scalar. A dictionary holding one scalar tensor is then passed to torch.tensor, which raises RuntimeError: Could not infer dtype of dict. A singleton tuple can be converted, but that drops the parameter gradients. This also prevents diagonal VI from training a one-parameter dictionary model.

Use the existing is_scalar helper for scalar broadcasting so single-element parameter trees keep their structure and gradients. The docstrings clarify the distinction.

Validation on Python 3.12.14 and PyTorch 2.15.0+cpu:

  • The new selection gives 16 failures and 14 passes on the original implementation.
  • pytest tests/test_utils.py tests/test_tree_utils.py -q: 57 passed with the fix.
  • Regression coverage checks dictionary and tuple structures, scalar and length-one tensor leaves, mixed scalar/tree arguments, normalized and unnormalized log probabilities, sample shapes and gradients against torch.distributions.Normal.
  • A single-parameter diagonal VI model completed three updates with finite parameters and NELBO for n_samples=1 and 3, with each STL setting.
  • Repository-wide Ruff lint and formatting passed with version 0.13.1; git diff --check passed.

The full sampling suite is not part of this completed validation.

Fixes normal-computing-oss#156.

Signed-off-by: Keshav Tibrewal <keshav.tibrewal@iitgn.ac.in>

@SamDuffield SamDuffield left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, thanks!

Edit: Sorry wanted to clean up the docs a bit

Comment thread posteriors/utils.py Resolved
Comment thread posteriors/utils.py Resolved
Signed-off-by: Keshav Tibrewal <keshav.tibrewal@iitgn.ac.in>

@keshtib07-ghost keshtib07-ghost left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review! I've removed both redundant docstring comments in 94079f9. The latest CI run is green, including the tests and pre-commit checks.

Could you take another look when you get a chance?

@SamDuffield
SamDuffield merged commit 54700da into normal-computing-oss:main Nov 14, 2026
2 checks passed
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.

Diagonal normal helpers mistake single-parameter trees for scalars

2 participants