Describe the bug
TypeError: Cannot loop over the object <class 'taichi.lang.matrix.VectorNdarray'> in Taichi scope. Only Taichi fields (via template) or dense arrays (via types.ndarray) are supported.
How do I make an ndarray of vec3? Why did this type become based on ti.lang.matrix?
To Reproduce
Starting from the increment example, I create an array of 3-vectors and attempt to increment the first element of each.
Basically, I want perform some computation on every 3-vector in an array of 3-vectors.
import taichi as ti
ti.init()
@ti.data_oriented
class TiArray:
def __init__(self, n):
self.x = ti.ndarray(dtype=ti.math.vec3, shape=n)
@ti.kernel
def inc(self):
# Increment the first element of each vector
for i in self.x:
self.x[i][0] += 1
a = TiArray(32)
a.inc()
I tried changing to this:
#!/usr/bin/env python3
import taichi as ti
ti.init()
@ti.data_oriented
class TiArray:
def __init__(self, n):
self.x = ti.ndarray(dtype=ti.math.vec3, shape=n)
@ti.kernel
def inc(self):
for i in range(self.x.shape[0]):
self.x[i][0] += 1
a = TiArray(32)
a.inc()
Which gives the following exception:
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
[Taichi] Starting on arch=arm64
Traceback (most recent call last):
File "/Users/ben/test/./test2.py", line 17, in <module>
a.inc()
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/kernel_impl.py", line 1086, in __call__
raise type(e)('\n' + str(e)) from None
taichi.lang.exception.TaichiCompilationError:
File "/Users/ben/test/./test2.py", line 14, in inc:
self.x[i][0] += 1
^^^^^^^^^
Traceback (most recent call last):
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer_utils.py", line 25, in __call__
return method(ctx, node)
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer.py", line 238, in build_Subscript
node.ptr = impl.subscript(ctx.ast_builder, node.value.ptr,
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/util.py", line 301, in wrapped
return func(*args, **kwargs)
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/impl.py", line 164, in subscript
return value.__getitem__(_indices)
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/util.py", line 309, in wrapped
assert in_python_scope(), \
AssertionError: __getitem__ cannot be called in Taichi-scope
Which I can fix by passing the array as an arg in this way but I don't understand why it works (and it defeats the purpose of self):
#!/usr/bin/env python3
import taichi as ti
ti.init()
@ti.data_oriented
class TiArray:
def __init__(self, n):
self.x = ti.ndarray(dtype=ti.math.vec3, shape=n)
@ti.kernel
def inc(self, x: ti.types.ndarray(ti.math.vec3, 1)):
for i in range(x.shape[0]):
x[i][0] += 1
a = TiArray(32)
a.inc(a.x)
Log/Screenshots
ben@Bens-M2-Max test % ./test.py
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
[Taichi] Starting on arch=arm64
Traceback (most recent call last):
File "/Users/ben/test/./test.py", line 17, in <module>
a.inc()
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/kernel_impl.py", line 1086, in __call__
raise type(e)('\n' + str(e)) from None
taichi.lang.exception.TaichiCompilationError:
File "/Users/ben/test/./test.py", line 13, in inc:
for i in self.x:
^^^^^^^^^^^^^^^^
Traceback (most recent call last):
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer_utils.py", line 25, in __call__
return method(ctx, node)
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer.py", line 1352, in build_For
return ASTTransformer.build_struct_for(ctx,
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer.py", line 1242, in build_struct_for
impl.begin_frontend_struct_for(ctx.ast_builder, expr_group,
File "/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/lang/impl.py", line 89, in begin_frontend_struct_for
raise TypeError(
TypeError: Cannot loop over the object <class 'taichi.lang.matrix.VectorNdarray'> in Taichi scope. Only Taichi fields (via template) or dense arrays (via types.ndarray) are supported.
Additional comments
ben@Bens-M2-Max test % ti diagnose
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
*******************************************
** Taichi Programming Language **
*******************************************
Docs: https://docs.taichi-lang.org/
GitHub: https://github.com/taichi-dev/taichi/
Forum: https://forum.taichi.graphics/
Taichi system diagnose:
python: 3.10.10 (main, Mar 16 2023, 16:52:20) [Clang 14.0.0 (clang-1400.0.29.202)]
system: darwin
executable: /Users/ben/.pyenv/versions/3.10.10/bin/python3.10
platform: macOS-13.2.1-arm64-arm-64bit
architecture: 64bit
uname: uname_result(system='Darwin', node='Bens-M2-Max.local', release='22.3.0', version='Darwin Kernel Version 22.3.0: Mon Jan 30 20:39:46 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6020', machine='arm64')
locale: en_US.UTF-8
PATH: /Users/ben/.pyenv/versions/3.10.10/bin:/opt/homebrew/Cellar/pyenv/2.3.15/libexec:/opt/homebrew/Cellar/pyenv/2.3.15/plugins/python-build/bin:/Users/ben/.pyenv/shims:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Users/ben/.cargo/bin:/Users/ben/.pyenv/shims:/opt/homebrew/bin:/opt/homebrew/sbin
PYTHONPATH: ['/Users/ben/.pyenv/versions/3.10.10/bin', '/Users/ben/.pyenv/versions/3.10.10/lib/python310.zip', '/Users/ben/.pyenv/versions/3.10.10/lib/python3.10', '/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/lib-dynload', '/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages']
`lsb_release` not available: [Errno 2] No such file or directory: 'lsb_release'
import: <module 'taichi' from '/Users/ben/.pyenv/versions/3.10.10/lib/python3.10/site-packages/taichi/__init__.py'>
cc: False
cpu: True
metal: True
opengl: False
cuda: False
vulkan: True
`glewinfo` not available: [Errno 2] No such file or directory: 'glewinfo'
`nvidia-smi` not available: [Errno 2] No such file or directory: 'nvidia-smi'
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
[Taichi] Starting on arch=arm64
[W 04/26/23 14:12:26.013 20887529] [misc.py:adaptive_arch_select@772] Arch=[<Arch.opengl: 7>] is not supported, falling back to CPU
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
[Taichi] Starting on arch=arm64
[W 04/26/23 14:12:26.502 20887589] [misc.py:adaptive_arch_select@772] Arch=[<Arch.cuda: 5>] is not supported, falling back to CPU
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
[Taichi] Starting on arch=arm64
[Taichi] version 1.5.0, llvm 16.0.0git, commit 7b885c28, osx, python 3.10.10
*******************************************
** Taichi Programming Language **
*******************************************
Docs: https://docs.taichi-lang.org/
GitHub: https://github.com/taichi-dev/taichi/
Forum: https://forum.taichi.graphics/
TAICHI EXAMPLES
────────────────────────────────────────────────────────────────────────────────────
0: ad_gravity 25: laplace 50: physarum
1: circle_packing_image 26: laplace_equation 51: poisson_disk_sampling
2: comet 27: mandelbrot_zoom 52: print_offset
3: cornell_box 28: marching_squares 53: rasterizer
4: diff_sph 29: mass_spring_3d_ggui 54: regression
5: euler 30: mass_spring_game 55: sdf_renderer
6: explicit_activation 31: mass_spring_game_ggui 56: simple_derivative
7: export_mesh 32: mciso_advanced 57: simple_texture
8: export_ply 33: mgpcg 58: simple_uv
9: export_videos 34: mgpcg_advanced 59: snow_phaseField
10: fem128 35: minimal 60: stable_fluid
11: fem128_ggui 36: minimization 61: stable_fluid_ggui
12: fem99 37: mpm128 62: stable_fluid_graph
13: fractal 38: mpm128_ggui 63: taichi_bitmasked
14: fractal3d_ggui 39: mpm3d 64: taichi_dynamic
15: fullscreen 40: mpm3d_ggui 65: taichi_logo
16: game_of_life 41: mpm88 66: taichi_ngp
17: gui_image_io 42: mpm88_graph 67: taichi_sparse
18: gui_widgets 43: mpm99 68: texture_graph
19: implicit_fem 44: mpm_lagrangian_forces 69: tutorial
20: implicit_mass_spring 45: nbody 70: two_stream_instability
21: initial_value_problem 46: odop_solar 71: vortex_rings
22: jacobian 47: oit_renderer 72: waterwave
23: karman_vortex_street 48: patterns
24: keyboard 49: pbf2d
────────────────────────────────────────────────────────────────────────────────────
42
Running example minimal ...
[Taichi] Starting on arch=arm64
42.0
>>> Running time: 0.20s
Consider attaching this log when maintainers ask about system information.
>>> Running time: 4.84s
Describe the bug TypeError: Cannot loop over the object <class 'taichi.lang.matrix.VectorNdarray'> in Taichi scope. Only Taichi fields (via template) or dense arrays (via types.ndarray) are supported. How do I make an ndarray of vec3? Why did this type become based on ti.lang.matrix?
To Reproduce Starting from the increment example, I create an array of 3-vectors and attempt to increment the first element of each. Basically, I want perform some computation on every 3-vector in an array of 3-vectors.
I tried changing to this:
Which gives the following exception:
Which I can fix by passing the array as an arg in this way but I don't understand why it works (and it defeats the purpose of
self
):Log/Screenshots
Additional comments