Skip to content
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

Unable to directly compute texture gradient on SceneParameters object #1493

Open
dvicini opened this issue Feb 15, 2025 · 0 comments
Open

Unable to directly compute texture gradient on SceneParameters object #1493

dvicini opened this issue Feb 15, 2025 · 0 comments
Assignees

Comments

@dvicini
Copy link
Member

dvicini commented Feb 15, 2025

It used to be possible to call dr.enable_grad on any differentiable scene parameter, call params.update(), render and compute gradients of the respective parameters.

This is currently no longer possible, the offending commit seems to be af5aaa6. I haven't looked into the details yet, but it might be related to the enabling of packet instructions for textures and the associated padding. Something in that logic prevents gradients from being accumulated at the original value in the scene parameters object.

The following reproducer now prints False, but prior to af5aaa6 prints True:

import drjit as dr
import mitsuba as mi
import numpy as np

mi.set_variant('llvm_ad_rgb')

scene = mi.load_dict({
    'type': 'scene',
    'integrator': {'type': 'direct'},
    'sensor': {
        'type': 'perspective',
        'to_world': mi.ScalarTransform4f().look_at(
            [0, 0, 5], [0, 0, 0], [0, 1, 0]
        ),
    },
    'emitter': {'type': 'constant'},
    'mesh': {
        'type': 'cube',
        'bsdf': {
            'type': 'diffuse',
            'reflectance': {
                'type': 'bitmap',
                'bitmap': mi.Bitmap(np.random.rand(16, 16, 3).astype(np.float32)),
                "format": "variant"
            },
        },
    },
})

scene_parameters = mi.traverse(scene)
key = 'mesh.bsdf.reflectance.data'
scene_parameters.keep([key])
dr.enable_grad(scene_parameters[key])
scene_parameters.update()

dr.backward(dr.mean(mi.render(scene, params=scene_parameters, spp=4)))
grad = dr.grad(scene_parameters[key])
print("Non-zero gradient? ", dr.any(grad != 0))

It's possible to work around this problem by creating a new tensor and re-assigning it:

texture = mi.TensorXf(scene_parameters[key])
dr.enable_grad(texture)
scene_parameters[key] = texture
...
grad = dr.grad(texture)

However, it's still confusing that directly enabling gradients on the SceneParameters object does not work for textures, but keeps working for all other parameters.

@dvicini dvicini added bug Something isn't working and removed bug Something isn't working labels Feb 15, 2025
@rtabbara rtabbara self-assigned this Feb 18, 2025
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

No branches or pull requests

2 participants