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

Struct argument to kernel has no function attributes #7822

Closed
oliver-batchelor opened this issue Apr 16, 2023 · 2 comments · Fixed by #7865
Closed

Struct argument to kernel has no function attributes #7822

oliver-batchelor opened this issue Apr 16, 2023 · 2 comments · Fixed by #7865
Assignees

Comments

@oliver-batchelor
Copy link
Contributor

A struct passed into a kernel as an argument doesn't have function attributes as a normal struct does, an example showing the difference below.

import taichi as ti
ti.init()

@ti.dataclass
class Foo:
    x: ti.f32
    y: ti.f32

    @ti.func
    def add(self, other:ti.template()):
        return Foo(self.x + other.x, self.y + other.y )

@ti.kernel
def foo(x:Foo):
  print(x.add(x).x)

@ti.kernel
def bar():
  x = Foo(1, 2)
  print(x.add(x).x) #Works

bar() # Works
foo(Foo(1, 2))   # Fails - Struct2 has no attribute 'add'
[Taichi] version 1.6.0, llvm 15.0.4, commit cc4cacfa, linux, python 3.8.16
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "test_types.py", line 23, in <module>
    bar() # Works
  File "/home/oliver/mambaforge/envs/conan_ros/lib/python3.8/site-packages/taichi/lang/kernel_impl.py", line 1038, in wrapped
    raise type(e)('\n' + str(e)) from None
taichi.lang.exception.TaichiTypeError: 
File "test_types.py", line 20, in bar:
  print((x + x).x) #Works
         ^^^^^
unsupported operand type(s) for +: 'Struct3' and 'Struct3'
@bobcao3
Copy link
Collaborator

bobcao3 commented Apr 18, 2023

Ah... this is something funny. Struct arguments are compiled into IR struct type which does not have encapsulation. We might need to support UFCS to make such things like member methods work in Taichi. @ailzhang @lin-hitonami

@lin-hitonami
Copy link
Contributor

Maybe we can implement this feature in the AST transformer.

@ailzhang ailzhang moved this from Untriaged to Todo in Taichi Lang Apr 21, 2023
lin-hitonami added a commit that referenced this issue Apr 23, 2023
…rn value (#7865)

Issue: fixes #7822

<!--
copilot:all
-->
### <samp>🤖 Generated by Copilot at 4ae87d7</samp>

### Summary
🧪🛠️🚀

<!--
1. 🧪 - This emoji represents testing, experimentation, or science, and
can be used to indicate the addition of test cases or the verification
of functionality.
2. 🛠️ - This emoji represents tools, construction, or repair, and can be
used to indicate the addition of a new feature or the improvement of an
existing one.
3. 🚀 - This emoji represents speed, launch, or innovation, and can be
used to indicate the enhancement of performance, the expansion of
capabilities, or the introduction of a novel idea.
-->
This pull request enables data classes defined by `ti.dataclass` to have
methods decorated by `ti.func` and `ti.kernel`. It also adds tests to
check the functionality and correctness of this feature.

> _`StructType` creates the data class of doom_
> _With `__struct_methods` to unleash its power_
> _Call them from Python, or pass them to `ti.kernel`_
> _`ti.func` returns them, the ultimate metal_

### Walkthrough
* Add `__struct_methods` attribute to data class dictionaries to enable
access of methods defined in `ti.dataclass` decorator
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR633),
[link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR654))
* Add test cases for using `ti.func` and `ti.kernel` as methods of data
classes, both as kernel arguments and kernel returns, in
`test_struct.py`
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-e87bf5cb1cd09e10b5cfa001ab2ef18f31a242db3a7b66ee98a76d60b1615e71R79-R130))
@github-project-automation github-project-automation bot moved this from Todo to Done in Taichi Lang Apr 23, 2023
lin-hitonami added a commit to lin-hitonami/taichi that referenced this issue Apr 27, 2023
…rn value (taichi-dev#7865)

Issue: fixes taichi-dev#7822

<!--
copilot:all
-->
### <samp>🤖 Generated by Copilot at 4ae87d7</samp>

### Summary
🧪🛠️🚀

<!--
1. 🧪 - This emoji represents testing, experimentation, or science, and
can be used to indicate the addition of test cases or the verification
of functionality.
2. 🛠️ - This emoji represents tools, construction, or repair, and can be
used to indicate the addition of a new feature or the improvement of an
existing one.
3. 🚀 - This emoji represents speed, launch, or innovation, and can be
used to indicate the enhancement of performance, the expansion of
capabilities, or the introduction of a novel idea.
-->
This pull request enables data classes defined by `ti.dataclass` to have
methods decorated by `ti.func` and `ti.kernel`. It also adds tests to
check the functionality and correctness of this feature.

> _`StructType` creates the data class of doom_
> _With `__struct_methods` to unleash its power_
> _Call them from Python, or pass them to `ti.kernel`_
> _`ti.func` returns them, the ultimate metal_

### Walkthrough
* Add `__struct_methods` attribute to data class dictionaries to enable
access of methods defined in `ti.dataclass` decorator
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR633),
[link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR654))
* Add test cases for using `ti.func` and `ti.kernel` as methods of data
classes, both as kernel arguments and kernel returns, in
`test_struct.py`
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-e87bf5cb1cd09e10b5cfa001ab2ef18f31a242db3a7b66ee98a76d60b1615e71R79-R130))
feisuzhu pushed a commit to feisuzhu/taichi that referenced this issue Apr 27, 2023
…rn value (taichi-dev#7865)

Issue: fixes taichi-dev#7822

<!--
copilot:all
-->
### <samp>🤖 Generated by Copilot at 4ae87d7</samp>

### Summary
🧪🛠️🚀

<!--
1. 🧪 - This emoji represents testing, experimentation, or science, and
can be used to indicate the addition of test cases or the verification
of functionality.
2. 🛠️ - This emoji represents tools, construction, or repair, and can be
used to indicate the addition of a new feature or the improvement of an
existing one.
3. 🚀 - This emoji represents speed, launch, or innovation, and can be
used to indicate the enhancement of performance, the expansion of
capabilities, or the introduction of a novel idea.
-->
This pull request enables data classes defined by `ti.dataclass` to have
methods decorated by `ti.func` and `ti.kernel`. It also adds tests to
check the functionality and correctness of this feature.

> _`StructType` creates the data class of doom_
> _With `__struct_methods` to unleash its power_
> _Call them from Python, or pass them to `ti.kernel`_
> _`ti.func` returns them, the ultimate metal_

### Walkthrough
* Add `__struct_methods` attribute to data class dictionaries to enable
access of methods defined in `ti.dataclass` decorator
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR633),
[link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR654))
* Add test cases for using `ti.func` and `ti.kernel` as methods of data
classes, both as kernel arguments and kernel returns, in
`test_struct.py`
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-e87bf5cb1cd09e10b5cfa001ab2ef18f31a242db3a7b66ee98a76d60b1615e71R79-R130))
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
…rn value (taichi-dev#7865)

Issue: fixes taichi-dev#7822

<!--
copilot:all
-->
### <samp>🤖 Generated by Copilot at 4ae87d7</samp>

### Summary
🧪🛠️🚀

<!--
1. 🧪 - This emoji represents testing, experimentation, or science, and
can be used to indicate the addition of test cases or the verification
of functionality.
2. 🛠️ - This emoji represents tools, construction, or repair, and can be
used to indicate the addition of a new feature or the improvement of an
existing one.
3. 🚀 - This emoji represents speed, launch, or innovation, and can be
used to indicate the enhancement of performance, the expansion of
capabilities, or the introduction of a novel idea.
-->
This pull request enables data classes defined by `ti.dataclass` to have
methods decorated by `ti.func` and `ti.kernel`. It also adds tests to
check the functionality and correctness of this feature.

> _`StructType` creates the data class of doom_
> _With `__struct_methods` to unleash its power_
> _Call them from Python, or pass them to `ti.kernel`_
> _`ti.func` returns them, the ultimate metal_

### Walkthrough
* Add `__struct_methods` attribute to data class dictionaries to enable
access of methods defined in `ti.dataclass` decorator
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR633),
[link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-3154e0533b9fd63e663c16c2e87c65068c3b002a65cf80529b6577d173bdb5feR654))
* Add test cases for using `ti.func` and `ti.kernel` as methods of data
classes, both as kernel arguments and kernel returns, in
`test_struct.py`
([link](https://github.com/taichi-dev/taichi/pull/7865/files?diff=unified&w=0#diff-e87bf5cb1cd09e10b5cfa001ab2ef18f31a242db3a7b66ee98a76d60b1615e71R79-R130))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants