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

[Misc] Strictly check ndim with external array #7126

Merged
merged 6 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/taichi/lang/_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _ndarray_matrix_from_numpy(self, arr, as_vector):
raise TypeError(f"{np.ndarray} expected, but {type(arr)} provided")
if tuple(self.arr.total_shape()) != tuple(arr.shape):
raise ValueError(
f"Mismatch shape: {tuple(self.arr.shape)} expected, but {tuple(arr.shape)} provided"
f"Mismatch shape: {tuple(self.arr.total_shape())} expected, but {tuple(arr.shape)} provided"
)
if not arr.flags.c_contiguous:
arr = np.ascontiguousarray(arr)
Expand Down
21 changes: 17 additions & 4 deletions python/taichi/lang/kernel_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,24 @@ def extract_arg(arg, anno):
shape = tuple(shape)
element_shape = ()
if isinstance(anno.dtype, MatrixType):
if len(shape) < anno.dtype.ndim:
raise ValueError(
f"Invalid argument into ti.types.ndarray() - required element_dim={anno.dtype.ndim}, "
f"but the argument has only {len(shape)} dimensions")
if anno.ndim is not None:
turbo0628 marked this conversation as resolved.
Show resolved Hide resolved
if len(shape) != anno.dtype.ndim + anno.ndim:
raise ValueError(
f"Invalid argument into ti.types.ndarray() - required array has ndim={anno.ndim} element_dim={anno.dtype.ndim}, "
f"but the argument has only {len(shape)} dimensions"
)
else:
if len(shape) < anno.dtype.ndim:
raise ValueError(
f"Invalid argument into ti.types.ndarray() - required element_dim={anno.dtype.ndim}, "
f"but the argument has only {len(shape)} dimensions"
)
element_shape = shape[-anno.dtype.ndim:]
if element_shape != anno.dtype.get_shape():
raise ValueError(
f"Invalid argument into ti.types.ndarray() - required element_shape={anno.dtype.get_shape()}, "
f"but the argument has element shape of {element_shape}"
)
return to_taichi_type(
arg.dtype), len(shape), element_shape, Layout.AOS
if isinstance(anno, sparse_matrix_builder):
Expand Down
3 changes: 0 additions & 3 deletions tests/python/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,6 @@ def func(a: ti.types.ndarray(ti.types.vector(n=10, dtype=ti.i32))):
v = np.zeros((6, 10), dtype=np.int32)
func(v)
assert impl.get_runtime().get_num_compiled_functions() == 1
v = np.zeros((6, 11), dtype=np.int32)
func(v)
assert impl.get_runtime().get_num_compiled_functions() == 2


@test_utils.test(arch=supported_archs_taichi_ndarray)
Expand Down