-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
ti.real_func
parameter / return types support more complicated types?
#8037
Comments
We plan to support more types like structs, matrices and ndarrays in the future (maybe in a few months). Please stay tuned. |
Thanks! Hope to see that I've noticed that today there is a new commit by you: Support ndarray argument for real function. Seems that you are making progress there. So is it difficult for real_func |
It is a bit difficult because we handle a kernel without considering function calls inside it when performing many optimizations. There's a lot to do to let all the optimization passes support real function. |
Does import taichi as ti
@ti.dataclass
class TestStruct:
p1: float
p2: float
val: float
@ti.experimental.real_func
def compute(struct1: ti.template(), struct2: ti.template()) -> float:
p = (struct1.p1 + struct2.p2) / 2
return p
@ti.kernel
def process_struct(field1: ti.template(), field2: ti.template()):
for i in field1:
field1[i].val = compute(field1[i], field2[i])
ti.init(arch = ti.cuda)
field1 = TestStruct.field()
field2 = TestStruct.field()
ti.root.dense(ti.i, 64).place(field1, field2)
for i in range(64):
field1[i] = TestStruct(p1 = 1, p2 = 2)
field2[i] = TestStruct(p1 = 2, p2 = 1)
process_struct(field1, field2) Outputs:
|
No, expressions in the kernel can't be passed as template to the real function right now... |
One more problem: it seems that @ti.experimental.real_func
def get_struct(a: Struct) -> Struct:
return Struct(vec = vec3([a.vec[0], a.vec[1], a.vec[0] + a.vec[1]])) and @ti.kernel
def calculate():
for i, j in str_field:
struct = Struct(vec = vec3([field1[i, j], field2[i, j], field1[i, j]]))
str_field[i, j] = get_struct(struct) Then we will have exception:
|
IIRC it is already supported in the nightly version. |
Ok, thanks |
I am trying to use
ti.real_func
(which is actuallyti.experimental.real_func
), in order to accelerate compiling. I read from the documentations thatreal_func
only supports scalar inputs and return, yet I wish to return exactly two scalar digits (which should form a tuple) but I can't do that sincereal_func
requires type annotation.I also learned that we might return a struct (from this PR #7059), so I tried the following code. The code is just a simple test for input/output, but what confuses me is: When I tried to directly return a
vec3
, the compiler will tell me "unsupported return type":But I can also just wrap the
vec3
by a struct, then return it fromreal_func
decorated function and everything works. Why? This doesn't make sense to me, it seems just so meaningless to wrap thevec3
(and other "unsupported return types") by a struct as a working-around. By the way, the experimental functionalityreal_func
is released since 1.0.0 and until 1.6.0 it remains asexperimental
. I think this is a good feature, so, when willreal_func
get fully supported and moved fromexperimental
?This is my env:
Here is the code,
get_struct
throws no compilation error whileget_vec
does.The text was updated successfully, but these errors were encountered: