-
Notifications
You must be signed in to change notification settings - Fork 313
Fix numerical issue in PRB integrator #1818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DoeringChristian
wants to merge
10
commits into
master
Choose a base branch
from
prb-replace-grad
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+22
−24
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
lnuic
approved these changes
Dec 12, 2025
Contributor
lnuic
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Contributor
|
Example of a scene that causes the issue. In this configuration, re-evaluating BSDF * cos(theta) differentiably at
returns 0.0, which later causes NaN values in the gradients. import mitsuba as mi
mi.set_variant("cuda_ad_rgb")
integrator_prb = mi.load_dict({
"type": "prb",
})
scene_dict = {
'type': 'scene',
'perpendicular_plane': {
'type': 'obj',
'filename': 'mitsuba3/resources/data/common/meshes/rectangle.obj',
'to_world': mi.ScalarTransform4f.translate([0, 0, 1]),
'flip_normals': True,
'bsdf': {
'type': 'conductor'
}
},
'parallel_plane': {
'type': 'obj',
'filename': 'mitsuba3/resources/data/common/meshes/rectangle.obj',
'to_world': mi.ScalarTransform4f.translate([0, 1, 0]).scale(2).rotate([1, 0, 0], 90),
},
'light': {
'type': 'constant'
},
'sensor': {
'type': 'perspective',
'to_world': mi.ScalarTransform4f().look_at(origin=[0, 0, 0], target=[0, 0, 1], up=[0, 1, 0]),
'film': {
'type': 'hdrfilm',
'rfilter': { 'type': 'box' },
'width': 128,
'height': 128,
'sample_border': True,
'pixel_format': 'rgb',
'component_format': 'float32',
},
}
}
scene = mi.load_dict(scene_dict)
scene_params = mi.traverse(scene)
dr.enable_grad(scene_params["perpendicular_plane.vertex_positions"])
img = mi.render(scene, scene_params, integrator=integrator_prb, spp=1)
dr.backward(img)
print(dr.grad(scene_params["perpendicular_plane.vertex_positions"])) |
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.
Description
This PR fixes a numerical issue in the PRB integrator gradients. When multiplying
bsdf_val / dr.detach(bsdf_val)withJ / dr.detach(J)inside thereplace_gradfunction, the gradient will be expanded according to the product rule. Therefore, the differential ofbsdf_val / dr.detach(bsdf_val)will be multiplied withJ / dr.detach(J)which should be 1. However with numerical instabilities, this might not be the case. In this PR we are multiplying the values outside thereplace_grad. Dr.Jit will therefore know to use 1 regardless of the actual value.Fixes # (issue)
Testing
Checklist
cuda_*andllvm_*variants. If you can't test this, please leave below