Skip to content

fix(flow): avoid float32 cancellation in x + sqrt(1 + x**2) - #375

Open
aaronanish-t wants to merge 1 commit into
pymc-devs:mainfrom
aaronanish-t:fix/float32-exp-asinh
Open

aaronanish-t wants to merge 1 commit into
pymc-devs:mainfrom
aaronanish-t:fix/float32-exp-asinh

Conversation

@aaronanish-t

Copy link
Copy Markdown

normalizing_flow.py computes x + sqrt(1 + x**2) in several places. For large negative x the two terms cancel, so in float32 the result loses precision and then becomes 0. That causes both issues:

Changes

Testing

  • Without the fix, 3 of the new tests fail: Contract2 at -1000 and -10000, and make_flow at small scale. With the fix, all 8 pass.
  • pytest tests/test_pymc.py -m flow passes both before and after.
  • The issue reproducers now give the expected values: Contract2 returns -10000 for -10000, and the log-determinant at scale 1e-4 is -18.4207.
  • ruff check and ruff format --check are clean.
  • Not covered: I tested on the released 0.16.11 package with only normalizing_flow.py replaced, not a full source build, and I didn't run the Stan flow test.

Closes #363
Closes #364

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.

Contract2 returns -inf for large negative inputs in float32 Flow scale parameters underflow to 0 in float32 for posterior scales of 1e-4

1 participant