Skip to content

[DOCS] The gradient issue with no error reminder when passing torch tensor into kernal in differentiable setting #394

@Jianghanxiao

Description

@Jianghanxiao

Bug Description

Image
Following the document, it's a bit misleading. At the start, I think "cannot handle" means something like "requires_grad=False" for the torch tensor passing to the kernel. However, after constructing my own stuffs, I find it surprisingly modify the original value of the torch tensor. Now I understand that "cannot handle" means that if using this way in a differentiable setting, there will definitely be a bug. But there should be some error message for this if such operations are weird.

Here is a minimum test example to show the above problems

import torch
import warp as wp


@wp.kernel
def test_kernel(
    x: wp.array(dtype=wp.vec3), y: wp.array(dtype=wp.vec3), z: wp.array(dtype=wp.vec3)
):
    tid = wp.tid()
    z[tid] = x[tid] + y[tid]


wp.init()
wp.set_device("cuda:0")

x = torch.ones((10, 3), dtype=torch.float32, device="cuda")
y = torch.ones((10, 3), dtype=torch.float32, device="cuda")
wp_y = wp.from_torch(y, dtype=wp.vec3, requires_grad=True)
z = torch.zeros((10, 3), dtype=torch.float32, device="cuda")
wp_z = wp.from_torch(y, dtype=wp.vec3, requires_grad=True)

tape = wp.Tape()

with tape:
    wp.launch(test_kernel, dim=10, inputs=[x, wp_y], outputs=[wp_z])

print(x) # Output all 1

tape.backward(grads={wp_z: wp.ones_like(wp_z)})

print(x) # Output all 2

And here is the output
Image

Hope this can help others who misunderstand the document and don't have any reported error messages or warnings

System Information

No response

Metadata

Metadata

Assignees

Labels

docsImprovements or additions to documentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions