taichi-dev / taichi

Productive, portable, and performant GPU programming in Python.
https://taichi-lang.org
Apache License 2.0
25.56k stars 2.29k forks source link

Taichi ndarray element assignment not working #8560

Open agravier opened 4 months ago

agravier commented 4 months ago

Describe the bug When I assign a scalar to an ndarray, it sometimes doesn't work!

To Reproduce

import taichi as ti

ti.init(arch=ti.gpu)

g = ti.ndarray(ti.int8, shape=(11, 5))
g.fill(1)
g[1, 2] = 2
assert g[1, 2] == 2

Log/Screenshots

$ python tests/ndarray_weird_behaviour.py
[Taichi] version 1.7.1, llvm 15.0.7, commit 0f143b2f, osx, python 3.10.9
[Taichi] Starting on arch=metal
Traceback (most recent call last):
  File "/Users/agravier/projects/perso/arc/archi/tests/ndarray_weird_behaviour.py", line 9, in <module>
    assert g[1, 2] == 2
AssertionError

Additional comments

In a more complex program, the behaviour is affected by other operations. For instance, this works:

from itertools import product
import taichi as ti

ti.init(arch=ti.gpu)

g = ti.ndarray(ti.int8, shape=(11, 5))
g.fill(1)
for i, j in product(range(11), range(5)):
    g[i, j] = i*j+1

print(g.to_numpy())

assert g[1, 2] == 3

Moreover, this one works too (!):

import taichi as ti

ti.init(arch=ti.gpu)

g = ti.ndarray(ti.int8, shape=(11, 5))
g.fill(1)
g[1, 2] = 2
g[1, 2] = 2
assert g[1, 2] == 2

I suspect that it's sometimes the first assignment that fails.