Skip to content

Incorrect running result #8744

@emailweixu

Description

@emailweixu

Describe the bug

Two seemingly useless lines of code make a function incorrect. Note that the code is simplified from a more complex code where those two lines of useless code are different and necessary. This makes me very concerned as I am worrying about the overall correctness of our project that heavily uses Taichi.

To Reproduce

import torch
import taichi as ti
torch.set_default_device("cuda")
ti.init(arch=ti.cuda, default_ip=ti.i32, default_fp=ti.f32)

@ti.func
def testf(box0, box1, p) -> ti.u1:
    bbox = box0
    a = bbox[0]
    x0 = a
    # If the following two lines are commented out, the assert will pass.
    if p:
        x0 = x0 + 0
    # If the following variable is renamed to `bbox1`, the assert will pass
    # (bbox at the line after is also changed accordingly to `bbox1`).
    bbox = box1
    a = bbox[0]
    x1 = a
    return x0 == x1

@ti.kernel
def test(boxes0: ti.template(), boxes1: ti.template(), res: ti.types.ndarray(), n: int):

    for i in ti.ndrange(boxes0.shape[0]):
        box0 = boxes0[i]
        box1 = boxes1[i]
        res[i] = testf(box0, box1, i == n)

boxes0 = ti.math.vec2.field(shape=(10,))
boxes1 = ti.math.vec2.field(shape=(10,))
boxes0.from_torch(torch.randn((10, 2)))
boxes1.from_torch(torch.randn((10, 2)))
result = torch.zeros((10,), dtype=torch.bool)

test(boxes0, boxes1, result, 15)
expected = boxes0.to_torch()[:, 0] == boxes1.to_torch()[:, 1]
assert (result == expected).all(), f"{result} != {expected}"

However, if the if p block is commented out, the result is correct and the assert can pass.

Log/Screenshots

$ python my_sample_code.py
[Taichi] version 1.7.3, llvm 15.0.4, commit 5ec301be, linux, python 3.10.12
[Taichi] Starting on arch=cuda
Traceback (most recent call last):
  File "/data/weixu/code/test/ti_bug.py", line 37, in <module>
    assert (result == expected).all(), f"{result} != {expected}"
AssertionError: tensor([True, True, True, True, True, True, True, True, True, True],
       device='cuda:0') != tensor([False, False, False, False, False, False, False, False, False, False],
       device='cuda:0')

Additional comments

ti diagnose result is shown below:

[Taichi] version 1.7.3, llvm 15.0.4, commit 5ec301be, linux, python 3.10.12

*******************************************
**      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.12 (main, Mar 22 2024, 16:50:05) [GCC 11.4.0]
system: linux
executable: /home/weixu/venvs/working/bin/python
platform: Linux-6.5.0-41-generic-x86_64-with-glibc2.35
architecture: 64bit ELF
uname: uname_result(system='Linux', node='GAIL-X', release='6.5.0-41-generic', version='#41~22.04.2-Ubuntu SMP PREEMPT_DYNAMIC Mon Jun  3 11:32:55 UTC 2', machine='x86_64')
locale: en_US.UTF-8
PATH: /home/weixu/venvs/working/bin:/data/weixu/bin:/home/weixu/.vscode-server/cli/servers/Stable-cb0c47c0cfaad0757385834bd89d410c78a856c0/server/bin/remote-cli:/home/weixu/.local/bin:/home/weixu/miniconda3/condabin:/data/weixu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/weixu/.vscode-server/extensions/ms-python.debugpy-2025.10.0-linux-x64/bundled/scripts/noConfigScripts:/home/weixu/.vscode-server/data/User/globalStorage/github.copilot-chat/debugCommand
PYTHONPATH: ['/home/weixu/venvs/working/bin', '/data/weixu/code/Carla-0.10.0-Linux-Shipping/PythonAPI/carla', '/data/weixu/code/FlashDrive', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '/home/weixu/venvs/working/lib/python3.10/site-packages', '__editable__.omni_isaac_lab-0.22.4.finder.__path_hook__', '__editable__.omni_isaac_lab_assets-0.1.3.finder.__path_hook__', '__editable__.omni_isaac_lab_tasks-0.10.0.finder.__path_hook__', '/data/weixu/code/hobot_submission', '/home/weixu/venvs/working/lib/python3.10/site-packages/fused_linear_relu-0.0.0-py3.10-linux-x86_64.egg']

`lsb_release` not available: [Errno 2] No such file or directory: 'lsb_release'


import: <module 'taichi' from '/home/weixu/venvs/working/lib/python3.10/site-packages/taichi/__init__.py'>

cpu: True
metal: False
opengl: True
cuda: True
vulkan: True

`glewinfo` not available: [Errno 2] No such file or directory: 'glewinfo'

Mon Jul 14 23:46:39 2025       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.104.05             Driver Version: 535.104.05   CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| 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 RTX 3090        On  | 00000000:17:00.0  On |                  N/A |
|  0%   60C    P5             119W / 420W |    514MiB / 24576MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
|   1  NVIDIA GeForce RTX 3090        On  | 00000000:65:00.0 Off |                  N/A |
|  0%   53C    P8              45W / 420W |     14MiB / 24576MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+
                                                                                         
+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|    0   N/A  N/A      1563      G   /usr/lib/xorg/Xorg                          103MiB |
|    0   N/A  N/A      1789      G   /usr/bin/gnome-shell                         11MiB |
|    0   N/A  N/A    935392      C   /home/weixu/venvs/working/bin/python        384MiB |
|    1   N/A  N/A      1563      G   /usr/lib/xorg/Xorg                            4MiB |
+---------------------------------------------------------------------------------------+

[Taichi] version 1.7.3, llvm 15.0.4, commit 5ec301be, linux, python 3.10.12

[Taichi] version 1.7.3, llvm 15.0.4, commit 5ec301be, linux, python 3.10.12
[Taichi] Starting on arch=x64

[Taichi] version 1.7.3, llvm 15.0.4, commit 5ec301be, linux, python 3.10.12
[Taichi] Starting on arch=opengl

[Taichi] version 1.7.3, llvm 15.0.4, commit 5ec301be, linux, python 3.10.12
[Taichi] Starting on arch=cuda

[Taichi] version 1.7.3, llvm 15.0.4, commit 5ec301be, linux, python 3.10.12

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

Consider attaching this log when maintainers ask about system information.
>>> Running time: 4.75s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Untriaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions