Fix CbrtOp derivative NaN gradient on negative inputs (#2571)#2575
Open
kimjune01 wants to merge 1 commit into
Open
Fix CbrtOp derivative NaN gradient on negative inputs (#2571)#2575kimjune01 wants to merge 1 commit into
kimjune01 wants to merge 1 commit into
Conversation
The CbrtOp AD rule computed d/dx cbrt(x) via pow(x, -2/3), which is NaN for negative bases, while stablehlo.cbrt is real and smooth there (cbrt(-8) = -2). So the primal was correct but the gradient was poisoned to NaN for all x < 0. Compute from the real primal instead: d/dx = DiffeRet / (3 * cbrt(x)^2), mirroring jax.lax.cbrt's JVP. Dropping the power op entirely means the fix cannot reproduce the failure. Update the diff-rule lit test to sample negative inputs, which the old rule failed on (true gradients are sign-independent, so the expected magnitudes are unchanged).
This was referenced Jun 22, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2575 +/- ##
==========================================
+ Coverage 28.62% 31.01% +2.38%
==========================================
Files 225 225
Lines 44316 44438 +122
==========================================
+ Hits 12686 13782 +1096
+ Misses 31630 30656 -974 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Author
|
@wsmoses only red here is the A100 job, and it died with a Malt.TerminatedWorkerException in nn/convolution and nn/scatter_gather (looks like a cuDNN/OOM crash, unrelated to this cbrt gradient change). CPU and TPU are both green. mind kicking off a re-run when you get a sec? |
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.
Fixes #2571 (maintainer-approved: "PR also welcome here!").
Problem
The
CbrtOpAD rule (HLODerivatives.td) computes the gradient as(1/3) * pow(x, -2/3). A non-integer exponent on a negative base is NaN (StableHLOpowerspec:power([-36.0], [1.1]) = -nan), butstablehlo.cbrtis real and smooth forx < 0(cbrt(-8) = -2). So for finitex < 0the primal is correct but the gradient is NaN, where the true derivative1/(3*cbrt(x)^2)is a finite real.Fix
Compute the derivative from the already-real primal, dropping the
powerop entirely:This mirrors
jax.lax.cbrt's JVP (g * (1/3) * integer_pow(ans, -2)). Because the failure came from routing throughpoweron a negative base, removingpowermeans the fix cannot reproduce it. Verified againstjax.grad(jnp.cbrt)across the sign range (float64):jax.gradpow(x,-2/3)/31/(3*cbrt^2)At
x = 0the derivative is a genuine singularity (+/-inf), the same boundarySqrtOpalready special-cases.Test
test/lit_tests/diffrules/stablehlo/cbrt.mlir's@mainnumerically checks the gradient understablehlo-translate --interpret, but only sampled positive inputs. Flipped to[-8.0, -27.0]: true gradients are sign-independent so%expectedis unchanged, but the old rule produced NaN there. The FORWARD/REVERSE FileCheck blocks are updated to the new (power-free) IR.Found mechanically with a value-and-gradient soundness gate over the rewrite/AD-rule library; happy to share the process if useful.