Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[error] Improve kernel argument type mismatch error message #1995

Merged
merged 2 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/taichi/lang/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def make_constant_expr(val):
else:
assert False
else:
raise ValueError(f'Bad constant scalar expression: {type(val)}')
raise ValueError(f'Invalid constant scalar expression: {type(val)}')


def reset():
Expand Down
10 changes: 4 additions & 6 deletions python/taichi/lang/kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,12 @@ def __init__(self, msg):

class KernelArgError(Exception):
def __init__(self, pos, needed, provided):
message = f'Argument {pos} (type={provided}) cannot be converted into required type {needed}'
super().__init__(message)
self.pos = pos
self.needed = needed
self.provided = provided

def message(self):
return 'Argument {} (type={}) cannot be converted into required type {}'.format(
self.pos, str(self.needed), str(self.provided))


def _get_global_vars(func):
# Discussions: https://github.com/taichi-dev/taichi/issues/282
Expand Down Expand Up @@ -393,11 +391,11 @@ def func__(*args):
# Note: do not use sth like "needed == f32". That would be slow.
if id(needed) in real_type_ids:
if not isinstance(v, (float, int)):
raise KernelArgError(i, needed, provided)
raise KernelArgError(i, needed.to_string(), provided)
launch_ctx.set_arg_float(actual_argument_slot, float(v))
elif id(needed) in integer_type_ids:
if not isinstance(v, int):
raise KernelArgError(i, needed, provided)
raise KernelArgError(i, needed.to_string(), provided)
launch_ctx.set_arg_int(actual_argument_slot, int(v))
elif self.match_ext_arr(v, needed):
has_external_arrays = True
Expand Down
1 change: 1 addition & 0 deletions taichi/python/export_lang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void export_lang(py::module &m) {
.def(py::init<Type *>())
.def(py::self == py::self)
.def("__hash__", &DataType::hash)
.def("to_string", &DataType::to_string)
.def(py::pickle(
[](const DataType &dt) {
// Note: this only works for primitive types, which is fine for now.
Expand Down
16 changes: 16 additions & 0 deletions tests/python/test_kernel_arg_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import taichi as ti
import pytest


@ti.test(arch=ti.cpu)
def test_pass_float_as_i32():
@ti.kernel
def foo(a: ti.i32):
pass

with pytest.raises(ti.KernelArgError) as e:
foo(1.2)

assert e.type is ti.KernelArgError
assert e.value.args[
0] == "Argument 0 (type=<class 'float'>) cannot be converted into required type i32"