Skip to content

fix(flow): avoid NaN gradients in DipBij's inverse - #377

Open
aaronanish-t wants to merge 5 commits into
pymc-devs:mainfrom
aaronanish-t:fix/dipbij-nan-grad
Open

aaronanish-t wants to merge 5 commits into
pymc-devs:mainfrom
aaronanish-t:fix/dipbij-nan-grad

Conversation

@aaronanish-t

Copy link
Copy Markdown

This is stacked on #375, because both add tests to tests/test_normalizing_flow.py. Only the last commit is new. I'll rebase onto main once #375 is merged.

DipBij.inverse_and_log_det solves a cubic with Cardano's formula, z = cbrt(-Q/2 + sqrt(delta)) + cbrt(-Q/2 - sqrt(delta)). Where P = 0, which is at y = ±sqrt(3 * (1 - a)), one of the two cube root arguments cancels to zero. The gradient of cbrt is infinite there, so the gradient of the inverse is nan. With a = 0 that's at y = sqrt(3), as in the issue.

Changes

  • The inverse now takes only the cube root whose argument can't cancel, s = cbrt(-Q/2 + sign(-Q/2) * sqrt(delta)), and gets the other root from s1 * s2 = -P / 3, so z = s - P / (3 * s).
  • Added test_dipbij_inverse_gradient_float32 for b in -1, 0 and 1. It checks points at and around ±sqrt(3 * (1 - a)): that transform(inverse(y)) returns y, and that the gradient of the inverse equals 1 / f'(x).

Testing

  • Without the fix, the gradient is nan at 1.7305, sqrt(3) and 1.7345 in float32, and the 3 new tests fail. With the fix, the gradients there are 1.0000 and all tests in the file pass (11).
  • Over 401 points near sqrt(3) in float64, the gradient was non-finite at all 401 before the fix and at none after it.
  • pytest tests/test_pymc.py -m flow passes.
  • ruff check and ruff format --check are clean.
  • Not covered: as in fix(flow): avoid float32 cancellation in x + sqrt(1 + x**2) #375, I tested on the released package with only normalizing_flow.py replaced, not a full source build.

Closes #361

@ricardoV94

Copy link
Copy Markdown
Member

I reckon we need more extensive tests

aaronanish-t and others added 3 commits November 14, 2026 21:28
Cover b from -3 to 3 in float32 and float64, a dense grid of y, large values and points next to y = +-sqrt(3 * (1 - a)). Check the round trip, the log-det, and the derivatives of the inverse and its log-det with respect to y and b against the forward map.
…1 at y = 0

Both Cardano terms are zero there, so s - P / (3 * s) was 0 / 0.
@aaronanish-t

Copy link
Copy Markdown
Author

@ricardoV94 I extended the tests in tests/test_normalizing_flow.py, in both float32 and float64:

  • Round trip of DipBij's inverse for b from -3 to 3, over a dense grid, large values, and points at and next to y = ±sqrt(3(1 - a)).
  • Gradients of x and the log-det with respect to y and b, checked against the implicit function theorem on the forward map.
  • DipBij inside a Planar layer as make_flow builds it, with gradients for y and every trained parameter checked against finite differences.
  • Saturated b, where tanh(b) rounds to 1. That case showed the new inverse returned NaN at y = 0 (main returns 0), so 65a5c03 guards it.

With the fix all 49 tests pass, and 37 fail on main.

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.

DipBij.inverse_and_log_det gives NaN gradients near y = sqrt(3) in float32

2 participants