taichi-dev / taichi

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

0d scalar field cannot be assigned value for dx12 backend #8335

Closed AndrewChan2022 closed 10 months ago

AndrewChan2022 commented 1 year ago

Describe the bug simple tutorial code only works on x64 and cuda backend, fails to work on dx12/dx11/opengl backend.

To Reproduce change arch to different backend to try.

import taichi as ti

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

max_num_particles = 256

dt = 1e-3

num_particles = ti.field(ti.i32, shape=())
spring_stiffness = ti.field(ti.f32, shape=())
paused = ti.field(ti.i32, shape=())
damping = ti.field(ti.f32, shape=())

particle_mass = 1
bottom_y = 0.05

x = ti.Vector.field(2, dtype=ti.f32, shape=max_num_particles)
v = ti.Vector.field(2, dtype=ti.f32, shape=max_num_particles)

A = ti.Matrix.field(2, 2, dtype=ti.f32, shape=(max_num_particles, max_num_particles))
b = ti.Vector.field(2, dtype=ti.f32, shape=max_num_particles)

# rest_length[i, j] = 0 means i and j are not connected
rest_length = ti.field(ti.f32, shape=(max_num_particles, max_num_particles))

connection_radius = 0.15

gravity = [0, -9.8]

@ti.kernel
def substep():
    # Compute force and new velocity
    n = num_particles[None]
    for i in range(n):
        v[i] *= ti.exp(-dt * damping[None]) # damping
        total_force = ti.Vector(gravity) * particle_mass
        for j in range(n):
            if rest_length[i, j] != 0:
                x_ij = x[i] - x[j]
                total_force += -spring_stiffness[None] * (x_ij.norm() - rest_length[i, j]) * x_ij.normalized()
        v[i] += dt * total_force / particle_mass

    # Collide with ground
    for i in range(n):
        if x[i].y < bottom_y:
            x[i].y = bottom_y
            v[i].y = 0

    # Compute new position
    for i in range(num_particles[None]):
        x[i] += v[i] * dt

@ti.kernel
def new_particle(pos_x: ti.f32, pos_y: ti.f32): # Taichi doesn't support using Matrices as kernel arguments yet
    new_particle_id = num_particles[None]
    x[new_particle_id] = [pos_x, pos_y]
    v[new_particle_id] = [0, 0]
    num_particles[None] += 1

    # Connect with existing particles
    for i in range(new_particle_id):
        dist = (x[new_particle_id] - x[i]).norm()
        if dist < connection_radius:
            rest_length[i, new_particle_id] = 0.1
            rest_length[new_particle_id, i] = 0.1

gui = ti.GUI('Mass Spring System', res=(512, 512), background_color=0xdddddd)

spring_stiffness[None] = 10000
damping[None] = 20

new_particle(0.3, 0.3)
new_particle(0.3, 0.4)
new_particle(0.4, 0.4)

while True:
    for e in gui.get_events(ti.GUI.PRESS):
        if e.key in [ti.GUI.ESCAPE, ti.GUI.EXIT]:
            exit()
        elif e.key == gui.SPACE:
            paused[None] = not paused[None]
        elif e.key == ti.GUI.LMB:
            new_particle(e.pos[0], e.pos[1])
        elif e.key == 'c':
            num_particles[None] = 0
            rest_length.fill(0)
        elif e.key == 's':
            if gui.is_pressed('Shift'):
                spring_stiffness[None] /= 1.1
            else:
                spring_stiffness[None] *= 1.1
        elif e.key == 'd':
            if gui.is_pressed('Shift'):
                damping[None] /= 1.1
            else:
                damping[None] *= 1.1

    if not paused[None]:
        for step in range(10):
            substep()

    X = x.to_numpy()
    gui.circles(X[:num_particles[None]], color=0xffaa77, radius=5)

    gui.line(begin=(0.0, bottom_y), end=(1.0, bottom_y), color=0x0, radius=1)

    for i in range(num_particles[None]):
        for j in range(i + 1, num_particles[None]):
            if rest_length[i, j] != 0:
                gui.line(begin=X[i], end=X[j], radius=2, color=0x445566)
    gui.text(content=f'C: clear all; Space: pause', pos=(0, 0.95), color=0x0)
    gui.text(content=f'S: Spring stiffness {spring_stiffness[None]:.1f}', pos=(0, 0.9), color=0x0)
    gui.text(content=f'D: damping {damping[None]:.2f}', pos=(0, 0.85), color=0x0)
    gui.show()

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:

p .\mass_spring_explicit.py
[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.11
[Taichi] Starting on arch=dx12
[W 08/27/23 17:31:04.526 39080] [program.cpp:taichi::lang::Program::Program@162] Out-of-bound access checking is not supported on arch=dx12
[E 08/27/23 17:31:04.873 39080] [llvm_program.cpp:taichi::lang::LlvmProgramImpl::make_kernel_launcher@147] Not supported.

***********************************
* Taichi Compiler Stack Traceback *
***********************************
0x7ffdc05ce959: taichi::Time::get_cycles in taichi_python.cp310-win_amd64.pyd
0x7ffdc0f70249: taichi::Logger::error in taichi_python.cp310-win_amd64.pyd
0x7ffdc06eb2e5: taichi::lang::directx12::make_aot_module in taichi_python.cp310-win_amd64.pyd
0x7ffdc04bde4f: taichi::lang::Program::enqueue_compute_op_lambda in taichi_python.cp310-win_amd64.pyd
0x7ffdc04b1827: taichi::lang::Program::compile in taichi_python.cp310-win_amd64.pyd
0x7ffdc0492c73: taichi::lang::Kernel::compile in taichi_python.cp310-win_amd64.pyd
0x7ffdc04c1172: taichi::lang::Program::enqueue_compute_op_lambda in taichi_python.cp310-win_amd64.pyd
0x7ffdc043bdd2: taichi::lang::aot::Module::get_snode_tree in taichi_python.cp310-win_amd64.pyd
0x7ffdc0173a88: taichi::ui::vulkan::Canvas::operator= in taichi_python.cp310-win_amd64.pyd
0x7ffdc0173bb4: taichi::ui::vulkan::Canvas::operator= in taichi_python.cp310-win_amd64.pyd
0x7ffdbffa7fa1: taichi::Logger::operator= in taichi_python.cp310-win_amd64.pyd
0x7ffe35ee9eea: PyObject_IsTrue in python310.dll
0x7ffe35f2ffbb: PyObject_MakeTpCall in python310.dll
0x7ffe3606bf8f: Py_gitversion in python310.dll
0x7ffe35f46033: PyEval_EvalFrameDefault in python310.dll
0x7ffe35f449d7: PyFunction_Vectorcall in python310.dll
0x7ffe35f2bfb0: PyVectorcall_Call in python310.dll
0x7ffe35f2be87: PyObject_Call in python310.dll
0x7ffe35f4b5e7: PyEval_EvalFrameDefault in python310.dll
0x7ffe35f449d7: PyFunction_Vectorcall in python310.dll
0x7ffe35f2bfb0: PyVectorcall_Call in python310.dll
0x7ffe35f2be87: PyObject_Call in python310.dll
0x7ffe35f4b5e7: PyEval_EvalFrameDefault in python310.dll
0x7ffe35f449d7: PyFunction_Vectorcall in python310.dll
0x7ffe35ef2e90: PyObject_DelItem in python310.dll
0x7ffe35f487a2: PyEval_EvalFrameDefault in python310.dll
0x7ffe35efa1cd: PyType_CalculateMetaclass in python310.dll
0x7ffe35f5ad06: PyEval_EvalCode in python310.dll
0x7ffe35f5a9ca: PyArena_New in python310.dll
0x7ffe35f5a94a: PyArena_New in python310.dll
0x7ffe35fdf394: PyRun_SimpleFileObject in python310.dll
0x7ffe35fdf249: PyRun_SimpleFileObject in python310.dll
0x7ffe35fddd20: PyRun_AnyFileObject in python310.dll
0x7ffe35fdd9a3: Py_MakePendingCalls in python310.dll
0x7ffe35fdd85f: Py_MakePendingCalls in python310.dll
0x7ffe35f53a30: Py_RunMain in python310.dll
0x7ffe35f538c1: Py_RunMain in python310.dll
0x7ffe35f532d9: Py_Main in python310.dll
0x7ff7d97b1230: Unknown Function in python.exe
0x7ffec39e26ad: BaseThreadInitThunk in KERNEL32.DLL
0x7ffec506aa68: RtlUserThreadStart in ntdll.dll

Internal error occurred. Check out this page for possible solutions:
https://docs.taichi-lang.org/docs/install
Traceback (most recent call last):
  File "mass_spring_explicit.py", line 71, in <module>
    spring_stiffness[None] = 10000
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\taichi\lang\util.py", line 321, in wrapped
    return func(*args, **kwargs)
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\taichi\lang\field.py", line 362, in __setitem__
    self.host_accessors[0].setter(value, *self._pad_key(key))
  File "C:\Users\xx\AppData\Local\Programs\Python\Python310\lib\site-packages\taichi\lang\field.py", line 409, in setter
    write_func(key, value)
RuntimeError: [llvm_program.cpp:taichi::lang::LlvmProgramImpl::make_kernel_launcher@147] Not supported.
...

Additional comments

 ti diagnose
[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.11

*******************************************
**      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.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
system: win32
executable: C:\Users\xx\AppData\Local\Programs\Python\Python310\python.exe
platform: Windows-10-10.0.22621-SP0
architecture: 64bit WindowsPE
uname: uname_result(system='Windows', node='chenkai', release='10', version='10.0.22621', machine='AMD64')
locale: en_US.cp936
PATH: C:\Users\xx\AppData\Local\oh-my-posh;C:\Program Files\PowerShell\7;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.2\libnvvp;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\dotnet\;C:\Program Files\IDM Computer Solutions\UltraCompare;C:\Program Files\Vim\vim82;C:\Program Files (x86)\GnuWin32\bin;C:\Prog\KTX-Software\bin;C:\Program Files\KTX-Software\bin;C:\Program Files\Git\cmd;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Paragon Software\APFS for Windows\;C:\Program Files\PowerShell\7\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\LLVM\bin;C:\Program Files\nodejs\;C:\Program Files\NVIDIA Corporation\Nsight Compute 2023.2.0\;C:\Program Files (x86)\Vim\vim82;C:\Users\xx\.cargo\bin;C:\Users\xx\AppData\Local\Programs\Python\Python310\Scripts\;C:\Users\xx\AppData\Local\Programs\Python\Python310\;C:\Users\xx\AppData\Local\Microsoft\WindowsApps;C:\Users\xx\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\xx\anaconda3\condabin;C:\mingw64\bin;C:\Program Files\shaderc\bin;C:\Users\xx\AppData\Local\GitHubDesktop\bin;C:\Users\xx\.dotnet\tools;C:\Users\xx\AppData\Roaming\npm;C:\Users\xx\anaconda3\envs\usd;c:\usd4\bin;c:\usd4\lib;C:\Users\xx\AppData\Local\Programs\oh-my-posh\bin;C:\Program Files\CMake\bin;C:\Users\xx\AppData\Local\Programs\Python\Python310\Lib\site-packages\taichi\_lib
PYTHONPATH: ['C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\Scripts\\ti.exe', 'c:\\usd4\\lib\\python', 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip', 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\DLLs', 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\lib', 'C:\\Users\\CK\\AppData\\Local\\Programs\\Python\\Python310', 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages', 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32', 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\win32\\lib', 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\Pythonwin']

`lsb_release` not available: [WinError 2] The system cannot find the file specified

import: <module 'taichi' from 'C:\\Users\\xx\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\taichi\\__init__.py'>

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

`glewinfo` not available: [WinError 2] The system cannot find the file specified

Sun Aug 27 17:34:28 2023
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 536.67                 Driver Version: 536.67       CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                     TCC/WDDM  | 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 3080 ...  WDDM  | 00000000:01:00.0 Off |                  N/A |
| N/A   40C    P0              33W / 165W |      0MiB / 16384MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
|  No running processes found                                                           |
+---------------------------------------------------------------------------------------+

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.11

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.11
[Taichi] Starting on arch=x64

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.11
[Taichi] Starting on arch=opengl

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.11
[Taichi] Starting on arch=cuda

[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.11

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

Consider attaching this log when maintainers ask about system information.
>>> Running time: 12.20s
lin-hitonami commented 10 months ago

DX12 backend is not mature right now. Please use vulkan backend instead.