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

@ti.func decorator calling for matrix when a vector is specified #8114

Open kurt-rhee opened 1 year ago

kurt-rhee commented 1 year ago

Describe the bug When specifying a vec4 in a function decorated with @ti.func, the function gives an error which states that a matrix is expected instead.

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.

from taichi.math import vec4

    point = vec4([1, 0, 0, 0])
    sun_vector = vec4([1, 1, 1, 1])

    @ti.kernel
    def project_system_kernel():

          # --- Project each point onto the plane ---
          new_point = project_point(point=point, sun_vector=sun_vector)

          return new_point

    @ti.func
    def project_point(
            point: vec4,
            sun_vector: vec4
    ) -> vec4:
        # --- Project point onto the normal vector ---
        projection = ti.math.dot(point, sun_vector) / ti.math.length(sun_vector)
        point_along_projection = sun_vector * projection
        new_point = point - point_along_projection
        return new_point

    new_point = project_system_kernel()
    print(new_point)

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 my_sample_code.py
[Taichi] version 1.6.0, llvm 15.0.1, commit f1c6fbbd, win, python 3.10.10
[W 05/31/23 16:56:57.483 20056] [cuda_driver.cpp:taichi::lang::CUDADriverBase::load_lib@36] nvcuda.dll lib not found.
[W 05/31/23 16:56:57.484 20056] [misc.py:adaptive_arch_select@753] Arch=[<Arch.cuda: 4>] is not supported, falling back to CPU
[Taichi] Starting on arch=x64

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.2\plugins\python-ce\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.2\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2022.3.2\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:\Users\krhee\PycharmProjects\shade-engine\main.py", line 175, in <module>
    new_point = project_system_kernel()
  File "C:\Users\krhee\PycharmProjects\shade-engine\_env\lib\site-packages\taichi\lang\kernel_impl.py", line 976, in wrapped
    raise type(e)("\n" + str(e)) from None
taichi.lang.exception.TaichiSyntaxError: 
File "C:\Users\krhee\PycharmProjects\shade-engine\main.py", line 156, in project_system_kernel:
            new_point = project_point(point=point, sun_vector=sun_vector)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\krhee\PycharmProjects\shade-engine\main.py", line 165, in project_point:
    def project_point(
    ^^^^^^^^^^^^^^^^^^
            point: vec4,
            ^^^^^^^^^^^^
            sun_vector: vec4
            ^^^^^^^^^^^^^^^^
    ) -> vec4:
    ^^^^^^^^^^
        # --- Project point onto the normal vector ---
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Argument point of type <taichi.lang.matrix.VectorType object at 0x000001A346314940> is expected to be a Matrix, but got <class 'taichi.lang.matrix.Vector'>.
...

Additional comments If possible, please also consider attaching the output of command ti diagnose. This produces the detailed environment information and hopefully helps us diagnose faster.

If you have local commits (e.g. compile fixes before you reproduce the bug), please make sure you first make a PR to fix the build errors and then report the bug.

kurt-rhee commented 1 year ago

@bobcao3 Thank you for the help!

kurt-rhee commented 1 year ago

I also want to note that just not type hinting the @ti.func function allows this program to run.

jim19930609 commented 1 year ago

Same issue with: https://github.com/taichi-dev/taichi/issues/8042. It's actually a regression with ti.func taking in Python-scope ti.Matrix/ti.Vector objects.

While I'm investigating the issue, a quick work-around is to put Python-scope variables point and sun_vector inside the kernel:

mport taichi as ti
from taichi.math import vec4

ti.init()

@ti.kernel
def project_system_kernel() -> vec4:

  point = vec4([1, 0, 0, 0]) 
  sun_vector = vec4([1, 1, 1, 1]) 

  # --- Project each point onto the plane ---
  new_point = project_point(point=point, sun_vector=sun_vector)

  return new_point

@ti.func
def project_point(
        point: vec4,
        sun_vector: vec4
) -> vec4:
    # --- Project point onto the normal vector ---
    projection = ti.math.dot(point, sun_vector) / ti.math.length(sun_vector)
    point_along_projection = sun_vector * projection
    new_point = point - point_along_projection
    return new_point

new_point = project_system_kernel()
print(new_point)

that should give you:

[Taichi] version 1.7.0, llvm 15.0.4, commit 41117f58, linux, python 3.10.6
[Taichi] Starting on arch=x64
[ 0.5 -0.5 -0.5 -0.5]