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

Type hint error when passing ti.template() var to ti.func as a list index #8449

Closed
jinjinhe2001 opened this issue Dec 24, 2023 · 3 comments
Closed

Comments

@jinjinhe2001
Copy link

jinjinhe2001 commented Dec 24, 2023

Describe the bug
When passing a ti.template() variable as a list index into the ti.func function, it have to use type hint to work

To Reproduce

import taichi as ti

ti.init(arch=ti.gpu, default_fp=ti.f32)

layer_num = 10
array = [ti.field(dtype=ti.f32, shape=(100)) for _ in range(layer_num)]
sum = [ti.field(dtype=ti.f32, shape=()) for _ in range(layer_num)]

@ti.kernel
def iter_layer(layer: ti.template()):
    for i in range(100):
        add_func(layer)

@ti.func
def add_func(layer): # only work under layer: ti.template()
    sum[layer][None] += array[layer][i]

for i in range(layer_num):
    iter_layer(i)

Log/Screenshots
Please post the full log of the program (instead of just a few lines around the error message, unless the log is > 1000 lines). This will help us diagnose what's happening. For example:

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.9.0
[Taichi] Starting on arch=cuda
Traceback (most recent call last):
  File "D:\taichi\test.py", line 20, in <module>
    iter_layer(i)
  File "E:\python3.9\lib\site-packages\taichi\lang\kernel_impl.py", line 976, in wrapped
    raise type(e)("\n" + str(e)) from None
taichi.lang.exception.TaichiCompilationError:
File "D:\taichi\test.py", line 13, in iter_layer:
        add_func(layer)
        ^^^^^^^^^^^^^^^
File "D:\taichi\test.py", line 17, in add_func:
    sum[layer][None] += array[layer][i]
    ^^^^^^^^^^
Traceback (most recent call last):
  File "E:\python3.9\lib\site-packages\taichi\lang\ast\ast_transformer_utils.py", line 27, in __call__
    return method(ctx, node)
  File "E:\python3.9\lib\site-packages\taichi\lang\ast\ast_transformer.py", line 233, in build_Subscript
    node.ptr = impl.subscript(ctx.ast_builder, node.value.ptr, *node.slice.ptr)
  File "E:\python3.9\lib\site-packages\taichi\lang\util.py", line 312, in wrapped
    return func(*args, **kwargs)
  File "E:\python3.9\lib\site-packages\taichi\lang\impl.py", line 194, in subscript
    return value.__getitem__(_indices)
TypeError: list indices must be integers or slices, not Expr

Additional comments
I'm not sure if this is a bug or not. it seems like ti.func should work without a type hint

@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Dec 24, 2023
@jinjinhe2001 jinjinhe2001 changed the title Type hint error when passing ti.template() var to ti.func for list index Type hint error when passing ti.template() var to ti.func as a list index Dec 24, 2023
@bobcao3
Copy link
Collaborator

bobcao3 commented Dec 24, 2023

This is not a bug. If you want fields with multiple dimensions you can declare them as so e.g. shape=(num_layers, 100). Python arrays in taichi must be indexed statically (i.e. with ti.template and ti.static which makes the indexing compile time)

@bobcao3 bobcao3 closed this as completed Dec 24, 2023
@github-project-automation github-project-automation bot moved this from Untriaged to Done in Taichi Lang Dec 24, 2023
@jinjinhe2001
Copy link
Author

jinjinhe2001 commented Dec 24, 2023

The fields are just for reproducing. The key point is the type hint ti.template() of the ti.func is mandatory for this case...

@bobcao3
Copy link
Collaborator

bobcao3 commented Dec 26, 2023

The fields are just for reproducing. The key point is the type hint ti.template() of the ti.func is mandatory for this case...

Still, Python arrays must be explicitly statically indexed ...

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

No branches or pull requests

2 participants