Fix product reduction gradient when an element is zero - #528
Open
lnuic wants to merge 1 commit into
Open
Conversation
Member
|
Hi Lovro -- are you sure about needing new backend operations? Dr.Jit supports |
wjakob
force-pushed
the
master
branch
7 times, most recently
from
August 22, 2026 03:50
60b72ec to
23ef9cd
Compare
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.
The gradient of a product should be the product of the other entries, but we
computed it as
result / value, which breaks when an entry is zero.dr.prodcaught the divide by zero and returned 0, which is the wrong answer: for[2, 0, 4]the gradient should be[0, 8, 0]and previous result was[0, 0, 0].The axis/block version didn't check at all and returned NaN.
The cost is two extra reductions in the backward pass, so a product backward is about 3x slower:
prodblock_reduceprodblock_reduceThat could be brought back to a single pass by combining the two reductions, but
it needs a new reduction primitive in drjit-core, which felt like too much for
this one fix. I am happy to add that if needed.
If the slowdown isn't worth it I am happy to leave the gradient approximate and
just guard the block version so it returns 0 instead of NaN.