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

Petition to keep ti.pyfunc #7324

Closed
AmesingFlank opened this issue Feb 8, 2023 · 2 comments · Fixed by #7530
Closed

Petition to keep ti.pyfunc #7324

AmesingFlank opened this issue Feb 8, 2023 · 2 comments · Fixed by #7530
Assignees
Labels
feature request Suggest an idea on this project
Milestone

Comments

@AmesingFlank
Copy link
Collaborator

AmesingFlank commented Feb 8, 2023

I understand that the plan is to remove ti.pyfunc in 1.6.0, but I found this to be a very important feature which I use all the time, so I'd like to make a case for keeping it in taichi.

In #4414, @lin-hitonami explained that even without ti.pyfunc, you can still create functions that work in both taichi scope and python scope using the following pattern:

def fmod_python(a, b):
    return a-ti.floor(a/b)*b

fmod_taichi = ti.func(fmod_python)

While this does work for the simplest scenarios, there are many use cases that this pattern cannot support:

  • Multiple functions that calls each other. For example:
xs = ti.field(float, shape=100)

@ti.pyfunc
def f(i):
    return xs[i] + 1

@ti.pyfunc
def g(i):
    return f(i) + xs[i] + 1

@ti.kernel
def k():
    print(g(3))

k()

The solution proposed in #4414 does not work here:

xs = ti.field(float, shape=100)

def f(i):
    return xs[i] + 1

def g(i):
    return f(i) + xs[i] + 1

@ti.kernel
def k():
    print(ti.func(g)(3))

k()

because in ti.func(g), the f function that it calls is not transformed by ti.func, so this triggers an error:

  File "c:\users\ldfra\code\taichi\taichi\python\taichi\lang\util.py", line 308, in wrapped
    assert in_python_scope(), \
AssertionError: __getitem__ cannot be called in Taichi-scope
  • member functions of @ti.data_oriented classes:
    Consider for example:
@ti.data_oriented
class C:
    @ti.pyfunc
    def f(self, i):
        return i + 1

c = C()

print(c.f(1))

@ti.kernel
def k():
    print(c.f(1))

k()

this would be very awkward without ti.pyfunc. The only solution I see is by doing

@ti.data_oriented
class C:
    def f(self, i):
        return i + 1

@ti.kernel
def k():
    print(ti.func(C.f)(c, 1))

which is quite bad in terms of readability

  • member functions struct types (@ti.dataclass). This is the same as with ti.data_oriented classes, because members of struct types are also callable from both taichi and python scope.

These are all very common use cases, and not having ti.pyfunc really makes writing large-scale taichi programs a lot more awkward. So please consider keeping ti.pyfunc. Alternatively, I would also greatly appreciate if you could help me identify other patterns that could solve all of the three aforementioned problems!

cc @lin-hitonami @strongoier @k-ye @ailzhang @yuanming-hu

@AmesingFlank AmesingFlank added the feature request Suggest an idea on this project label Feb 8, 2023
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Feb 8, 2023
@ailzhang ailzhang moved this from Untriaged to Todo in Taichi Lang Feb 10, 2023
@ailzhang
Copy link
Contributor

ailzhang commented Feb 22, 2023

Hey @AmesingFlank thanks for bringing this up! We discussed this offline with @strongoier and @lin-hitonami and totally agreed this is a valid use case! We'll keep pyfunc and raise better error message when an invalid pyfunc is written/called instead. Cheers!

@AmesingFlank
Copy link
Collaborator Author

Hey @AmesingFlank thanks for bringing this up! We discussed this offline with @strongoier and @lin-hitonami and totally agreed this is a valid use case! We'll keep pyfunc and raise better error message when an invalid pyfunc is written/called instead. Cheers!

That's awesome, thank you!

@lin-hitonami lin-hitonami added this to the v1.5.0 milestone Mar 10, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in Taichi Lang Mar 10, 2023
lin-hitonami added a commit that referenced this issue Mar 10, 2023
Issue: fixes #7324

### Brief Summary
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Suggest an idea on this project
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants