Fix: resolve NaN gradients in RQS out-of-bounds inverse pass#240
Merged
Conversation
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.
Fix: Resolve NaN gradients for out-of-bounds inputs in RationalQuadraticSpline
Closes #239
Description
This PR resolves a regression introduced in
v18.0.0where evaluating theRationalQuadraticSplineon out-of-bounds inputs causedNaNs in the reverse-mode gradients for the inverse pass, poisoning the gradients for the entire batch.The Issue:
Although the final output safely defaults to the identity mapping via
jnp.wherefor out-of-bounds inputs, the mathematical operations were still being evaluated on the raw, unclipped variables. In the inverse pass, this causedjnp.sqrtto evaluate a negative discriminant for out-of-bounds data. This resulted inNaNvalues that propagated through the unselected branches during backpropagation.The Fix:
jnp.cliptoyto bound it toself.intervalstrictly before computing the bin indexkand intermediate variables. The original, unclippedyis preserved solely for the finaljnp.wherefallback.xintransform_and_log_det. While the forward pass was not strictly producingNaNs (as the polynomial math happens to avoid undefined domains), applying this ensures architectural consistency and protects against potential overflows.Testing:
test_RationalQuadraticSpline_out_of_bounds_grad_nanintest_rational_quadratic_spline.py.forwardandinversepasses on out-of-bounds inputs (50.0), asserting that noNaNs exist in the resulting gradient leaves. Both passes test cleanly.