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] [lang] Allow numpy int as snode dimension #6211

Merged
merged 3 commits into from
Oct 8, 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
8 changes: 4 additions & 4 deletions python/taichi/lang/snode.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def dense(self, axes, dimensions):
Returns:
The added :class:`~taichi.lang.SNode` instance.
"""
if isinstance(dimensions, int):
if isinstance(dimensions, numbers.Number):
dimensions = [dimensions] * len(axes)
return SNode(
self.ptr.dense(axes, dimensions,
Expand All @@ -46,7 +46,7 @@ def pointer(self, axes, dimensions):
Returns:
The added :class:`~taichi.lang.SNode` instance.
"""
if isinstance(dimensions, int):
if isinstance(dimensions, numbers.Number):
dimensions = [dimensions] * len(axes)
return SNode(
self.ptr.pointer(axes, dimensions,
Expand Down Expand Up @@ -90,7 +90,7 @@ def bitmasked(self, axes, dimensions):
Returns:
The added :class:`~taichi.lang.SNode` instance.
"""
if isinstance(dimensions, int):
if isinstance(dimensions, numbers.Number):
dimensions = [dimensions] * len(axes)
return SNode(
self.ptr.bitmasked(axes, dimensions,
Expand All @@ -107,7 +107,7 @@ def quant_array(self, axes, dimensions, max_num_bits):
Returns:
The added :class:`~taichi.lang.SNode` instance.
"""
if isinstance(dimensions, int):
if isinstance(dimensions, numbers.Number):
dimensions = [dimensions] * len(axes)
return SNode(
self.ptr.quant_array(axes, dimensions, max_num_bits,
Expand Down
12 changes: 12 additions & 0 deletions tests/python/test_fields_builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import numpy as np
import pytest
from taichi.lang.exception import TaichiRuntimeError

Expand Down Expand Up @@ -227,3 +228,14 @@ def calc_loss(arr: ti.template(), loss: ti.template()):
mul.grad(arr, out)
for i in range(10):
assert arr.grad[i] == 2.0


@test_utils.test(arch=ti.cpu)
def test_fields_builder_numpy_dimension():
shape = np.int32(5)
fb = ti.FieldsBuilder()
x = ti.field(ti.f32)
y = ti.field(ti.i32)
fb.dense(ti.i, shape).place(x)
fb.pointer(ti.j, shape).place(y)
fb.finalize()