Skip to content

Incorrect kernel argument validation and error message with ti.field #8747

@rafmudaf

Description

@rafmudaf

Describe the bug
The kernel type validation and error reporting system may have an error.

On the error message, the runtime error in kernel_impl.py's set_arg_ext_array function requires the type-hint to have the .to_string method:

raise TaichiRuntimeTypeError(
    f"Argument {needed.to_string()} cannot be converted into required type {v}"
)

However, it looks like Matrix and Vector are the only types that have it. Notably, it's missing in Field. This causes the error message to say this:

AttributeError: 'NdarrayType' object has no attribute 'to_string'

Instead of something like this:

Argument NdarrayType(dtype=None, ndim=None, layout=Layout.AOS, needs_grad=None) cannot be converted into required type <class 'taichi.lang.field.ScalarField'>

See the short code below to reproduce.

Additionally, I believe the checks for PyTorch and Paddle could result in an incorrect and difficult to track error. If the user has a Python environment activate with both Paddle and PyTorch installed and they are intending to use Paddle arrays, this flow control will error prior to executing the Paddle section:

        def set_arg_ext_array(indices, v, needed):
            ... redacted ...

            if isinstance(v, np.ndarray):
                if v.flags.c_contiguous:
                    ...
                elif v.flags.f_contiguous:
                    ...
                else:
                    raise ValueError(
                        "Non contiguous numpy arrays are not supported, please call np.ascontiguousarray(arr) "
                        "before passing it into taichi kernel."
                    )
            elif has_pytorch():
                if isinstance(v, torch.Tensor):
                    ...
-- bug? -->     else:
                    raise TaichiRuntimeTypeError(
                        f"Argument {needed.to_string()} cannot asdf be converted into required type {v}"
                    )
            elif has_paddle():
                if isinstance(v, paddle.Tensor):
                    ...
                else:
                    raise TaichiRuntimeTypeError(
                        f"Argument {needed.to_string()} cannot be converted into required type {v}"
                    )
            else:
                raise TaichiRuntimeTypeError(
                    # f"Argument {needed.to_string()} cannot be converted into required type {v}"
                    f"Argument {needed} cannot be converted into required type {type(v)}"
                )

To Reproduce

import taichi as ti

ti.init(arch=ti.cpu)

@ti.kernel
def kernel_example(arr: ti.types.ndarray()):
    pass

# arr = ti.ndarray(dtype=ti.f32, shape=(10,)) # Correct arg type to kernel
arr = ti.field(dtype=ti.f32, shape=(10,)) # Incorrect arg type to kernel
arr.fill(0.0)

kernel_example(arr)

Log/Screenshots

(raf) >> (gpu *$)$ python taichi_bug_demo.py 
[Taichi] version 1.7.3, llvm 15.0.7, commit 5ec301be, osx, python 3.12.1
[Taichi] Starting on arch=arm64
Traceback (most recent call last):
  File "taichi_bug_demo.py", line 14, in <module>
    kernel_example(arr)
  File "<removed>/miniforge3/envs/raf/lib/python3.12/site-packages/taichi/lang/kernel_impl.py", line 1114, in wrapped
    return primal(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "<removed>/miniforge3/envs/raf/lib/python3.12/site-packages/taichi/lang/kernel_impl.py", line 1046, in __call__
    return self.launch_kernel(kernel_cpp, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<removed>/miniforge3/envs/raf/lib/python3.12/site-packages/taichi/lang/kernel_impl.py", line 957, in launch_kernel
    recursive_set_args(needed_, type(val), val, (i - template_num,))
  File "<removed>/miniforge3/envs/raf/lib/python3.12/site-packages/taichi/lang/kernel_impl.py", line 935, in recursive_set_args
    set_arg_ext_array(indices, v, needed)
  File "<removed>/miniforge3/envs/raf/lib/python3.12/site-packages/taichi/lang/kernel_impl.py", line 835, in set_arg_ext_array
    f"Argument {needed.to_string()} cannot be converted into required type {v}"
                ^^^^^^^^^^^^^^^^
AttributeError: 'NdarrayType' object has no attribute 'to_string'

Additional comments
I'm happy to propose a fix and submit a pull request. Prior to that, though, I'd like to confirm with the development team that this is indeed an issue and not user error. Thanks!

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