You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Recently there have been recurring issues centered around calling Taichi-scope methods in Python-scope. For example, #1051 and #1338, #1379. Just to name a few.
Here I provide an AST transform-based approach, which I've been considering for a while (but not until today did I get a chance to write it down here....)
Note that we are already using the AST transform-based approach for @ti.kernel and @ti.func in Taichi-scope. This proposal simply extends that approach to handle calling @ti.func in Python-scope.
What prevents us from calling ti.func's in Python-scope?
It may seem that many ti.funcs are directly callable in Python. This is indeed true for simple functions (e.g. no branching, no mutable variables). However, this may not be the case for more complex functions, such as Matrix.__matmul__.
As a compiled language, Taichi does have subtle differences from raw Python. For example, its local variables are statically typed:
@ti.funcdeffoo():
a=int(0)
a=0.4print(a) # 0, instead of 0.4
Another difference would be that Taichi uses lexical scoping yet Python uses function scoping:
@ti.funcdefbar():
ifTrue:
a=1print(a) # SyntaxError: a is no longer available here
How to fix these incompatibilities?
For Taichi-scope kernels and functions, we have already successfully fixed these issues using an AST transformer (in transformer.py): the bar function above will be transformed into
Similarly, we will be able to directly call @ti.func in Python-scope, if we have a PythonTransformer that translates @ti.func into an AST that is directly runnable by the Python interpreter.
deffoo(x, y)
a=variable(0) # integer local varifx>0:
b=variable(y*2)
a.set_val(b.val+1.5) # convert to integerdelb# Ensure Taich lexical scopingelse:
a.set_val(x)
returna.val
The transformed function is directly executable by the Python interpreter. This enables an interpretation mode of @ti.func. As long as we implement the PythonTransformer, then all the @ti.func can run in both Python- and Taichi-scope.
I think many existing attempts (e.g., invoking functions that get dispatched to different implementations depending on if we are in Taichi- or Python- scope) are already close to this approach (i.e., dispatching functions to Taichi/Python-scope AST transformers.)
Transformer-based dispatching is the ultimate solution.
The text was updated successfully, but these errors were encountered:
What's the point of running ti.func in Python-scope? I feel this is a kind of overkill. IMHO when ti.func is called from Python-scope, it should behave like a kernel, if with type-hints.
(Continuing the discussions in #1338 (review))
Recently there have been recurring issues centered around calling Taichi-scope methods in Python-scope. For example, #1051 and #1338, #1379. Just to name a few.
Here I provide an AST transform-based approach, which I've been considering for a while (but not until today did I get a chance to write it down here....)
Note that we are already using the AST transform-based approach for
@ti.kernel
and@ti.func
in Taichi-scope. This proposal simply extends that approach to handle calling@ti.func
in Python-scope.What prevents us from calling
ti.func
's in Python-scope?It may seem that many
ti.func
s are directly callable in Python. This is indeed true for simple functions (e.g. no branching, no mutable variables). However, this may not be the case for more complex functions, such asMatrix.__matmul__
.As a compiled language, Taichi does have subtle differences from raw Python. For example, its local variables are statically typed:
Another difference would be that Taichi uses lexical scoping yet Python uses function scoping:
How to fix these incompatibilities?
For Taichi-scope kernels and functions, we have already successfully fixed these issues using an AST transformer (in
transformer.py
): thebar
function above will be transformed intoSimilarly, we will be able to directly call
@ti.func
in Python-scope, if we have aPythonTransformer
that translates@ti.func
into an AST that is directly runnable by the Python interpreter.For example,
Should be transformed (by
PythonTransformer
) intoThe transformed function is directly executable by the Python interpreter. This enables an
interpretation mode
of@ti.func
. As long as we implement thePythonTransformer
, then all the@ti.func
can run in both Python- and Taichi-scope.I think many existing attempts (e.g., invoking functions that get dispatched to different implementations depending on if we are in Taichi- or Python- scope) are already close to this approach (i.e., dispatching functions to Taichi/Python-scope AST transformers.)
Transformer-based dispatching is the ultimate solution.
The text was updated successfully, but these errors were encountered: