Internal Error occurred when using ti.f32 as if condition

import taichi as ti
ti.init()
tmp = ti.var(ti.f32, shape = ())
tmp[None] = 1.0
@ti.kernel
def fun():
    if tmp[None]:
        tmp[None] = 2.0
fun()

It seems that line 7 if tmp[None]: cause an internal error.
Is this an undefined behavior in Taichi scope?
Here is the result:

[Taichi] mode=release
[Taichi] version 0.6.16, llvm 8.0.1, commit 9c415674, python 3.8.3
[Taichi] Starting on arch=x64
[W 07/05/20 10:52:05.085] [type_check.cpp:taichi::lang::TypeCheck::visit@99] [$23] Local store may lose precision (target = i32 x1, value = f32 x1, at
[W 07/05/20 10:52:05.086] [type_check.cpp:taichi::lang::TypeCheck::visit@99] [$25] Local store may lose precision (target = i32 x1, value = f32 x1, at
[E 07/05/20 10:52:05.086] [type_check.cpp:taichi::lang::TypeCheck::visit@43] if_stmt->cond->ret_type.data_type == DataType::i32


***********************************
* Taichi Compiler Stack Traceback *
***********************************
0x7ffb123f91b1: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb124b72ea: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1245d5e9: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1245b91d: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1245a82b: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1240d221: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1232fa11: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb12340cb1: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1232ece6: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1232eb5f: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb12398b12: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1234b366: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb1227ea63: taichi::create_instance<taichi::Benchmark> in taichi_core.pyd
0x7ffb45408584: PyCFunction_Call in python38.dll
0x7ffb45409bba: PyObject_MakeTpCall in python38.dll
0x7ffb45537141: Py_gitversion in python38.dll
0x7ffb453ef4f2: PyVectorcall_Call in python38.dll
0x7ffb453edb35: PyObject_Call in python38.dll
0x7ffb45562313: Py_gitversion in python38.dll
0x7ffb45427706: PyEval_EvalFrameDefault in python38.dll
0x7ffb4541fc58: PyEval_EvalCodeWithName in python38.dll
0x7ffb4542143f: PyFunction_Vectorcall in python38.dll
    0x7ffb453ef4f2: PyVectorcall_Call in python38.dll
0x7ffb453ef383: PySequence_GetItem in python38.dll
0x7ffb45428119: PyEval_EvalFrameDefault in python38.dll
0x7ffb4541fc58: PyEval_EvalCodeWithName in python38.dll
0x7ffb4542143f: PyFunction_Vectorcall in python38.dll
0x7ffb453f9260: PyObject_FastCallDict in python38.dll
0x7ffb454e2e11: PyObject_Call_Prepend in python38.dll
0x7ffb454e2d81: PyDescr_NewGetSet in python38.dll
0x7ffb453ef3ff: PySequence_GetItem in python38.dll
0x7ffb45428119: PyEval_EvalFrameDefault in python38.dll
0x7ffb4541fc58: PyEval_EvalCodeWithName in python38.dll
0x7ffb4542654b: PyEval_EvalFrameDefault in python38.dll
0x7ffb4541fc58: PyEval_EvalCodeWithName in python38.dll
0x7ffb4548b3c3: PyEval_EvalCodeEx in python38.dll
0x7ffb4548b321: PyEval_EvalCode in python38.dll
0x7ffb45489fde: PyArena_New in python38.dll
0x7ffb45489f6d: PyArena_New in python38.dll
0x7ffb454ff078: PyRun_FileExFlags in python38.dll
0x7ffb454fe71f: PyRun_SimpleFileExFlags in python38.dll
0x7ffb455c724f: PyRun_AnyFileExFlags in python38.dll
0x7ffb45571c27: Py_gitversion in python38.dll
0x7ffb45454684: Py_RunMain in python38.dll
0x7ffb4545450d: Py_RunMain in python38.dll
0x7ff730083d7e: Unknown Function in python3.8.exe
0x7ff730087574: Unknown Function in python3.8.exe
0x7ffb63297bd4: BaseThreadInitThunk in KERNEL32.DLL
0x7ffb642ece51: RtlUserThreadStart in ntdll.dll

Internal Error occurred, check this page for possible solutions:
https://taichi.readthedocs.io/en/stable/install.html#troubleshooting
Traceback (most recent call last):
  File "test.py", line 9, in <module>
    fun()
  File "C:\Users\BillXu2000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\taichi
\lang\kernel.py", line 553, in wrapped
    return primal(*args, **kwargs)
  File "C:\Users\BillXu2000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\taichi
\lang\kernel.py", line 484, in __call__
    return self.compiled_functions[key](*args)
  File "C:\Users\BillXu2000\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\taichi
\lang\kernel.py", line 448, in func__
    t_kernel()
RuntimeError: [type_check.cpp:taichi::lang::TypeCheck::visit@43] if_stmt->cond->ret_type.data_type == DataType::i32

No, it’s not an UB: if_stmt->cond->ret_type.data_type == DataType::i32
The error message shows correctly that if cannot be used with types other than i32.

You may use if tmp[None] == 0: to fix it.

The reason why it’s i32 not i64 is bacause, Taichi consider boolean as i32, and the comparison operators will return i32 instead of a Python bool.

1 个赞