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

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

Open
Jianghanxiao opened this issue Dec 13, 2024 · 0 comments
Assignees
Labels
docs Improvements or additions to documentation

Comments

@Jianghanxiao
Copy link

Jianghanxiao commented Dec 13, 2024

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

@Jianghanxiao Jianghanxiao added the bug Something isn't working label Dec 13, 2024
@shi-eric shi-eric added docs Improvements or additions to documentation and removed bug Something isn't working labels Dec 13, 2024
@shi-eric shi-eric changed the title [BUG] The gradient issue with no error reminder when passing torch tensor into kernal in differentiable setting [DOCS] The gradient issue with no error reminder when passing torch tensor into kernal in differentiable setting Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants