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

Why TaiChi can't recognize subscript operation of 'taichi.lang.matrix.Vector' #8349

Closed
Ives0721 opened this issue Sep 14, 2023 · 2 comments
Closed
Assignees
Labels
question Question on using Taichi

Comments

@Ives0721
Copy link

Ives0721 commented Sep 14, 2023

Problem code

In the code below, TaiChi will throw a IndexError about the index of w.

import taichi as ti
ti.init(arch=ti.cpu)

w = ti.types.vector(9, float)([4/9, 1/9, 1/9, 1/9, 1/9, 1/36, 1/36, 1/36, 1/36])

@ti.data_oriented
class TestCls:
    def __init__(self, Nx: int, Ny: int, omega: float=1.0):
        # parameters to define a lattice
        self.Nx = Nx
        self.Ny = Ny

        self.feq = ti.Vector.field(9, float, shape=(Nx, Ny))
        self.rho = ti.field(float, shape=(Nx, Ny))

    @ti.kernel
    def initialize(self):
        rho, feq = ti.static(self.rho, self.feq)
        for x, y in ti.ndrange(self.Nx, self.Ny):
            for q in ti.ndrange((0, 9)):
                feq[x, y][q] = w[q] * rho[x, y]


test_obj = TestCls(5, 5)
test_obj.initialize()

PowerShell output looks like this

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.9.16
[Taichi] Starting on arch=x64
<class 'taichi.lang.matrix.Vector'>
Traceback (most recent call last):
  File "d:\Codes\CorpRepo\LBDEM-TaiChi\try_ti.py", line 29, in <module>
    test_obj.initialize()
  File "D:\ProgramData\PyEnv\SCI\lib\site-packages\taichi\lang\kernel_impl.py", line 1035, in __call__
    raise type(e)("\n" + str(e)) from None
taichi.lang.exception.TaichiCompilationError:
File "d:\Codes\CorpRepo\LBDEM-TaiChi\try_ti.py", line 25, in initialize:
                feq[x, y][q] = w[q] * rho[x, y]
                               ^^^^
Traceback (most recent call last):
  File "D:\ProgramData\PyEnv\SCI\lib\site-packages\taichi\lang\ast\ast_transformer_utils.py", line 27, in __call__  
    return method(ctx, node)
  File "D:\ProgramData\PyEnv\SCI\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 "D:\ProgramData\PyEnv\SCI\lib\site-packages\taichi\lang\util.py", line 312, in wrapped
    return func(*args, **kwargs)
  File "D:\ProgramData\PyEnv\SCI\lib\site-packages\taichi\lang\impl.py", line 194, in subscript
    return value.__getitem__(_indices)
  File "D:\ProgramData\PyEnv\SCI\lib\site-packages\taichi\lang\matrix.py", line 320, in __getitem__
    entry = self._get_entry(indices)
  File "D:\ProgramData\PyEnv\SCI\lib\site-packages\taichi\lang\matrix.py", line 352, in _get_entry
    return self.entries[indices[0]]
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

A way to avoid this

But when I change the definition of w into this, then everything works well.

import numpy as np

w = ti.field(float, shape=(9,))
w.from_numpy(
    np.array([4/9, 1/9, 1/9, 1/9, 1/9, 1/36, 1/36, 1/36, 1/36])
)

Question

How to pass the w defined by ti.types.vector(9, float) to the @ti.kernel without use w as input argument?

@Ives0721 Ives0721 added the question Question on using Taichi label Sep 14, 2023
@github-project-automation github-project-automation bot moved this to Untriaged in Taichi Lang Sep 14, 2023
@jim19930609
Copy link
Contributor

I was able to run the code with Taichi built with latest commit. Can you try installing the nightly build and see if this issue have already been resolved?

pip install -i https://pypi.taichi.graphics/simple/ taichi-nightly

@jim19930609 jim19930609 moved this from Untriaged to Backlog in Taichi Lang Oct 12, 2023
@Ives0721
Copy link
Author

I was able to run the code with Taichi built with latest commit. Can you try installing the nightly build and see if this issue have already been resolved?

pip install -i https://pypi.taichi.graphics/simple/ taichi-nightly

Thanks. That's OK.

@github-project-automation github-project-automation bot moved this from Backlog to Done in Taichi Lang Mar 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question on using Taichi
Projects
Status: Done
Development

No branches or pull requests

2 participants