Repository navigation
fix(flow): avoid float32 cancellation in x + sqrt(1 + x**2) - #375
Open
aaronanish-t wants to merge 1 commit into
Open
aaronanish-t wants to merge 1 commit into
aaronanish-t wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
normalizing_flow.pycomputesx + sqrt(1 + x**2)in several places. For large negativexthe two terms cancel, so in float32 the result loses precision and then becomes 0. That causes both issues:-inffor -10000.make_flow's log-determinant is-infat scale 1e-4.Changes
_exp_asinh(x). It computes the same value,exp(asinh(x)), but forx < 0it uses1 / (|x| + sqrt(1 + x**2)), which doesn't cancel.Parameterizescales and thetasUnconstrainedAffine's scalesigma_mod, its forwardzand its inversewsinh/asinhrewrite from Contract2 returns -inf for large negative inputs in float32 #364 isn't needed. Happy to switch to that rewrite if you'd prefer it.tests/test_normalizing_flow.py, markedflowso it runs in the flow CI job. It covers:make_flow's log-determinant staying consistent at scales 1, 1e-2 and 1e-4Testing
make_flowat small scale. With the fix, all 8 pass.pytest tests/test_pymc.py -m flowpasses both before and after.ruff checkandruff format --checkare clean.normalizing_flow.pyreplaced, not a full source build, and I didn't run the Stan flow test.Closes #363
Closes #364