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

@ti.func type hint error when using vector type #8042

Closed
neozhaoliang opened this issue May 18, 2023 · 3 comments · Fixed by #8197
Closed

@ti.func type hint error when using vector type #8042

neozhaoliang opened this issue May 18, 2023 · 3 comments · Fixed by #8197
Assignees

Comments

@neozhaoliang
Copy link
Contributor

neozhaoliang commented May 18, 2023

To reproduce:

import taichi as ti
ti.init()

vec4d = ti.types.vector(4, float)
v = vec4d(1, 2, 3, 4)

@ti.func
def length(w: vec4d):
    return w.norm()

@ti.kernel
def test():
    print(length(v))

test()

It reports error

Argument w of type <taichi.lang.matrix.VectorType object at 0x7f9954126d40> is expected to be a Matrix, but got <class 'taichi.lang.matrix.Vector'>.
@jim19930609
Copy link
Contributor

It fails because you're directly using a python-scope Matrix v in Taichi kernel. To make it work, either pass v through the kernel arguments:

import taichi as ti
ti.init()

vec4d = ti.types.vector(4, float)
v = vec4d(1, 2, 3, 4)

@ti.func
def length(w: vec4d):
    return w.norm()

@ti.kernel
def test(x: vec4d):
    print(length(x))

test(v)

Or define v inside the kernel:

import taichi as ti
ti.init()

vec4d = ti.types.vector(4, float)

@ti.func
def length(w: vec4d):
    return w.norm()

@ti.kernel
def test():
    v = vec4d(1, 2, 3, 4)
    print(length(x))

test()

@neozhaoliang
Copy link
Contributor Author

Just checked that the code works in v1.3.0 and fails for v1.4.0

@jim19930609
Copy link
Contributor

hmmm, let me dig into this further

@github-project-automation github-project-automation bot moved this from Todo to Done in Taichi Lang Jun 19, 2023
jim19930609 added a commit that referenced this issue Jun 19, 2023
…8197)

Issue: fix #8042 #8114

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at da679b9</samp>

Add support for Python matrices and functions in Taichi kernels. Update
`get_var_by_name` to handle `Matrix` objects and add a new test case in
`test_matrix.py`.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at da679b9</samp>

*  Enable using Python matrices and functions in Taichi kernels by:
- Converting `Matrix` objects to `ti.Matrix` objects in
`get_var_by_name`
([link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-3c4fc72604c650382997de2541e648ef88ee8f608a9e3d3ebbe7e6a74cb2eec8L258-R263))
- Adding a new test case for the matrix and function feature in
`test_matrix.py`
([link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-28226020cc3cc2e223eb43801fa78360d006c26d7140c3b37719faf9e9df34f7R6),
[link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-28226020cc3cc2e223eb43801fa78360d006c26d7140c3b37719faf9e9df34f7R1362-R1377))
L2ncE pushed a commit to L2ncE/taichi that referenced this issue Jun 20, 2023
…aichi-dev#8197)

Issue: fix taichi-dev#8042 taichi-dev#8114

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at da679b9</samp>

Add support for Python matrices and functions in Taichi kernels. Update
`get_var_by_name` to handle `Matrix` objects and add a new test case in
`test_matrix.py`.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at da679b9</samp>

*  Enable using Python matrices and functions in Taichi kernels by:
- Converting `Matrix` objects to `ti.Matrix` objects in
`get_var_by_name`
([link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-3c4fc72604c650382997de2541e648ef88ee8f608a9e3d3ebbe7e6a74cb2eec8L258-R263))
- Adding a new test case for the matrix and function feature in
`test_matrix.py`
([link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-28226020cc3cc2e223eb43801fa78360d006c26d7140c3b37719faf9e9df34f7R6),
[link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-28226020cc3cc2e223eb43801fa78360d006c26d7140c3b37719faf9e9df34f7R1362-R1377))
PGZXB pushed a commit to PGZXB/taichi that referenced this issue Jul 13, 2023
…aichi-dev#8197)

Issue: fix taichi-dev#8042 taichi-dev#8114

### Brief Summary

<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at da679b9</samp>

Add support for Python matrices and functions in Taichi kernels. Update
`get_var_by_name` to handle `Matrix` objects and add a new test case in
`test_matrix.py`.

### Walkthrough

<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at da679b9</samp>

*  Enable using Python matrices and functions in Taichi kernels by:
- Converting `Matrix` objects to `ti.Matrix` objects in
`get_var_by_name`
([link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-3c4fc72604c650382997de2541e648ef88ee8f608a9e3d3ebbe7e6a74cb2eec8L258-R263))
- Adding a new test case for the matrix and function feature in
`test_matrix.py`
([link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-28226020cc3cc2e223eb43801fa78360d006c26d7140c3b37719faf9e9df34f7R6),
[link](https://github.com/taichi-dev/taichi/pull/8197/files?diff=unified&w=0#diff-28226020cc3cc2e223eb43801fa78360d006c26d7140c3b37719faf9e9df34f7R1362-R1377))
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.

2 participants