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

[bug] Accept numpy integers in ndrange (#5245) #5323

Merged
merged 2 commits into from
Jul 5, 2022
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
3 changes: 2 additions & 1 deletion python/taichi/lang/_ndrange.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import collections.abc

import numpy as np
from taichi.lang import ops
from taichi.lang.exception import TaichiSyntaxError, TaichiTypeError
from taichi.lang.expr import Expr
Expand All @@ -20,7 +21,7 @@ def __init__(self, *args):
args[i] = (args[i][0], ops.max(args[i][0], args[i][1]))
for arg in args:
for bound in arg:
if not isinstance(bound, int) and not (
if not isinstance(bound, (int, np.integer)) and not (
isinstance(bound, Expr)
and is_integral(bound.ptr.get_ret_type())):
raise TaichiTypeError(
Expand Down
24 changes: 24 additions & 0 deletions tests/python/test_ndrange.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,18 @@ def example():
example()


@test_utils.test()
def test_ndrange_should_accept_numpy_integer():
a, b = np.int64(0), np.int32(10)

@ti.kernel
def example():
for i in ti.ndrange((a, b)):
pass

example()


@test_utils.test()
def test_static_ndrange_non_integer_arguments():
@ti.kernel
Expand All @@ -291,3 +303,15 @@ def example():
r"Every argument of ndrange should be an integer scalar or a tuple/list of \(int, int\)"
):
example()


@test_utils.test()
def test_static_ndrange_should_accept_numpy_integer():
a, b = np.int64(0), np.int32(10)

@ti.kernel
def example():
for i in ti.static(ti.ndrange((a, b))):
pass

example()