taichi-dev / taichi

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

StructType namespacing issue using same field name in different Struct #6825

Closed oliver-batchelor closed 1 year ago

oliver-batchelor commented 1 year ago

Describe the bug

Strange namespacing issue when I have a ti.dataclass (StructType) with a name which matches another and I use them in the same ti.func.

If I swap these two lines the problem goes away

    line1 = self.to_line()
    line2 = seg2.dir()

If I change either of the dir fields to something else the problem goes away.

To Reproduce Please post a minimal sample code to reproduce the bug. The developer team will put a higher priority on bugs that can be reproduced within 20 lines of code. If you want a prompt reply, please keep the sample code short and representative.

import taichi as ti
from taichi.math import vec2, vec3

@ti.dataclass
class Line:
  p: vec3
  dir: vec3

@ti.dataclass
class Segment:

  a: vec3
  b: vec3

  @ti.func
  def dir(self) -> vec3:
    return self.b - self.a

  @ti.func
  def to_line(self):
    return Line(self.a, self.dir())

  @ti.func
  def segment_closest(self, seg2:ti.template()) -> vec2:
    line1 = self.to_line()
    line2 = seg2.dir()

    return vec2(0, 0)    

ti.init(arch=ti.cpu, debug=True)

seg1 = Segment(vec3(0.0, -1.0, 0.0), vec3(0.0, 1.0, 0.0))
seg2 = Segment(vec3(0.0, 0.0, -1.0), vec3(1.0, 0.0, 1.0))

@ti.kernel
def test_seg_seg() -> vec2:
  return seg1.segment_closest(seg2)
print(test_seg_seg())
File "/csse/users/owb14/sync/python_geometry/bug.py", line 28, in segment_closest:
    line2 = seg2.dir()
            ^^^^^^^^
Traceback (most recent call last):
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    return method(ctx, node)
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer.py", line 871, in build_Attribute
    node.ptr = getattr(node.value.ptr, node.attr)
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/struct.py", line 145, in getter
    return self[key]
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/struct.py", line 112, in __getitem__
    ret = self.entries[key]
KeyError: 'dir'

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:

> python_geometry python bug.py                                                                                                                                                     main
[Taichi] version 1.3.0, llvm 15.0.4, commit 0f25b95e, linux, python 3.10.0
[Taichi] Starting on arch=x64
Traceback (most recent call last):
  File "/csse/users/owb14/sync/python_geometry/bug.py", line 43, in <module>
    print(test_seg_seg())
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/kernel_impl.py", line 947, in wrapped
    raise type(e)('\n' + str(e)) from None
taichi.lang.exception.TaichiCompilationError: 
File "/csse/users/owb14/sync/python_geometry/bug.py", line 41, in test_seg_seg:
  return seg1.segment_closest(seg2)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/csse/users/owb14/sync/python_geometry/bug.py", line 28, in segment_closest:
    line2 = seg2.dir()
            ^^^^^^^^
Traceback (most recent call last):
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer_utils.py", line 26, in __call__
    return method(ctx, node)
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/ast/ast_transformer.py", line 871, in build_Attribute
    node.ptr = getattr(node.value.ptr, node.attr)
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/struct.py", line 145, in getter
    return self[key]
  File "/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/lang/struct.py", line 112, in __getitem__
    ret = self.entries[key]
KeyError: 'dir'

Additional comments

ti diagnose main [Taichi] version 1.3.0, llvm 15.0.4, commit 0f25b95e, linux, python 3.10.0


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.0 | packaged by conda-forge | (default, Nov 20 2021, 02:24:10) [GCC 9.4.0] system: linux executable: /local/miniconda3/envs/torch13/bin/python3.10 platform: Linux-5.13.0-35-generic-x86_64-with-glibc2.31 architecture: 64bit ELF uname: uname_result(system='Linux', node='cs19120bs', release='5.13.0-35-generic', version='#40~20.04.1-Ubuntu SMP Mon Mar 7 09:18:32 UTC 2022', machine='x86_64') locale: en_NZ.UTF-8 PATH: /local/miniconda3/envs/torch13/bin:/local/miniconda3/condabin:/csse/users/owb14/.local/bin:/usr/local/cuda/bin:/opt/spinnaker/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/csse/misc/android-sdk-linux/tools:/csse/misc/android-sdk-linux/platform-tools:/csse/users/owb14/.dotnet/tools:/usr/local/gradle-7.3.1/bin:/usr/local/jazzer:/usr/local/maven-3.8.4/bin:/usr/share/java/javafx-sdk-17.0.1:/usr/local/sqlmap-1.6.8:/csse/users/owb14/.local/bin:/csse/users/owb14/bin:/netfs/share/bin PYTHONPATH: ['/local/miniconda3/envs/torch13/bin', '/local/miniconda3/envs/torch13/lib/python310.zip', '/local/miniconda3/envs/torch13/lib/python3.10', '/local/miniconda3/envs/torch13/lib/python3.10/lib-dynload', '/local/miniconda3/envs/torch13/lib/python3.10/site-packages', '/csse/users/owb14/sync/py_structs', '/csse/users/owb14/sync/camera_geometry_python', '/csse/users/owb14/sync/open3d_vis', '/csse/users/owb14/sync/python_geometry']

LSB Version: core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal

import: <module 'taichi' from '/local/miniconda3/envs/torch13/lib/python3.10/site-packages/taichi/init.py'>

cc: False cpu: True metal: False opengl: True cuda: True MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0

vulkan: True

OpenGL version 4.6.0 NVIDIA 520.61.05 is supported GL_ARB_compute_shader: OK GL_ARB_gpu_shader_int64: OK GL_NV_shader_atomic_float: OK GL_NV_shader_atomic_float64: OK GL_NV_shader_atomic_int64: OK

Wed Dec 7 19:17:09 2022
+-----------------------------------------------------------------------------+ | NVIDIA-SMI 520.61.05 Driver Version: 520.61.05 CUDA Version: 11.8 | |-------------------------------+----------------------+----------------------+ | GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 NVIDIA GeForce ... On | 00000000:01:00.0 On | N/A | | 0% 55C P0 55W / 215W | 727MiB / 8192MiB | 1% Default | | | | N/A | +-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=============================================================================| | 0 N/A N/A 2225 G /usr/lib/xorg/Xorg 218MiB | | 0 N/A N/A 3014 G /usr/bin/gnome-shell 46MiB | | 0 N/A N/A 3423 G ...RendererForSitePerProcess 15MiB | | 0 N/A N/A 20604 G ...veSuggestionsOnlyOnDemand 65MiB | | 0 N/A N/A 110122 C+G ...515139170622904451,131072 329MiB | | 0 N/A N/A 1040523 G ...RendererForSitePerProcess 47MiB | +-----------------------------------------------------------------------------+

[Taichi] version 1.3.0, llvm 15.0.4, commit 0f25b95e, linux, python 3.10.0

[Taichi] version 1.3.0, llvm 15.0.4, commit 0f25b95e, linux, python 3.10.0 [Taichi] Starting on arch=x64

[Taichi] version 1.3.0, llvm 15.0.4, commit 0f25b95e, linux, python 3.10.0 [Taichi] Starting on arch=opengl

[Taichi] version 1.3.0, llvm 15.0.4, commit 0f25b95e, linux, python 3.10.0 [Taichi] Starting on arch=cuda

[Taichi] version 1.3.0, llvm 15.0.4, commit 0f25b95e, linux, python 3.10.0


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 24: laplace 48: physarum
1: comet 25: laplace_equation 49: print_offset
2: cornell_box 26: mandelbrot_zoom 50: rasterizer
3: diff_sph 27: marching_squares 51: regression
4: euler 28: mass_spring_3d_ggui 52: sdf_renderer
5: explicit_activation 29: mass_spring_game 53: simple_derivative
6: export_mesh 30: mass_spring_game_ggui 54: simple_texture
7: export_ply 31: mciso_advanced 55: simple_uv
8: export_videos 32: mgpcg 56: snow_phaseField
9: fem128 33: mgpcg_advanced 57: stable_fluid
10: fem128_ggui 34: minimal 58: stable_fluid_ggui
11: fem99 35: minimization 59: stable_fluid_graph
12: fractal 36: mpm128 60: taichi_bitmasked
13: fractal3d_ggui 37: mpm128_ggui 61: taichi_dynamic
14: fullscreen 38: mpm3d 62: taichi_logo
15: game_of_life 39: mpm3d_ggui 63: taichi_ngp
16: gui_image_io 40: mpm88 64: taichi_sparse
17: gui_widgets 41: mpm88_graph 65: texture_graph
18: implicit_fem 42: mpm99 66: tutorial
19: implicit_mass_spring 43: mpm_lagrangian_forces 67: two_stream_instability
20: initial_value_problem 44: nbody 68: vortex_rings
21: jacobian 45: odop_solar 69: waterwave
22: karman_vortex_street 46: patterns
23: keyboard 47: pbf2d
──────────────────────────────────────────────────────────────────────────────────── 42 Running example minimal ... [Taichi] Starting on arch=x64 42.0

Running time: 0.33s

Consider attaching this log when maintainers ask about system information.

Running time: 17.47s

strongoier commented 1 year ago

Hi @oliver-batchelor. The bug looks the same as #6652 and was fixed by #6737. Could you try the latest nightly version (pip install -i https://pypi.taichi.graphics/simple/ taichi-nightly) and see if the problem still exists?

oliver-batchelor commented 1 year ago

@strongoier Yup, absolutely the same - and can confirm it is fixed. Thanks!