NVIDIA / warp

A Python framework for high performance GPU simulation and graphics
https://nvidia.github.io/warp/
Other
4.28k stars 243 forks source link

[QUESTION] Question about Differentiating Material Parameters #358

Open rrzhang139 opened 1 week ago

rrzhang139 commented 1 week ago

Hello, I have a question about differentiating the tet_materials array in self.model. Specifically, I am trying to write a script that can change the k_mu parameter. I set self.model = builder.finalize(requires_grad=True) and am able to view the gradients. However, the loss does not change which indicates the gradients are not being updated properly. The object function is defined by comparing the center of mass between the target and model states. Here is my gradient update kernel:

@wp.kernel
def step_kernel(
    x: wp.array2d(dtype=wp.float32),
    grad: wp.array2d(dtype=wp.float32),
    learning_rate: float,
    param_min: float,
    param_max: float,
    param_idx: int
):
    tid = wp.tid()
    new_x = x[tid, param_idx] - learning_rate * grad[tid, param_idx]
    # if new_x < param_min:
    #     new_x = param_min
    # if new_x > param_max:
    #     new_x = param_max

    x[tid, param_idx] = new_x

Here is the entire script. https://gist.github.com/rrzhang139/29f57c89f12813ec9b969a2f200bbe6d