Skip to content

Instantly share code, notes, and snippets.

@supplient
Created November 17, 2022 00:05
Show Gist options
  • Save supplient/b884cf9a30fa0f58ac669de5c78966e2 to your computer and use it in GitHub Desktop.
Save supplient/b884cf9a30fa0f58ac669de5c78966e2 to your computer and use it in GitHub Desktop.
Test for taichi + pytorch to use vector operations in kernel function without deepcopy.
import torch
import taichi as ti
ti.init(arch=ti.cuda)
@ti.func
def test_dev(x: ti.types.vector(2, ti.f32)):
return 2*x
@ti.kernel
def test_kernel(x: ti.types.ndarray()):
for i in range(x.shape[0]):
v = ti.Vector([x[i, 0], x[i, 1]])
v = test_dev(v)
x[i, 0] = v[0]
x[i, 1] = v[1]
x = torch.tensor([[2, 3]], device="cuda")
test_kernel(x)
print(x)
# Out:
# tensor([[4, 6]], device='cuda:0')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment