taichi-dev/taichi

[Lang] Make `@ti.func` callable from Python-scope, or `@ti.kfunc` if we're afraid of breaking API

Open

#1.379 geöffnet am 2. Juli 2020

Auf GitHub ansehen
 (1 Kommentar) (1 Reaktion) (0 zugewiesene Personen)C++ (2.384 Forks)batch import
feature requestgood first issuepython

Repository-Metriken

Stars
 (28.195 Stars)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

Concisely describe the proposed feature It would be convenient to unify these two concepts, both for API clarity and reducing user learning obstacle. This issue will be just a begining of this refactor.

Describe the solution you'd like (if any) We may first add @ti.kfunc as a experimental one, then replace @ti.func when it's mature and battle-tested.

  1. When @ti.func called from Python-scope will be translated into a minimal kernel that executes it, and exit.
  2. Add @ti.kfunc that create both a @ti.func and @ti.kernel, when called from corresponding scope, call func/kernel.
def kfunc(foo):
  func_foo = ti.func(foo)
  kernel_foo = ti.kernel(foo)
  @functools.wraps(foo)
  def wrapped(*args, **kwargs):
    if impl.inside_kernel():
      func_foo(*args, **kwargs)
    else:
      kernel_foo(*args, **kwargs)

Additional comments @yuanming-hu @k-ye WDYT? Inputs are welcome!

Contributor Guide