taichi-dev / taichi

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

[Bug]"ti.sin()" doesn't work with a local variable. #1970

Closed Xayah-Hina closed 2 years ago

Xayah-Hina commented 4 years ago

Describe the bug

Hello, I found ti.sin() could not work with a local variable.

# 1.
ti.sin(9) # works

# 2. 
x=9
ti.sin(x)# error

[Environment]:

I'm new to Taichi, and wonder if this is just a feature or something?

Best regards.

To Reproduce

import taichi as ti
ti.init(arch=ti.gpu)

@ti.kernel
def foo():
  x = 9
  y = ti.sin(x)

foo()

Log/Screenshots

[Taichi] mode=release
[Taichi] preparing sandbox at /var/folders/wx/fdq0cyf948v4smwbqqk5j4pm0000gn/T/taichi-p54yb2n7
[Taichi] version 0.6.41, llvm 10.0.0, commit 706c5196, osx, python 3.7.7
[Taichi] Starting on arch=metal
[Taichi] materializing...
[E 10/17/20 16:05:25.449] [type_check.cpp:visit@186] [$9] Trigonometric operator takes real inputs only. At   File "05.py", line 14, in foo
    y = ti.sin(x)
  File "/usr/local/lib/python3.7/site-packages/taichi/lang/ops.py", line 48, in wrapped
    return imp_foo(a)
  File "/usr/local/lib/python3.7/site-packages/taichi/lang/ops.py", line 40, in imp_foo
    return foo(x)

                            * Taichi Core - Stack Traceback *                             
==========================================================================================
|                       Module |  Offset | Function                                      |
|----------------------------------------------------------------------------------------|
*               taichi_core.so |     110 | taichi::Logger::error(std::__1::basic_string< |
                                         | char, std::__1::char_traits<char>, std::__1:: |
                                         | allocator<char> > const&, bool)               |
*               taichi_core.so |     817 | taichi::lang::TypeCheck::visit(taichi::lang:: |
                                         | UnaryOpStmt*)                                 |
*               taichi_core.so |     396 | taichi::lang::TypeCheck::visit(taichi::lang:: |
                                         | Block*)                                       |
*               taichi_core.so |     134 | taichi::lang::irpass::type_check(taichi::lang |
                                         | ::IRNode*)                                    |
*               taichi_core.so |     618 | taichi::lang::irpass::compile_to_offloads(tai |
                                         | chi::lang::IRNode*, taichi::lang::CompileConf |
                                         | ig const&, bool, bool, bool, bool)            |
*               taichi_core.so |     149 | taichi::lang::irpass::compile_to_executable(t |
                                         | aichi::lang::IRNode*, taichi::lang::CompileCo |
                                         | nfig const&, bool, bool, bool, bool, bool, bo |
                                         | ol, bool)                                     |
*               taichi_core.so |     263 | taichi::lang::Kernel::lower(bool)             |
*               taichi_core.so |     196 | taichi::lang::Program::compile(taichi::lang:: |
                                         | Kernel&)                                      |
*               taichi_core.so |      62 | taichi::lang::Kernel::compile()               |
*               taichi_core.so |      56 | taichi::lang::Kernel::operator()(taichi::lang |
                                         | ::Kernel::LaunchContextBuilder&)              |
*               taichi_core.so |     180 | void pybind11::cpp_function::initialize<taich |
                                         | i::export_lang(pybind11::module&)::$_12, void |
                                         | , taichi::lang::Kernel*, taichi::lang::Kernel |
                                         | ::LaunchContextBuilder&, pybind11::name, pybi |
                                         | nd11::is_method, pybind11::sibling>(taichi::e |
                                         | xport_lang(pybind11::module&)::$_12&&, void ( |
                                         | *)(taichi::lang::Kernel*, taichi::lang::Kerne |
                                         | l::LaunchContextBuilder&), pybind11::name con |
                                         | st&, pybind11::is_method const&, pybind11::si |
                                         | bling const&)::'lambda'(pybind11::detail::fun |
                                         | ction_call&)::__invoke(pybind11::detail::func |
                                         | tion_call&)                                   |
*               taichi_core.so |    4075 | pybind11::cpp_function::dispatcher(_object*,  |
                                         | _object*, _object*)                           |
*                       Python |     516 | (null)                                        |
*                       Python |      41 | (null)                                        |
*                       Python |     131 | (null)                                        |
*                       Python |     136 | (null)                                        |
*                       Python |      92 | (null)                                        |
*                       Python |     358 | (null)                                        |
*                       Python |     730 | (null)                                        |
*                       Python |    6922 | (null)                                        |
*                       Python |    1698 | (null)                                        |
*                       Python |     444 | (null)                                        |
*                       Python |    7507 | (null)                                        |
*                       Python |    1698 | (null)                                        |
*                       Python |     444 | (null)                                        |
*                       Python |     131 | (null)                                        |
*                       Python |      71 | (null)                                        |
*                       Python |     136 | (null)                                        |
*                       Python |    7507 | (null)                                        |
*                       Python |    1698 | (null)                                        |
*                       Python |     212 | (null)                                        |
*                       Python |     737 | (null)                                        |
*                       Python |    6922 | (null)                                        |
*                       Python |    1698 | (null)                                        |
*                       Python |      51 | (null)                                        |
*                       Python |      54 | (null)                                        |
*                       Python |     160 | (null)                                        |
*                       Python |     270 | (null)                                        |
*                       Python |    5445 | (null)                                        |
*                       Python |      56 | (null)                                        |
*                libdyld.dylib |       1 | (null)                                        |
==========================================================================================

Internal error occurred. Check out this page for possible solutions:
https://taichi.readthedocs.io/en/stable/install.html#troubleshooting
Traceback (most recent call last):
  File "05.py", line 16, in <module>
    foo()
  File "/usr/local/lib/python3.7/site-packages/taichi/lang/kernel.py", line 574, in wrapped
    return primal(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/taichi/lang/kernel.py", line 502, in __call__
    return self.compiled_functions[key](*args)
  File "/usr/local/lib/python3.7/site-packages/taichi/lang/kernel.py", line 460, in func__
    t_kernel(launch_ctx)
RuntimeError: [type_check.cpp:visit@186] [$9] Trigonometric operator takes real inputs only. At   File "05.py", line 14, in foo
    y = ti.sin(x)
  File "/usr/local/lib/python3.7/site-packages/taichi/lang/ops.py", line 48, in wrapped
    return imp_foo(a)
  File "/usr/local/lib/python3.7/site-packages/taichi/lang/ops.py", line 40, in imp_foo
    return foo(x)

>>> Running time: 1.46s
archibate commented 4 years ago

As the error message suggests: Trigonometric operator takes real inputs only., ti.sin expects real numbers (i.e. floating-point numbers) as argument. We may use x = 9.0 to let x have a type float, therefore fix this problem. @xumingkuan I think we can actually cast the argument of int32/int64 to float32/float64 at this case?

Xayah-Hina commented 4 years ago

Thank you so much XD

xumingkuan commented 4 years ago

IMHO we don't want to allow the implicit cast of trigonometric operator's input to float-point values, because users may be writing degrees instead of radians.

For example, when a user writes ti.sin(45), it's very likely that what they really want is ti.sin(math.pi / 4). I think the error message can remind users to double-check errors like this...

If there exists some need for trigonometric functions of integral radians, maybe we can change the error to a warning then? (Of course, users can explicitly cast them now.)

bobcao3 commented 2 years ago

This is no longer a bug rn as it outputs a proper type error: 'sin' takes real inputs only, however 'i32' is provided