2021-11-17 09:11:41 +08:00
|
|
|
import copy
|
|
|
|
|
|
2021-08-14 16:11:25 +08:00
|
|
|
import numpy as np
|
2021-08-20 15:01:17 +08:00
|
|
|
import pytest
|
2022-01-27 10:06:37 +08:00
|
|
|
from taichi.lang import impl
|
2023-07-13 16:12:20 +08:00
|
|
|
from taichi.lang.exception import TaichiIndexError, TaichiRuntimeError, TaichiTypeError
|
2022-01-27 17:38:23 +08:00
|
|
|
from taichi.lang.misc import get_host_arch_list
|
2022-01-26 15:18:12 +08:00
|
|
|
from taichi.lang.util import has_pytorch
|
2023-05-13 18:14:12 +08:00
|
|
|
from taichi.math import vec3, ivec3
|
2021-08-14 16:11:25 +08:00
|
|
|
|
|
|
|
|
import taichi as ti
|
2022-02-10 12:37:36 +08:00
|
|
|
from tests import test_utils
|
2021-08-14 16:11:25 +08:00
|
|
|
|
2022-02-17 15:51:58 +08:00
|
|
|
if has_pytorch():
|
|
|
|
|
import torch
|
|
|
|
|
|
2021-08-30 21:48:26 +08:00
|
|
|
# properties
|
|
|
|
|
|
|
|
|
|
data_types = [ti.i32, ti.f32, ti.i64, ti.f64]
|
|
|
|
|
ndarray_shapes = [(), 8, (6, 12)]
|
|
|
|
|
vector_dims = [3]
|
|
|
|
|
matrix_dims = [(1, 2), (2, 3)]
|
2022-05-25 11:34:34 +08:00
|
|
|
supported_archs_taichi_ndarray = [
|
2023-02-06 13:54:49 +08:00
|
|
|
ti.cpu,
|
|
|
|
|
ti.cuda,
|
|
|
|
|
ti.opengl,
|
|
|
|
|
ti.vulkan,
|
|
|
|
|
ti.metal,
|
|
|
|
|
ti.amdgpu,
|
2022-05-25 11:34:34 +08:00
|
|
|
]
|
2021-08-30 21:48:26 +08:00
|
|
|
|
|
|
|
|
|
2021-10-19 19:33:03 +08:00
|
|
|
def _test_scalar_ndarray(dtype, shape):
|
2021-08-30 21:48:26 +08:00
|
|
|
x = ti.ndarray(dtype, shape)
|
|
|
|
|
|
|
|
|
|
if isinstance(shape, tuple):
|
|
|
|
|
assert x.shape == shape
|
|
|
|
|
else:
|
|
|
|
|
assert x.shape == (shape,)
|
2021-10-25 19:18:51 +08:00
|
|
|
assert x.element_shape == ()
|
2021-08-30 21:48:26 +08:00
|
|
|
|
|
|
|
|
assert x.dtype == dtype
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:33:03 +08:00
|
|
|
@pytest.mark.parametrize("dtype", data_types)
|
|
|
|
|
@pytest.mark.parametrize("shape", ndarray_shapes)
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=get_host_arch_list())
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_scalar_ndarray(dtype, shape):
|
|
|
|
|
_test_scalar_ndarray(dtype, shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _test_vector_ndarray(n, dtype, shape):
|
2021-08-30 21:48:26 +08:00
|
|
|
x = ti.Vector.ndarray(n, dtype, shape)
|
|
|
|
|
|
|
|
|
|
if isinstance(shape, tuple):
|
|
|
|
|
assert x.shape == shape
|
|
|
|
|
else:
|
|
|
|
|
assert x.shape == (shape,)
|
2021-10-25 19:18:51 +08:00
|
|
|
assert x.element_shape == (n,)
|
2021-08-30 21:48:26 +08:00
|
|
|
|
|
|
|
|
assert x.dtype == dtype
|
|
|
|
|
assert x.n == n
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:33:03 +08:00
|
|
|
@pytest.mark.parametrize("n", vector_dims)
|
|
|
|
|
@pytest.mark.parametrize("dtype", data_types)
|
|
|
|
|
@pytest.mark.parametrize("shape", ndarray_shapes)
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=get_host_arch_list())
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_vector_ndarray(n, dtype, shape):
|
|
|
|
|
_test_vector_ndarray(n, dtype, shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _test_matrix_ndarray(n, m, dtype, shape):
|
2021-08-30 21:48:26 +08:00
|
|
|
x = ti.Matrix.ndarray(n, m, dtype, shape)
|
|
|
|
|
|
|
|
|
|
if isinstance(shape, tuple):
|
|
|
|
|
assert x.shape == shape
|
|
|
|
|
else:
|
|
|
|
|
assert x.shape == (shape,)
|
2021-10-25 19:18:51 +08:00
|
|
|
assert x.element_shape == (n, m)
|
2021-08-30 21:48:26 +08:00
|
|
|
|
|
|
|
|
assert x.dtype == dtype
|
|
|
|
|
assert x.n == n
|
|
|
|
|
assert x.m == m
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:33:03 +08:00
|
|
|
@pytest.mark.parametrize("n,m", matrix_dims)
|
|
|
|
|
@pytest.mark.parametrize("dtype", data_types)
|
|
|
|
|
@pytest.mark.parametrize("shape", ndarray_shapes)
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=get_host_arch_list())
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_matrix_ndarray(n, m, dtype, shape):
|
|
|
|
|
_test_matrix_ndarray(n, m, dtype, shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [ti.f32, ti.f64])
|
2022-05-18 10:28:35 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_default_fp_ndarray(dtype):
|
2022-05-18 10:28:35 +08:00
|
|
|
arch = ti.lang.impl.current_cfg().arch
|
|
|
|
|
ti.reset()
|
|
|
|
|
ti.init(arch=arch, default_fp=dtype)
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
x = ti.Vector.ndarray(2, float, ())
|
|
|
|
|
|
2022-01-27 10:06:37 +08:00
|
|
|
assert x.dtype == impl.get_runtime().default_fp
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [ti.i32, ti.i64])
|
2022-05-18 10:28:35 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_default_ip_ndarray(dtype):
|
2022-05-18 10:28:35 +08:00
|
|
|
arch = ti.lang.impl.current_cfg().arch
|
|
|
|
|
ti.reset()
|
|
|
|
|
ti.init(arch=arch, default_ip=dtype)
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
x = ti.Vector.ndarray(2, int, ())
|
|
|
|
|
|
2022-01-27 10:06:37 +08:00
|
|
|
assert x.dtype == impl.get_runtime().default_ip
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_ndarray_1d():
|
|
|
|
|
n = 4
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
2022-04-01 17:53:06 +08:00
|
|
|
def run(x: ti.types.ndarray(), y: ti.types.ndarray()):
|
2021-10-19 19:33:03 +08:00
|
|
|
for i in range(n):
|
|
|
|
|
x[i] += i + y[i]
|
|
|
|
|
|
|
|
|
|
a = ti.ndarray(ti.i32, shape=(n,))
|
|
|
|
|
for i in range(n):
|
|
|
|
|
a[i] = i * i
|
|
|
|
|
b = np.ones((n,), dtype=np.int32)
|
|
|
|
|
run(a, b)
|
|
|
|
|
for i in range(n):
|
|
|
|
|
assert a[i] == i * i + i + 1
|
|
|
|
|
run(b, a)
|
|
|
|
|
for i in range(n):
|
|
|
|
|
assert b[i] == i * i + (i + 1) * 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _test_ndarray_2d():
|
2021-08-17 13:09:33 +08:00
|
|
|
n = 4
|
|
|
|
|
m = 7
|
2021-08-14 16:11:25 +08:00
|
|
|
|
|
|
|
|
@ti.kernel
|
2022-04-01 17:53:06 +08:00
|
|
|
def run(x: ti.types.ndarray(), y: ti.types.ndarray()):
|
2021-08-14 16:11:25 +08:00
|
|
|
for i in range(n):
|
|
|
|
|
for j in range(m):
|
2021-08-17 13:09:33 +08:00
|
|
|
x[i, j] += i + j + y[i, j]
|
2021-08-14 16:11:25 +08:00
|
|
|
|
2021-08-17 13:09:33 +08:00
|
|
|
a = ti.ndarray(ti.i32, shape=(n, m))
|
2021-08-14 16:11:25 +08:00
|
|
|
for i in range(n):
|
|
|
|
|
for j in range(m):
|
|
|
|
|
a[i, j] = i * j
|
2021-08-17 13:09:33 +08:00
|
|
|
b = np.ones((n, m), dtype=np.int32)
|
|
|
|
|
run(a, b)
|
2021-08-14 16:11:25 +08:00
|
|
|
for i in range(n):
|
|
|
|
|
for j in range(m):
|
2021-08-17 13:09:33 +08:00
|
|
|
assert a[i, j] == i * j + i + j + 1
|
|
|
|
|
run(b, a)
|
|
|
|
|
for i in range(n):
|
|
|
|
|
for j in range(m):
|
|
|
|
|
assert b[i, j] == i * j + (i + j + 1) * 2
|
2021-08-30 21:48:26 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_ndarray_2d():
|
|
|
|
|
_test_ndarray_2d()
|
|
|
|
|
|
|
|
|
|
|
2022-05-31 20:38:51 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_compound_element():
|
|
|
|
|
n = 10
|
|
|
|
|
a = ti.ndarray(ti.i32, shape=(n,))
|
|
|
|
|
|
|
|
|
|
vec3 = ti.types.vector(3, ti.i32)
|
|
|
|
|
b = ti.ndarray(vec3, shape=(n, n))
|
2022-10-31 17:56:46 +08:00
|
|
|
assert isinstance(b, ti.VectorNdarray)
|
2022-05-31 20:38:51 +08:00
|
|
|
assert b.shape == (n, n)
|
2022-08-30 09:24:20 +08:00
|
|
|
assert b.element_type.element_type() == ti.i32
|
2023-05-15 16:09:43 +08:00
|
|
|
assert b.element_type.shape() == [3]
|
2022-05-31 20:38:51 +08:00
|
|
|
|
|
|
|
|
matrix34 = ti.types.matrix(3, 4, float)
|
2022-09-15 08:39:38 +08:00
|
|
|
c = ti.ndarray(matrix34, shape=(n, n + 1))
|
2022-05-31 20:38:51 +08:00
|
|
|
assert isinstance(c, ti.MatrixNdarray)
|
|
|
|
|
assert c.shape == (n, n + 1)
|
2022-08-30 09:24:20 +08:00
|
|
|
assert c.element_type.element_type() == ti.f32
|
2023-05-15 16:09:43 +08:00
|
|
|
assert c.element_type.shape() == [3, 4]
|
2022-05-31 20:38:51 +08:00
|
|
|
|
|
|
|
|
|
2022-12-13 10:12:48 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_copy_from_ndarray():
|
2021-11-17 09:11:41 +08:00
|
|
|
n = 16
|
|
|
|
|
a = ti.ndarray(ti.i32, shape=n)
|
|
|
|
|
b = ti.ndarray(ti.i32, shape=n)
|
|
|
|
|
a[0] = 1
|
|
|
|
|
a[4] = 2
|
|
|
|
|
b[0] = 4
|
|
|
|
|
b[4] = 5
|
|
|
|
|
|
|
|
|
|
a.copy_from(b)
|
|
|
|
|
|
|
|
|
|
assert a[0] == 4
|
|
|
|
|
assert a[4] == 5
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
x = ti.Vector.ndarray(10, ti.i32, 5)
|
|
|
|
|
y = ti.Vector.ndarray(10, ti.i32, 5)
|
2021-11-17 09:11:41 +08:00
|
|
|
x[1][0] = 1
|
|
|
|
|
x[2][4] = 2
|
|
|
|
|
y[1][0] = 4
|
|
|
|
|
y[2][4] = 5
|
|
|
|
|
|
|
|
|
|
x.copy_from(y)
|
|
|
|
|
|
|
|
|
|
assert x[1][0] == 4
|
|
|
|
|
assert x[2][4] == 5
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
x = ti.Matrix.ndarray(2, 2, ti.i32, 5)
|
|
|
|
|
y = ti.Matrix.ndarray(2, 2, ti.i32, 5)
|
2021-11-17 09:11:41 +08:00
|
|
|
x[0][0, 0] = 1
|
|
|
|
|
x[4][1, 0] = 3
|
|
|
|
|
y[0][0, 0] = 4
|
|
|
|
|
y[4][1, 0] = 6
|
|
|
|
|
|
|
|
|
|
x.copy_from(y)
|
|
|
|
|
|
|
|
|
|
assert x[0][0, 0] == 4
|
|
|
|
|
assert x[4][1, 0] == 6
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2022-12-13 10:12:48 +08:00
|
|
|
def test_ndarray_deepcopy():
|
2021-11-17 09:11:41 +08:00
|
|
|
n = 16
|
|
|
|
|
x = ti.ndarray(ti.i32, shape=n)
|
|
|
|
|
x[0] = 1
|
|
|
|
|
x[4] = 2
|
|
|
|
|
|
|
|
|
|
y = copy.deepcopy(x)
|
|
|
|
|
|
|
|
|
|
assert y.shape == x.shape
|
|
|
|
|
assert y.dtype == x.dtype
|
|
|
|
|
assert y[0] == 1
|
|
|
|
|
assert y[4] == 2
|
|
|
|
|
x[0] = 4
|
|
|
|
|
x[4] = 5
|
|
|
|
|
assert y[0] == 1
|
|
|
|
|
assert y[4] == 2
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
x = ti.Vector.ndarray(10, ti.i32, 5)
|
2021-11-17 09:11:41 +08:00
|
|
|
x[1][0] = 4
|
|
|
|
|
x[2][4] = 5
|
|
|
|
|
|
|
|
|
|
y = copy.deepcopy(x)
|
|
|
|
|
|
|
|
|
|
assert y.shape == x.shape
|
|
|
|
|
assert y.dtype == x.dtype
|
|
|
|
|
assert y.n == x.n
|
|
|
|
|
assert y[1][0] == 4
|
|
|
|
|
assert y[2][4] == 5
|
|
|
|
|
x[1][0] = 1
|
|
|
|
|
x[2][4] = 2
|
|
|
|
|
assert y[1][0] == 4
|
|
|
|
|
assert y[2][4] == 5
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
x = ti.Matrix.ndarray(2, 2, ti.i32, 5)
|
2021-11-17 09:11:41 +08:00
|
|
|
x[0][0, 0] = 7
|
|
|
|
|
x[4][1, 0] = 9
|
|
|
|
|
|
|
|
|
|
y = copy.deepcopy(x)
|
|
|
|
|
|
|
|
|
|
assert y.shape == x.shape
|
|
|
|
|
assert y.dtype == x.dtype
|
|
|
|
|
assert y.m == x.m
|
|
|
|
|
assert y.n == x.n
|
|
|
|
|
assert y[0][0, 0] == 7
|
|
|
|
|
assert y[4][1, 0] == 9
|
|
|
|
|
x[0][0, 0] = 3
|
|
|
|
|
x[4][1, 0] = 5
|
|
|
|
|
assert y[0][0, 0] == 7
|
|
|
|
|
assert y[4][1, 0] == 9
|
|
|
|
|
|
|
|
|
|
|
2023-05-08 16:52:51 +08:00
|
|
|
@test_utils.test(arch=[ti.cuda])
|
2023-03-15 16:14:48 +08:00
|
|
|
def test_ndarray_caching_allocator():
|
2021-12-20 11:03:46 +08:00
|
|
|
n = 8
|
|
|
|
|
a = ti.ndarray(ti.i32, shape=(n))
|
|
|
|
|
a.fill(2)
|
|
|
|
|
a = 1
|
|
|
|
|
b = ti.ndarray(ti.i32, shape=(n))
|
|
|
|
|
b.fill(2)
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-12-31 13:19:53 +08:00
|
|
|
def test_ndarray_fill():
|
|
|
|
|
n = 8
|
|
|
|
|
a = ti.ndarray(ti.i32, shape=(n))
|
2022-01-03 23:48:33 +08:00
|
|
|
anp = np.ones((n,), dtype=np.int32)
|
2021-12-31 13:19:53 +08:00
|
|
|
a.fill(2)
|
2022-01-03 23:48:33 +08:00
|
|
|
anp.fill(2)
|
|
|
|
|
assert (a.to_numpy() == anp).all()
|
|
|
|
|
|
|
|
|
|
b = ti.Vector.ndarray(4, ti.f32, shape=(n))
|
2022-06-02 10:43:46 +08:00
|
|
|
bnp = np.ones(shape=b.arr.total_shape(), dtype=np.float32)
|
2022-01-03 23:48:33 +08:00
|
|
|
b.fill(2.5)
|
|
|
|
|
bnp.fill(2.5)
|
|
|
|
|
assert (b.to_numpy() == bnp).all()
|
|
|
|
|
|
|
|
|
|
c = ti.Matrix.ndarray(4, 4, ti.f32, shape=(n))
|
2022-06-02 10:43:46 +08:00
|
|
|
cnp = np.ones(shape=c.arr.total_shape(), dtype=np.float32)
|
2022-01-03 23:48:33 +08:00
|
|
|
c.fill(1.5)
|
|
|
|
|
cnp.fill(1.5)
|
|
|
|
|
assert (c.to_numpy() == cnp).all()
|
2021-12-31 13:19:53 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-12-15 13:26:10 +08:00
|
|
|
def test_ndarray_rw_cache():
|
|
|
|
|
a = ti.Vector.ndarray(3, ti.f32, ())
|
|
|
|
|
b = ti.Vector.ndarray(3, ti.f32, 12)
|
|
|
|
|
|
2022-11-15 14:28:48 +08:00
|
|
|
n = 100
|
2021-12-15 13:26:10 +08:00
|
|
|
for i in range(n):
|
|
|
|
|
c_a = copy.deepcopy(a)
|
|
|
|
|
c_b = copy.deepcopy(b)
|
|
|
|
|
c_a[None] = c_b[10]
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:33:03 +08:00
|
|
|
def _test_ndarray_numpy_io():
|
2021-09-02 15:19:54 +08:00
|
|
|
n = 7
|
|
|
|
|
m = 4
|
|
|
|
|
a = ti.ndarray(ti.i32, shape=(n, m))
|
|
|
|
|
a.fill(2)
|
|
|
|
|
b = ti.ndarray(ti.i32, shape=(n, m))
|
|
|
|
|
b.from_numpy(np.ones((n, m), dtype=np.int32) * 2)
|
|
|
|
|
assert (a.to_numpy() == b.to_numpy()).all()
|
|
|
|
|
|
2021-11-18 22:40:38 +08:00
|
|
|
d = 2
|
|
|
|
|
p = 4
|
|
|
|
|
x = ti.Vector.ndarray(d, ti.f32, p)
|
|
|
|
|
x.fill(2)
|
|
|
|
|
y = ti.Vector.ndarray(d, ti.f32, p)
|
|
|
|
|
y.from_numpy(np.ones((p, d), dtype=np.int32) * 2)
|
|
|
|
|
assert (x.to_numpy() == y.to_numpy()).all()
|
|
|
|
|
|
|
|
|
|
c = 2
|
|
|
|
|
d = 2
|
|
|
|
|
p = 4
|
|
|
|
|
x = ti.Matrix.ndarray(c, d, ti.f32, p)
|
|
|
|
|
x.fill(2)
|
|
|
|
|
y = ti.Matrix.ndarray(c, d, ti.f32, p)
|
|
|
|
|
y.from_numpy(np.ones((p, c, d), dtype=np.int32) * 2)
|
|
|
|
|
assert (x.to_numpy() == y.to_numpy()).all()
|
|
|
|
|
|
2021-09-02 15:19:54 +08:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_ndarray_numpy_io():
|
|
|
|
|
_test_ndarray_numpy_io()
|
|
|
|
|
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_matrix_numpy_io():
|
2022-03-22 14:41:04 +08:00
|
|
|
n = 5
|
|
|
|
|
m = 2
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
x = ti.Vector.ndarray(n, ti.i32, (m,))
|
|
|
|
|
x_np = 1 + np.arange(n * m).reshape(m, n).astype(np.int32)
|
2022-03-22 14:41:04 +08:00
|
|
|
x.from_numpy(x_np)
|
|
|
|
|
assert (x_np.flatten() == x.to_numpy().flatten()).all()
|
|
|
|
|
|
|
|
|
|
k = 2
|
2022-09-15 08:39:38 +08:00
|
|
|
x = ti.Matrix.ndarray(m, k, ti.i32, n)
|
|
|
|
|
x_np = 1 + np.arange(m * k * n).reshape(n, m, k).astype(np.int32)
|
2022-03-22 14:41:04 +08:00
|
|
|
x.from_numpy(x_np)
|
|
|
|
|
assert (x_np.flatten() == x.to_numpy().flatten()).all()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2022-09-15 08:39:38 +08:00
|
|
|
def test_matrix_ndarray_python_scope():
|
|
|
|
|
a = ti.Matrix.ndarray(2, 2, ti.i32, 5)
|
2021-08-30 21:48:26 +08:00
|
|
|
for i in range(5):
|
|
|
|
|
for j, k in ti.ndrange(2, 2):
|
|
|
|
|
a[i][j, k] = j * j + k * k
|
|
|
|
|
assert a[0][0, 0] == 0
|
|
|
|
|
assert a[1][0, 1] == 1
|
|
|
|
|
assert a[2][1, 0] == 1
|
|
|
|
|
assert a[3][1, 1] == 2
|
|
|
|
|
assert a[4][0, 1] == 1
|
|
|
|
|
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
def _test_matrix_ndarray_taichi_scope():
|
2021-08-30 21:48:26 +08:00
|
|
|
@ti.kernel
|
2022-04-01 17:53:06 +08:00
|
|
|
def func(a: ti.types.ndarray()):
|
2021-08-30 21:48:26 +08:00
|
|
|
for i in range(5):
|
|
|
|
|
for j, k in ti.ndrange(2, 2):
|
|
|
|
|
a[i][j, k] = j * j + k * k
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
m = ti.Matrix.ndarray(2, 2, ti.i32, 5)
|
2021-08-30 21:48:26 +08:00
|
|
|
func(m)
|
|
|
|
|
assert m[0][0, 0] == 0
|
|
|
|
|
assert m[1][0, 1] == 1
|
|
|
|
|
assert m[2][1, 0] == 1
|
|
|
|
|
assert m[3][1, 1] == 2
|
|
|
|
|
assert m[4][0, 1] == 1
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2022-09-15 08:39:38 +08:00
|
|
|
def test_matrix_ndarray_taichi_scope():
|
|
|
|
|
_test_matrix_ndarray_taichi_scope()
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
|
2022-12-13 10:12:48 +08:00
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda], real_matrix_scalarize=False)
|
2022-09-07 07:34:49 +08:00
|
|
|
def test_matrix_ndarray_taichi_scope_real_matrix():
|
2022-09-15 08:39:38 +08:00
|
|
|
_test_matrix_ndarray_taichi_scope()
|
2022-09-07 07:34:49 +08:00
|
|
|
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
def _test_matrix_ndarray_taichi_scope_struct_for():
|
2021-09-02 10:47:29 +08:00
|
|
|
@ti.kernel
|
2022-04-01 17:53:06 +08:00
|
|
|
def func(a: ti.types.ndarray()):
|
2021-09-02 10:47:29 +08:00
|
|
|
for i in a:
|
|
|
|
|
for j, k in ti.ndrange(2, 2):
|
|
|
|
|
a[i][j, k] = j * j + k * k
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
m = ti.Matrix.ndarray(2, 2, ti.i32, 5)
|
2021-09-02 10:47:29 +08:00
|
|
|
func(m)
|
|
|
|
|
assert m[0][0, 0] == 0
|
|
|
|
|
assert m[1][0, 1] == 1
|
|
|
|
|
assert m[2][1, 0] == 1
|
|
|
|
|
assert m[3][1, 1] == 2
|
|
|
|
|
assert m[4][0, 1] == 1
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2022-09-15 08:39:38 +08:00
|
|
|
def test_matrix_ndarray_taichi_scope_struct_for():
|
|
|
|
|
_test_matrix_ndarray_taichi_scope_struct_for()
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
|
2022-12-13 10:12:48 +08:00
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda], real_matrix_scalarize=False)
|
2022-09-07 07:34:49 +08:00
|
|
|
def test_matrix_ndarray_taichi_scope_struct_for_real_matrix():
|
2022-09-15 08:39:38 +08:00
|
|
|
_test_matrix_ndarray_taichi_scope_struct_for()
|
2022-09-07 07:34:49 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2022-09-15 08:39:38 +08:00
|
|
|
def test_vector_ndarray_python_scope():
|
|
|
|
|
a = ti.Vector.ndarray(10, ti.i32, 5)
|
2021-11-04 16:04:22 +08:00
|
|
|
for i in range(5):
|
|
|
|
|
for j in range(4):
|
|
|
|
|
a[i][j * j] = j * j
|
|
|
|
|
assert a[0][9] == 9
|
|
|
|
|
assert a[1][0] == 0
|
|
|
|
|
assert a[2][1] == 1
|
|
|
|
|
assert a[3][4] == 4
|
|
|
|
|
assert a[4][9] == 9
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
def _test_vector_ndarray_taichi_scope():
|
2021-11-04 16:04:22 +08:00
|
|
|
@ti.kernel
|
2022-04-01 17:53:06 +08:00
|
|
|
def func(a: ti.types.ndarray()):
|
2021-11-04 16:04:22 +08:00
|
|
|
for i in range(5):
|
|
|
|
|
for j in range(4):
|
|
|
|
|
a[i][j * j] = j * j
|
|
|
|
|
|
2022-09-15 08:39:38 +08:00
|
|
|
v = ti.Vector.ndarray(10, ti.i32, 5)
|
2021-11-04 16:04:22 +08:00
|
|
|
func(v)
|
|
|
|
|
assert v[0][9] == 9
|
|
|
|
|
assert v[1][0] == 0
|
|
|
|
|
assert v[2][1] == 1
|
|
|
|
|
assert v[3][4] == 4
|
|
|
|
|
assert v[4][9] == 9
|
2021-10-19 19:33:03 +08:00
|
|
|
|
|
|
|
|
|
2022-09-07 07:34:49 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2022-09-15 08:39:38 +08:00
|
|
|
def test_vector_ndarray_taichi_scope():
|
|
|
|
|
_test_vector_ndarray_taichi_scope()
|
2021-08-30 21:48:26 +08:00
|
|
|
|
|
|
|
|
|
2022-12-13 10:12:48 +08:00
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda], real_matrix_scalarize=False)
|
2022-09-07 07:34:49 +08:00
|
|
|
def test_vector_ndarray_taichi_scope_real_matrix():
|
2022-09-15 08:39:38 +08:00
|
|
|
_test_vector_ndarray_taichi_scope()
|
2022-09-07 07:34:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# number of compiled functions
|
2021-10-19 19:33:03 +08:00
|
|
|
def _test_compiled_functions():
|
2021-08-30 21:48:26 +08:00
|
|
|
@ti.kernel
|
2022-11-18 15:59:55 +08:00
|
|
|
def func(a: ti.types.ndarray(ti.types.vector(n=10, dtype=ti.i32))):
|
2021-08-30 21:48:26 +08:00
|
|
|
for i in range(5):
|
|
|
|
|
for j in range(4):
|
|
|
|
|
a[i][j * j] = j * j
|
|
|
|
|
|
|
|
|
|
v = ti.Vector.ndarray(10, ti.i32, 5)
|
|
|
|
|
func(v)
|
2022-01-27 10:06:37 +08:00
|
|
|
assert impl.get_runtime().get_num_compiled_functions() == 1
|
2021-08-30 21:48:26 +08:00
|
|
|
v = np.zeros((6, 10), dtype=np.int32)
|
|
|
|
|
func(v)
|
2022-01-27 10:06:37 +08:00
|
|
|
assert impl.get_runtime().get_num_compiled_functions() == 1
|
2021-09-06 19:38:26 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_compiled_functions():
|
|
|
|
|
_test_compiled_functions()
|
|
|
|
|
|
|
|
|
|
|
2021-09-06 19:38:26 +08:00
|
|
|
# annotation compatibility
|
|
|
|
|
|
|
|
|
|
|
2021-10-19 19:33:03 +08:00
|
|
|
def _test_arg_not_match():
|
2021-09-06 19:38:26 +08:00
|
|
|
@ti.kernel
|
2022-11-18 15:59:55 +08:00
|
|
|
def func1(a: ti.types.ndarray(dtype=ti.types.vector(2, ti.i32))):
|
2021-09-06 19:38:26 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
x = ti.Matrix.ndarray(2, 3, ti.i32, shape=(4, 7))
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid value for argument a - required element type: VectorType\[2, i32\], but .* is provided",
|
2023-05-15 18:11:24 +08:00
|
|
|
):
|
|
|
|
|
func1(x)
|
|
|
|
|
|
|
|
|
|
x = ti.Matrix.ndarray(2, 1, ti.i32, shape=(4, 7))
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid value for argument a - required element type: VectorType\[2, i32\], but .* is provided",
|
2021-09-06 19:38:26 +08:00
|
|
|
):
|
|
|
|
|
func1(x)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
2022-11-18 15:59:55 +08:00
|
|
|
def func2(a: ti.types.ndarray(dtype=ti.types.matrix(2, 2, ti.i32))):
|
2021-09-06 19:38:26 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
x = ti.Vector.ndarray(2, ti.i32, shape=(4, 7))
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid value for argument a - required element type: MatrixType\[2,2, i32\], but .* is provided",
|
2021-09-06 19:38:26 +08:00
|
|
|
):
|
|
|
|
|
func2(x)
|
|
|
|
|
|
2023-05-15 18:11:24 +08:00
|
|
|
@ti.kernel
|
|
|
|
|
def func3(a: ti.types.ndarray(dtype=ti.types.matrix(2, 1, ti.i32))):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
x = ti.Vector.ndarray(2, ti.i32, shape=(4, 7))
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid value for argument a - required element type: MatrixType\[2,1, i32\], but .* is provided",
|
2023-05-15 18:11:24 +08:00
|
|
|
):
|
|
|
|
|
func3(x)
|
|
|
|
|
|
2021-12-02 19:53:04 -05:00
|
|
|
@ti.kernel
|
2022-11-18 15:59:55 +08:00
|
|
|
def func5(a: ti.types.ndarray(dtype=ti.types.matrix(2, 3, dtype=ti.i32))):
|
2021-12-02 19:53:04 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
x = ti.Vector.ndarray(2, ti.i32, shape=(4, 7))
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid value for argument a - required element type",
|
2022-03-23 13:51:44 +08:00
|
|
|
):
|
2021-12-02 19:53:04 -05:00
|
|
|
func5(x)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
2022-11-22 09:48:35 +08:00
|
|
|
def func7(a: ti.types.ndarray(ndim=2)):
|
2021-12-02 19:53:04 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
x = ti.ndarray(ti.i32, shape=(3,))
|
|
|
|
|
with pytest.raises(
|
|
|
|
|
ValueError,
|
2023-06-14 14:01:43 +08:00
|
|
|
match=r"Invalid value for argument a - required ndim",
|
2022-11-22 09:48:35 +08:00
|
|
|
):
|
2021-12-02 19:53:04 -05:00
|
|
|
func7(x)
|
|
|
|
|
|
2022-11-21 12:43:39 +08:00
|
|
|
@ti.kernel
|
|
|
|
|
def func8(x: ti.types.ndarray(dtype=ti.f32)):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
x = ti.ndarray(dtype=ti.i32, shape=(16, 16))
|
2023-06-14 14:01:43 +08:00
|
|
|
with pytest.raises(TypeError, match=r"Expect element type .* for argument x, but get .*"):
|
2022-11-21 12:43:39 +08:00
|
|
|
func8(x)
|
|
|
|
|
|
2021-10-19 19:33:03 +08:00
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=get_host_arch_list())
|
2021-10-19 19:33:03 +08:00
|
|
|
def test_arg_not_match():
|
|
|
|
|
_test_arg_not_match()
|
2021-11-18 13:55:37 +08:00
|
|
|
|
|
|
|
|
|
2021-11-22 11:18:14 +08:00
|
|
|
def _test_size_in_bytes():
|
|
|
|
|
a = ti.ndarray(ti.i32, 8)
|
2022-02-16 15:53:44 +08:00
|
|
|
assert a._get_element_size() == 4
|
|
|
|
|
assert a._get_nelement() == 8
|
2021-11-22 11:18:14 +08:00
|
|
|
|
|
|
|
|
b = ti.Vector.ndarray(10, ti.f64, 5)
|
2022-06-01 16:00:30 +08:00
|
|
|
assert b._get_element_size() == 80
|
|
|
|
|
assert b._get_nelement() == 5
|
2021-11-22 11:18:14 +08:00
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda])
|
2021-11-22 11:18:14 +08:00
|
|
|
def test_size_in_bytes():
|
|
|
|
|
_test_size_in_bytes()
|
|
|
|
|
|
|
|
|
|
|
2022-02-10 12:37:36 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2021-11-23 21:08:44 -05:00
|
|
|
def test_different_shape():
|
|
|
|
|
n1 = 4
|
|
|
|
|
x = ti.ndarray(dtype=ti.f32, shape=(n1, n1))
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
2022-04-01 17:53:06 +08:00
|
|
|
def init(d: ti.i32, arr: ti.types.ndarray()):
|
2021-11-23 21:08:44 -05:00
|
|
|
for i, j in arr:
|
|
|
|
|
arr[i, j] = d
|
|
|
|
|
|
|
|
|
|
init(2, x)
|
|
|
|
|
assert (x.to_numpy() == (np.ones(shape=(n1, n1)) * 2)).all()
|
|
|
|
|
n2 = 8
|
|
|
|
|
y = ti.ndarray(dtype=ti.f32, shape=(n2, n2))
|
|
|
|
|
init(3, y)
|
|
|
|
|
assert (y.to_numpy() == (np.ones(shape=(n2, n2)) * 3)).all()
|
2022-07-14 11:50:31 +08:00
|
|
|
|
|
|
|
|
|
2022-09-07 07:34:49 +08:00
|
|
|
def _test_ndarray_grouped():
|
2022-07-14 11:50:31 +08:00
|
|
|
@ti.kernel
|
|
|
|
|
def func(a: ti.types.ndarray()):
|
|
|
|
|
for i in ti.grouped(a):
|
|
|
|
|
for j, k in ti.ndrange(2, 2):
|
|
|
|
|
a[i][j, k] = j * j
|
|
|
|
|
|
|
|
|
|
a1 = ti.Matrix.ndarray(2, 2, ti.i32, shape=5)
|
|
|
|
|
func(a1)
|
|
|
|
|
for i in range(5):
|
|
|
|
|
for j in range(2):
|
|
|
|
|
for k in range(2):
|
|
|
|
|
assert a1[i][j, k] == j * j
|
|
|
|
|
|
|
|
|
|
a2 = ti.Matrix.ndarray(2, 2, ti.i32, shape=(3, 3))
|
|
|
|
|
func(a2)
|
|
|
|
|
for i in range(3):
|
|
|
|
|
for j in range(3):
|
|
|
|
|
for k in range(2):
|
|
|
|
|
for p in range(2):
|
|
|
|
|
assert a2[i, j][k, p] == k * k
|
2022-07-19 15:36:43 +08:00
|
|
|
|
|
|
|
|
|
2022-09-07 07:34:49 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_grouped():
|
|
|
|
|
_test_ndarray_grouped()
|
|
|
|
|
|
|
|
|
|
|
2022-12-13 10:12:48 +08:00
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda], real_matrix_scalarize=False)
|
2022-09-07 07:34:49 +08:00
|
|
|
def test_ndarray_grouped_real_matrix():
|
|
|
|
|
_test_ndarray_grouped()
|
|
|
|
|
|
|
|
|
|
|
2022-07-19 15:36:43 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_as_template():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def func(arr_src: ti.template(), arr_dst: ti.template()):
|
|
|
|
|
for i, j in ti.ndrange(*arr_src.shape):
|
|
|
|
|
arr_dst[i, j] = arr_src[i, j]
|
|
|
|
|
|
|
|
|
|
arr_0 = ti.ndarray(ti.f32, shape=(5, 10))
|
|
|
|
|
arr_1 = ti.ndarray(ti.f32, shape=(5, 10))
|
|
|
|
|
with pytest.raises(ti.TaichiRuntimeTypeError, match=r"Ndarray shouldn't be passed in via"):
|
|
|
|
|
func(arr_0, arr_1)
|
2022-10-20 19:24:12 +08:00
|
|
|
|
|
|
|
|
|
2023-07-13 16:12:20 +08:00
|
|
|
@pytest.mark.parametrize("shape", [2**31, 1.5, 0, (1, 0), (1, 0.5), (1, 2**31)])
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_shape_invalid(shape):
|
|
|
|
|
with pytest.raises(TaichiRuntimeError, match=r"is not a valid shape for ndarray"):
|
|
|
|
|
x = ti.ndarray(dtype=int, shape=shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("shape", [1, np.int32(1), (1, np.int32(1), 4096)])
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_shape_valid(shape):
|
|
|
|
|
x = ti.ndarray(dtype=int, shape=shape)
|
|
|
|
|
|
|
|
|
|
|
2022-10-20 19:24:12 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_gaussian_kernel():
|
|
|
|
|
M_PI = 3.14159265358979323846
|
|
|
|
|
|
|
|
|
|
@ti.func
|
|
|
|
|
def gaussian(x, sigma):
|
|
|
|
|
return ti.exp(-0.5 * ti.pow(x / sigma, 2)) / (sigma * ti.sqrt(2.0 * M_PI))
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
2022-11-22 09:48:35 +08:00
|
|
|
def fill_gaussian_kernel(ker: ti.types.ndarray(ti.f32, ndim=1), N: ti.i32):
|
2022-10-20 19:24:12 +08:00
|
|
|
sum = 0.0
|
|
|
|
|
for i in range(2 * N + 1):
|
|
|
|
|
ker[i] = gaussian(i - N, ti.sqrt(N))
|
|
|
|
|
sum += ker[i]
|
|
|
|
|
for i in range(2 * N + 1):
|
|
|
|
|
ker[i] = ker[i] / sum
|
|
|
|
|
|
|
|
|
|
N = 4
|
|
|
|
|
arr = ti.ndarray(dtype=ti.f32, shape=(20))
|
|
|
|
|
fill_gaussian_kernel(arr, N)
|
|
|
|
|
res = arr.to_numpy()
|
|
|
|
|
|
|
|
|
|
np_arr = np.zeros(20, dtype=np.float32)
|
|
|
|
|
fill_gaussian_kernel(np_arr, N)
|
|
|
|
|
|
|
|
|
|
assert test_utils.allclose(res, np_arr)
|
2022-11-29 09:46:51 +08:00
|
|
|
|
|
|
|
|
|
2022-12-13 10:12:48 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_numpy_matrix():
|
2022-11-29 09:46:51 +08:00
|
|
|
boundary_box_np = np.array([[0, 0, 0], [1, 1, 1]], dtype=np.float32)
|
|
|
|
|
boundary_box = ti.Vector.ndarray(3, ti.f32, shape=2)
|
|
|
|
|
boundary_box.from_numpy(boundary_box_np)
|
|
|
|
|
ref_numpy = boundary_box.to_numpy()
|
|
|
|
|
|
|
|
|
|
assert (boundary_box_np == ref_numpy).all()
|
2022-12-09 10:13:55 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("dtype", [ti.i64, ti.u64, ti.f64])
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray, require=ti.extension.data64)
|
|
|
|
|
def test_ndarray_python_scope_read_64bit(dtype):
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def run(x: ti.types.ndarray()):
|
|
|
|
|
for i in x:
|
|
|
|
|
x[i] = i + ti.i64(2**40)
|
|
|
|
|
|
|
|
|
|
n = 4
|
|
|
|
|
a = ti.ndarray(dtype, shape=(n,))
|
|
|
|
|
run(a)
|
|
|
|
|
for i in range(n):
|
|
|
|
|
assert a[i] == i + 2**40
|
2023-01-06 10:09:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_init_as_zero():
|
|
|
|
|
a = ti.ndarray(dtype=ti.f32, shape=(6, 10))
|
|
|
|
|
v = np.zeros((6, 10), dtype=np.float32)
|
|
|
|
|
assert test_utils.allclose(a.to_numpy(), v)
|
2023-01-11 19:26:09 +08:00
|
|
|
|
2023-05-25 14:52:44 +08:00
|
|
|
b = ti.ndarray(dtype=ti.math.vec2, shape=(6, 4))
|
|
|
|
|
k = np.zeros((6, 4, 2), dtype=np.float32)
|
|
|
|
|
assert test_utils.allclose(b.to_numpy(), k)
|
|
|
|
|
|
|
|
|
|
c = ti.ndarray(dtype=ti.math.mat2, shape=(6, 4))
|
|
|
|
|
m = np.zeros((6, 4, 2, 2), dtype=np.float32)
|
|
|
|
|
assert test_utils.allclose(c.to_numpy(), m)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_zero_fill():
|
|
|
|
|
dt = ti.types.vector(n=2, dtype=ti.f32)
|
|
|
|
|
arr = ti.ndarray(dtype=dt, shape=(3, 4))
|
|
|
|
|
|
|
|
|
|
arr.fill(1.0)
|
|
|
|
|
|
|
|
|
|
arr.to_numpy()
|
|
|
|
|
no = ti.ndarray(dtype=dt, shape=(3, 5))
|
|
|
|
|
assert no[0, 0][0] == 0.0
|
|
|
|
|
|
2023-01-11 19:26:09 +08:00
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_reset():
|
|
|
|
|
n = 8
|
|
|
|
|
c = ti.Matrix.ndarray(4, 4, ti.f32, shape=(n))
|
|
|
|
|
del c
|
|
|
|
|
d = ti.Matrix.ndarray(4, 4, ti.f32, shape=(n))
|
|
|
|
|
ti.reset()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.run_in_serial
|
2023-01-12 11:05:38 +08:00
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
2023-01-11 19:26:09 +08:00
|
|
|
def test_ndarray_in_python_func():
|
|
|
|
|
def test():
|
|
|
|
|
z = ti.ndarray(float, (8192, 8192))
|
|
|
|
|
|
|
|
|
|
for i in range(300):
|
|
|
|
|
test()
|
2023-03-01 18:01:35 +08:00
|
|
|
|
|
|
|
|
|
2023-03-02 12:29:28 +08:00
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda], exclude=[ti.amdgpu])
|
|
|
|
|
def test_ndarray_with_fp16():
|
|
|
|
|
half2 = ti.types.vector(n=2, dtype=ti.f16)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
2023-03-09 17:42:18 +08:00
|
|
|
def init(x: ti.types.ndarray(dtype=half2, ndim=1)):
|
2023-03-02 12:29:28 +08:00
|
|
|
for i in x:
|
|
|
|
|
x[i] = half2(2.0)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test(table: ti.types.ndarray(dtype=half2, ndim=1)):
|
|
|
|
|
tmp = ti.Vector([ti.f16(0.0), ti.f16(0.0)])
|
|
|
|
|
for i in ti.static(range(2)):
|
|
|
|
|
tmp = tmp + 4.0 * table[i]
|
|
|
|
|
|
|
|
|
|
table[0] = tmp
|
|
|
|
|
|
|
|
|
|
acc = ti.ndarray(dtype=half2, shape=(40))
|
|
|
|
|
table = ti.ndarray(dtype=half2, shape=(40))
|
|
|
|
|
|
|
|
|
|
init(table)
|
|
|
|
|
test(table)
|
|
|
|
|
|
|
|
|
|
assert (table.to_numpy()[0] == 16.0).all()
|
|
|
|
|
|
|
|
|
|
|
2023-03-01 18:01:35 +08:00
|
|
|
@test_utils.test(
|
|
|
|
|
arch=supported_archs_taichi_ndarray,
|
|
|
|
|
require=ti.extension.assertion,
|
|
|
|
|
debug=True,
|
|
|
|
|
check_out_of_bound=True,
|
|
|
|
|
gdb_trigger=False,
|
|
|
|
|
)
|
|
|
|
|
def test_scalar_ndarray_oob():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def access_arr(input: ti.types.ndarray(), x: ti.i32) -> ti.f32:
|
|
|
|
|
return input[x]
|
|
|
|
|
|
|
|
|
|
input = np.random.randn(4)
|
|
|
|
|
|
|
|
|
|
# Works
|
|
|
|
|
access_arr(input, 1)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError, match=r"Out of bound access"):
|
|
|
|
|
access_arr(input, 4)
|
|
|
|
|
|
|
|
|
|
with pytest.raises(AssertionError, match=r"Out of bound access"):
|
|
|
|
|
access_arr(input, -1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# SOA layout for ndarray is deprecated so no need to test
|
|
|
|
|
@test_utils.test(
|
|
|
|
|
arch=supported_archs_taichi_ndarray,
|
|
|
|
|
require=ti.extension.assertion,
|
|
|
|
|
debug=True,
|
|
|
|
|
check_out_of_bound=True,
|
|
|
|
|
gdb_trigger=False,
|
|
|
|
|
)
|
|
|
|
|
def test_matrix_ndarray_oob():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def access_arr(input: ti.types.ndarray(), p: ti.i32, q: ti.i32, x: ti.i32, y: ti.i32) -> ti.f32:
|
|
|
|
|
return input[p, q][x, y]
|
|
|
|
|
|
2023-05-13 18:14:12 +08:00
|
|
|
@ti.kernel
|
|
|
|
|
def valid_access(indices: ti.types.ndarray(dtype=ivec3, ndim=1), dummy: ti.types.ndarray(dtype=ivec3, ndim=1)):
|
|
|
|
|
for i in indices:
|
|
|
|
|
index_vec = ti.Vector([0, 0, 0])
|
|
|
|
|
for j in ti.static(range(3)):
|
|
|
|
|
index = indices[i][j]
|
|
|
|
|
index_vec[j] = index
|
|
|
|
|
dummy[i] = index_vec
|
|
|
|
|
|
2023-03-01 18:01:35 +08:00
|
|
|
input = ti.ndarray(dtype=ti.math.mat2, shape=(4, 5))
|
|
|
|
|
|
2023-05-13 18:14:12 +08:00
|
|
|
indices = ti.ndarray(dtype=ivec3, shape=(10))
|
|
|
|
|
dummy = ti.ndarray(dtype=ivec3, shape=(10))
|
|
|
|
|
|
2023-03-01 18:01:35 +08:00
|
|
|
# Works
|
|
|
|
|
access_arr(input, 2, 3, 0, 1)
|
2023-05-13 18:14:12 +08:00
|
|
|
valid_access(indices, dummy)
|
2023-03-01 18:01:35 +08:00
|
|
|
|
|
|
|
|
# element_shape
|
|
|
|
|
with pytest.raises(AssertionError, match=r"Out of bound access"):
|
|
|
|
|
access_arr(input, 2, 3, 2, 1)
|
|
|
|
|
# field_shape[0]
|
|
|
|
|
with pytest.raises(AssertionError, match=r"Out of bound access"):
|
|
|
|
|
access_arr(input, 4, 4, 0, 1)
|
|
|
|
|
with pytest.raises(AssertionError, match=r"Out of bound access"):
|
|
|
|
|
access_arr(input, -3, 4, 1, 1)
|
|
|
|
|
# field_shape[1]
|
|
|
|
|
with pytest.raises(AssertionError, match=r"Out of bound access"):
|
|
|
|
|
access_arr(input, 3, 5, 0, 1)
|
|
|
|
|
with pytest.raises(AssertionError, match=r"Out of bound access"):
|
|
|
|
|
access_arr(input, 2, -10, 1, 1)
|
2023-04-06 07:57:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_mismatched_index_python_scope():
|
|
|
|
|
x = ti.ndarray(dtype=ti.f32, shape=(4, 4))
|
|
|
|
|
with pytest.raises(TaichiIndexError, match=r"2d ndarray indexed with 1d indices"):
|
|
|
|
|
x[0]
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TaichiIndexError, match=r"2d ndarray indexed with 3d indices"):
|
|
|
|
|
x[0, 0, 0]
|
2023-04-24 12:56:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_0dim_ndarray_read_write_python_scope():
|
|
|
|
|
x = ti.ndarray(dtype=ti.f32, shape=())
|
|
|
|
|
|
|
|
|
|
x[()] = 1.0
|
|
|
|
|
assert x[None] == 1.0
|
|
|
|
|
|
|
|
|
|
y = ti.ndarray(dtype=ti.math.vec2, shape=())
|
|
|
|
|
y[()] = [1.0, 2.0]
|
|
|
|
|
assert y[None] == [1.0, 2.0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_0dim_ndarray_read_write_taichi_scope():
|
|
|
|
|
x = ti.ndarray(dtype=ti.f32, shape=())
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def write(x: ti.types.ndarray()):
|
|
|
|
|
a = x[()] + 1
|
|
|
|
|
x[None] = 2 * a
|
|
|
|
|
|
|
|
|
|
write(x)
|
|
|
|
|
assert x[None] == 2.0
|
|
|
|
|
|
|
|
|
|
y = ti.ndarray(dtype=ti.math.vec2, shape=())
|
|
|
|
|
write(y)
|
|
|
|
|
assert y[None] == [2.0, 2.0]
|
2023-04-24 17:23:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray, require=ti.extension.data64)
|
|
|
|
|
def test_read_write_f64_python_scope():
|
|
|
|
|
x = ti.ndarray(dtype=ti.f64, shape=2)
|
|
|
|
|
|
|
|
|
|
x[0] = 1.0
|
|
|
|
|
assert x[0] == 1.0
|
|
|
|
|
|
|
|
|
|
y = ti.ndarray(dtype=ti.math.vec2, shape=2)
|
|
|
|
|
y[0] = [1.0, 2.0]
|
|
|
|
|
assert y[0] == [1.0, 2.0]
|
2023-04-28 09:47:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_fill():
|
|
|
|
|
vec2 = ti.types.vector(2, ti.f32)
|
|
|
|
|
x_vec = ti.ndarray(vec2, (512, 512))
|
|
|
|
|
x_vec.fill(1.0)
|
|
|
|
|
assert (x_vec[2, 2] == [1.0, 1.0]).all()
|
|
|
|
|
|
|
|
|
|
x_vec.fill(vec2(2.0, 4.0))
|
|
|
|
|
assert (x_vec[3, 3] == [2.0, 4.0]).all()
|
|
|
|
|
|
|
|
|
|
mat2x2 = ti.types.matrix(2, 2, ti.f32)
|
|
|
|
|
x_mat = ti.ndarray(mat2x2, (512, 512))
|
|
|
|
|
x_mat.fill(2.0)
|
|
|
|
|
assert (x_mat[2, 2] == [[2.0, 2.0], [2.0, 2.0]]).all()
|
|
|
|
|
|
|
|
|
|
x_mat.fill(mat2x2([[2.0, 4.0], [1.0, 3.0]]))
|
|
|
|
|
assert (x_mat[3, 3] == [[2.0, 4.0], [1.0, 3.0]]).all()
|
2023-05-10 12:13:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_wrong_dtype():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test2(arr: ti.types.ndarray(dtype=ti.f32)):
|
|
|
|
|
for I in ti.grouped(arr):
|
|
|
|
|
arr[I] = 2.0
|
|
|
|
|
|
|
|
|
|
tp_ivec3 = ti.types.vector(3, ti.i32)
|
|
|
|
|
|
|
|
|
|
y = ti.ndarray(tp_ivec3, shape=(12, 4))
|
2023-05-15 16:09:43 +08:00
|
|
|
with pytest.raises(TypeError, match=r"get \[Tensor \(3\) i32\]"):
|
2023-05-10 12:13:43 +08:00
|
|
|
test2(y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_bad_assign():
|
|
|
|
|
tp_ivec3 = ti.types.vector(3, ti.i32)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test4(arr: ti.types.ndarray(dtype=tp_ivec3)):
|
|
|
|
|
for I in ti.grouped(arr):
|
|
|
|
|
arr[I] = [1, 2]
|
|
|
|
|
|
|
|
|
|
y = ti.ndarray(tp_ivec3, shape=(12, 4))
|
|
|
|
|
with pytest.raises(TaichiTypeError, match=r"cannot assign '\[Tensor \(2\) i32\]' to '\[Tensor \(3\) i32\]'"):
|
|
|
|
|
test4(y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_bad_ndim():
|
|
|
|
|
x = ti.ndarray(ti.f32, shape=(12, 13))
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test5(arr: ti.types.ndarray(ndim=1)):
|
|
|
|
|
for i, j in arr:
|
|
|
|
|
arr[i, j] = 0
|
|
|
|
|
|
|
|
|
|
with pytest.raises(ValueError, match=r"required ndim=1, but 2d ndarray with shape \(12, 13\) is provided"):
|
|
|
|
|
test5(x)
|
2023-05-15 18:11:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_type_hint_matrix():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test(x: ti.types.ndarray(dtype=ti.types.matrix())):
|
|
|
|
|
for I in ti.grouped(x):
|
|
|
|
|
x[I] = 1.0
|
|
|
|
|
|
|
|
|
|
x = ti.ndarray(ti.math.mat2, (3))
|
|
|
|
|
test(x)
|
|
|
|
|
assert impl.get_runtime().get_num_compiled_functions() == 1
|
|
|
|
|
|
|
|
|
|
y = ti.ndarray(ti.math.mat3, (3))
|
|
|
|
|
test(y)
|
|
|
|
|
assert impl.get_runtime().get_num_compiled_functions() == 2
|
|
|
|
|
|
|
|
|
|
z = ti.ndarray(ti.math.vec2, (3))
|
2023-06-14 14:01:43 +08:00
|
|
|
with pytest.raises(ValueError, match=r"Invalid value for argument x"):
|
2023-05-15 18:11:24 +08:00
|
|
|
test(z)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_type_hint_vector():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test(x: ti.types.ndarray(dtype=ti.types.vector())):
|
|
|
|
|
for I in ti.grouped(x):
|
|
|
|
|
x[I] = 1.0
|
|
|
|
|
|
|
|
|
|
x = ti.ndarray(ti.math.vec3, (3))
|
|
|
|
|
test(x)
|
|
|
|
|
assert impl.get_runtime().get_num_compiled_functions() == 1
|
|
|
|
|
|
|
|
|
|
y = ti.ndarray(ti.math.vec2, (3))
|
|
|
|
|
test(y)
|
|
|
|
|
assert impl.get_runtime().get_num_compiled_functions() == 2
|
|
|
|
|
|
|
|
|
|
z = ti.ndarray(ti.math.mat2, (3))
|
2023-06-14 14:01:43 +08:00
|
|
|
with pytest.raises(ValueError, match=r"Invalid value for argument x"):
|
2023-05-15 18:11:24 +08:00
|
|
|
test(z)
|
2023-06-06 12:15:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_pass_ndarray_to_func():
|
|
|
|
|
@ti.func
|
2023-06-16 17:19:05 +08:00
|
|
|
def bar(weight: ti.types.ndarray(ti.f32, ndim=3)) -> ti.f32:
|
|
|
|
|
return weight[1, 1, 1]
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(weight: ti.types.ndarray(ti.f32, ndim=3)) -> ti.f32:
|
|
|
|
|
return bar(weight)
|
|
|
|
|
|
|
|
|
|
weight = ti.ndarray(dtype=ti.f32, shape=(2, 2, 2))
|
|
|
|
|
weight.fill(42.0)
|
|
|
|
|
assert foo(weight) == 42.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda])
|
|
|
|
|
def test_pass_ndarray_to_real_func():
|
[Lang] Move real function out of the experimental module (#8399)
Issue: #
### Brief Summary
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
This pull request updates the `real_func` examples and tests to use the
stable `ti.real_func` decorator and the new vector, matrix, and field
APIs. It also refactors some code for readability, performance, and
consistency, and removes the deprecated `experimental` module.
### Walkthrough
<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
* Remove `experimental` module and move `real_func` decorator to
top-level `taichi` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e0e39c57bc92c0e1da4c831f8820a4082a3cc26a9b88962bee72964dfa26a74aL13-R13),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-a157043b38542c8145447ff342fda65fe4d54fb777fe514daa70007e83e20dc1L1235-R1235))
* Update all examples and tests that use `real_func` decorator to import
it from `taichi` module instead of `experimental` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL58-R58),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL72-R82),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL136-R136),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL33-R33),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL55-R55),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL93-R98),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL43-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL89-R94),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL108-R108),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL61-R61),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL68-R68),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL78-R78),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL89-R89),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL116-R116),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL124-R124),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL66-R71),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL80-R80),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL86-R86),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL95-R95),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL105-R105),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL119-R119),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL125-R125),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL132-R132),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL344-R344),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f2d9c4a8c504d2073549cde99b31a733a2f39bba4ce9f062cbaa09e1be17aa87L243-R243),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L11-R11),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L29-R29),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L49-R49),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L153-R153),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L185-R185),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L194-R194),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L207-R207),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L221-R221),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L242-R242),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L259-R259),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L281-R281),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L295-R295),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L310-R310),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L329-R329),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L352-R352),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L451-R451),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L469-R469),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L484-R484),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L501-R501),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L515-R515),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L533-R533),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1035-R1035),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1052-R1052),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1134-R1134),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1149-R1149),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L480-R480),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L494-R494),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-8641463ffef94529453efd945e02d056e4339417cc65aad074d4e6b3ef093244L49-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L218-R218),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L238-R238),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L258-R258))
* Delete `python/taichi/experimental.py` file as it is no longer needed
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-60267f25c1e1dfada9487235a4a793b63a49aa3d33c93b8449e24ac821a33174))
2023-10-31 14:11:53 +08:00
|
|
|
@ti.real_func
|
2023-06-16 17:19:05 +08:00
|
|
|
def bar(weight: ti.types.ndarray(ti.f32, ndim=3)) -> ti.f32:
|
|
|
|
|
return weight[1, 1, 1]
|
2023-06-06 12:15:58 +08:00
|
|
|
|
|
|
|
|
@ti.kernel
|
2023-06-16 17:19:05 +08:00
|
|
|
def foo(weight: ti.types.ndarray(ti.f32, ndim=3)) -> ti.f32:
|
|
|
|
|
return bar(weight)
|
2023-06-06 12:15:58 +08:00
|
|
|
|
|
|
|
|
weight = ti.ndarray(dtype=ti.f32, shape=(2, 2, 2))
|
2023-06-16 17:19:05 +08:00
|
|
|
weight.fill(42.0)
|
|
|
|
|
assert foo(weight) == 42.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda])
|
|
|
|
|
def test_pass_ndarray_outside_kernel_to_real_func():
|
|
|
|
|
weight = ti.ndarray(dtype=ti.f32, shape=(2, 2, 2))
|
|
|
|
|
|
[Lang] Move real function out of the experimental module (#8399)
Issue: #
### Brief Summary
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
This pull request updates the `real_func` examples and tests to use the
stable `ti.real_func` decorator and the new vector, matrix, and field
APIs. It also refactors some code for readability, performance, and
consistency, and removes the deprecated `experimental` module.
### Walkthrough
<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
* Remove `experimental` module and move `real_func` decorator to
top-level `taichi` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e0e39c57bc92c0e1da4c831f8820a4082a3cc26a9b88962bee72964dfa26a74aL13-R13),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-a157043b38542c8145447ff342fda65fe4d54fb777fe514daa70007e83e20dc1L1235-R1235))
* Update all examples and tests that use `real_func` decorator to import
it from `taichi` module instead of `experimental` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL58-R58),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL72-R82),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL136-R136),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL33-R33),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL55-R55),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL93-R98),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL43-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL89-R94),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL108-R108),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL61-R61),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL68-R68),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL78-R78),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL89-R89),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL116-R116),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL124-R124),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL66-R71),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL80-R80),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL86-R86),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL95-R95),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL105-R105),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL119-R119),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL125-R125),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL132-R132),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL344-R344),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f2d9c4a8c504d2073549cde99b31a733a2f39bba4ce9f062cbaa09e1be17aa87L243-R243),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L11-R11),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L29-R29),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L49-R49),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L153-R153),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L185-R185),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L194-R194),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L207-R207),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L221-R221),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L242-R242),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L259-R259),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L281-R281),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L295-R295),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L310-R310),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L329-R329),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L352-R352),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L451-R451),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L469-R469),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L484-R484),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L501-R501),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L515-R515),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L533-R533),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1035-R1035),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1052-R1052),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1134-R1134),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1149-R1149),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L480-R480),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L494-R494),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-8641463ffef94529453efd945e02d056e4339417cc65aad074d4e6b3ef093244L49-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L218-R218),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L238-R238),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L258-R258))
* Delete `python/taichi/experimental.py` file as it is no longer needed
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-60267f25c1e1dfada9487235a4a793b63a49aa3d33c93b8449e24ac821a33174))
2023-10-31 14:11:53 +08:00
|
|
|
@ti.real_func
|
2023-06-16 17:19:05 +08:00
|
|
|
def bar(weight: ti.types.ndarray(ti.f32, ndim=3)) -> ti.f32:
|
|
|
|
|
return weight[1, 1, 1]
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo() -> ti.f32:
|
|
|
|
|
return bar(weight)
|
|
|
|
|
|
|
|
|
|
weight.fill(42.0)
|
|
|
|
|
with pytest.raises(ti.TaichiTypeError, match=r"Expected ndarray in the kernel argument for argument weight"):
|
|
|
|
|
foo()
|
2023-06-07 13:29:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_oob_clamp():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test(x: ti.types.ndarray(boundary="clamp"), y: ti.i32) -> ti.f32:
|
|
|
|
|
return x[y]
|
|
|
|
|
|
|
|
|
|
x = ti.ndarray(ti.f32, shape=(3))
|
|
|
|
|
for i in range(3):
|
|
|
|
|
x[i] = i
|
|
|
|
|
|
|
|
|
|
assert test(x, -1) == 0
|
|
|
|
|
assert test(x, -2) == 0
|
|
|
|
|
assert test(x, 3) == 2
|
|
|
|
|
assert test(x, 4) == 2
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test_vec_arr(x: ti.types.ndarray(boundary="clamp"), y: ti.i32) -> ti.f32:
|
|
|
|
|
return x[1, 2][y]
|
|
|
|
|
|
|
|
|
|
x2 = ti.ndarray(ti.math.vec2, shape=(3, 3))
|
|
|
|
|
for i in range(3):
|
|
|
|
|
for j in range(3):
|
|
|
|
|
x2[i, j] = [i, j]
|
|
|
|
|
assert test_vec_arr(x2, -1) == 1
|
|
|
|
|
assert test_vec_arr(x2, 2) == 2
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test_mat_arr(x: ti.types.ndarray(boundary="clamp"), i: ti.i32, j: ti.i32) -> ti.f32:
|
|
|
|
|
return x[1, 2][i, j]
|
|
|
|
|
|
|
|
|
|
x3 = ti.ndarray(ti.math.mat2, shape=(3, 3))
|
|
|
|
|
for i in range(3):
|
|
|
|
|
for j in range(3):
|
|
|
|
|
x3[i, j] = [[i, j], [i + 1, j + 1]]
|
|
|
|
|
assert test_mat_arr(x3, -1, 0) == 1
|
|
|
|
|
assert test_mat_arr(x3, 1, -1) == 2
|
|
|
|
|
assert test_mat_arr(x3, 2, 0) == 3
|
|
|
|
|
assert test_mat_arr(x3, 1, 2) == 3
|
2023-06-14 16:45:14 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_clamp_verify():
|
|
|
|
|
height = 3
|
|
|
|
|
width = 3
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test(ao: ti.types.ndarray(dtype=ti.f32, ndim=2, boundary="clamp")):
|
|
|
|
|
for y, x in ti.ndrange(height, width):
|
|
|
|
|
vis = 0.0
|
|
|
|
|
ao[y, x] = vis
|
|
|
|
|
|
|
|
|
|
ao = ti.ndarray(ti.f32, shape=(height, width))
|
|
|
|
|
test(ao)
|
|
|
|
|
assert (ao.to_numpy() == np.zeros((height, width))).all()
|
2023-06-28 16:18:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=supported_archs_taichi_ndarray)
|
|
|
|
|
def test_ndarray_arg_builtin_float_type():
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(x: ti.types.ndarray(float, ndim=0)) -> ti.f32:
|
|
|
|
|
return x[None]
|
|
|
|
|
|
|
|
|
|
x = ti.ndarray(ti.f32, shape=())
|
|
|
|
|
x[None] = 42
|
|
|
|
|
assert foo(x) == 42
|
2023-06-28 16:18:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda])
|
|
|
|
|
def test_real_func_vector_ndarray_arg():
|
[Lang] Move real function out of the experimental module (#8399)
Issue: #
### Brief Summary
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
This pull request updates the `real_func` examples and tests to use the
stable `ti.real_func` decorator and the new vector, matrix, and field
APIs. It also refactors some code for readability, performance, and
consistency, and removes the deprecated `experimental` module.
### Walkthrough
<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
* Remove `experimental` module and move `real_func` decorator to
top-level `taichi` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e0e39c57bc92c0e1da4c831f8820a4082a3cc26a9b88962bee72964dfa26a74aL13-R13),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-a157043b38542c8145447ff342fda65fe4d54fb777fe514daa70007e83e20dc1L1235-R1235))
* Update all examples and tests that use `real_func` decorator to import
it from `taichi` module instead of `experimental` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL58-R58),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL72-R82),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL136-R136),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL33-R33),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL55-R55),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL93-R98),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL43-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL89-R94),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL108-R108),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL61-R61),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL68-R68),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL78-R78),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL89-R89),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL116-R116),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL124-R124),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL66-R71),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL80-R80),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL86-R86),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL95-R95),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL105-R105),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL119-R119),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL125-R125),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL132-R132),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL344-R344),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f2d9c4a8c504d2073549cde99b31a733a2f39bba4ce9f062cbaa09e1be17aa87L243-R243),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L11-R11),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L29-R29),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L49-R49),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L153-R153),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L185-R185),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L194-R194),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L207-R207),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L221-R221),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L242-R242),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L259-R259),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L281-R281),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L295-R295),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L310-R310),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L329-R329),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L352-R352),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L451-R451),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L469-R469),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L484-R484),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L501-R501),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L515-R515),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L533-R533),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1035-R1035),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1052-R1052),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1134-R1134),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1149-R1149),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L480-R480),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L494-R494),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-8641463ffef94529453efd945e02d056e4339417cc65aad074d4e6b3ef093244L49-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L218-R218),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L238-R238),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L258-R258))
* Delete `python/taichi/experimental.py` file as it is no longer needed
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-60267f25c1e1dfada9487235a4a793b63a49aa3d33c93b8449e24ac821a33174))
2023-10-31 14:11:53 +08:00
|
|
|
@ti.real_func
|
2023-06-28 16:18:22 +08:00
|
|
|
def foo(x: ti.types.ndarray(ndim=1)) -> vec3:
|
|
|
|
|
return x[0]
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def test(x: ti.types.ndarray(ndim=1)) -> vec3:
|
|
|
|
|
return foo(x)
|
|
|
|
|
|
|
|
|
|
x = ti.Vector.ndarray(3, ti.f32, shape=(1))
|
|
|
|
|
x[0] = vec3(1, 2, 3)
|
|
|
|
|
assert (test(x) == vec3(1, 2, 3)).all()
|
2023-06-30 15:03:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@test_utils.test(arch=[ti.cpu, ti.cuda])
|
|
|
|
|
def test_real_func_write_ndarray_cfg():
|
[Lang] Move real function out of the experimental module (#8399)
Issue: #
### Brief Summary
<!--
copilot:summary
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
This pull request updates the `real_func` examples and tests to use the
stable `ti.real_func` decorator and the new vector, matrix, and field
APIs. It also refactors some code for readability, performance, and
consistency, and removes the deprecated `experimental` module.
### Walkthrough
<!--
copilot:walkthrough
-->
### <samp>🤖 Generated by Copilot at 8c46960</samp>
* Remove `experimental` module and move `real_func` decorator to
top-level `taichi` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e0e39c57bc92c0e1da4c831f8820a4082a3cc26a9b88962bee72964dfa26a74aL13-R13),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-a157043b38542c8145447ff342fda65fe4d54fb777fe514daa70007e83e20dc1L1235-R1235))
* Update all examples and tests that use `real_func` decorator to import
it from `taichi` module instead of `experimental` module
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL58-R58),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL72-R82),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-9cbae292e39bc9b37e4cfab4bfef6f363788d06ecb7757753ee5c0ba7ad0afcdL136-R136),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL33-R33),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL55-R55),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f6098eab3d43899f2146cafc8454b2d9bae86064709eb2eeb43e0edbf459f22fL93-R98),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL43-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL89-R94),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-86667a5be06e5995f4b15199aa3d4b655e64cceed6d108185d5cc71e03f0bb1fL108-R108),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL51-R51),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL61-R61),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL68-R68),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL78-R78),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL89-R89),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL116-R116),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f9f9a26653afb587b73d90d08c099a8236820fc72b4d247e001954d620073ebcL124-R124),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL66-R71),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL80-R80),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL86-R86),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL95-R95),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL105-R105),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL119-R119),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL125-R125),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL132-R132),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-52ca6a00561cb88b076a00d2ae2a30dca3d46713e52f0f5ca6b52ecfd95545daL344-R344),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-f2d9c4a8c504d2073549cde99b31a733a2f39bba4ce9f062cbaa09e1be17aa87L243-R243),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L11-R11),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L29-R29),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L49-R49),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L153-R153),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L185-R185),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L194-R194),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L207-R207),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L221-R221),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L242-R242),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L259-R259),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L281-R281),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L295-R295),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L310-R310),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L329-R329),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L352-R352),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L451-R451),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L469-R469),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L484-R484),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L501-R501),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L515-R515),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-d6f5c74e17462c8ff96d5bba06ebf81d16c015ca667fa945513a80be17bef017L533-R533),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1035-R1035),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1052-R1052),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1134-R1134),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-ca3c8d1edb25b6a7f4affbb79b2e3e74f73b3757e5d465258ce42ea9eb09fbc0L1149-R1149),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L480-R480),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-85fb062b09589d588166180e3c7954753018dc39474d3846346f2a8bb3faac32L494-R494),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-8641463ffef94529453efd945e02d056e4339417cc65aad074d4e6b3ef093244L49-R53),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L218-R218),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L238-R238),
[link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-e206c19691b4678b401a5e2787000e93ab0b0060cbf4ac2625b51f87599d98d8L258-R258))
* Delete `python/taichi/experimental.py` file as it is no longer needed
([link](https://github.com/taichi-dev/taichi/pull/8399/files?diff=unified&w=0#diff-60267f25c1e1dfada9487235a4a793b63a49aa3d33c93b8449e24ac821a33174))
2023-10-31 14:11:53 +08:00
|
|
|
@ti.real_func
|
2023-06-30 15:03:01 +08:00
|
|
|
def bar(a: ti.types.ndarray(ndim=1)):
|
|
|
|
|
a[0] = vec3(1)
|
|
|
|
|
|
|
|
|
|
@ti.kernel
|
|
|
|
|
def foo(
|
|
|
|
|
a: ti.types.ndarray(ndim=1),
|
|
|
|
|
):
|
|
|
|
|
a[0] = vec3(3)
|
|
|
|
|
bar(a)
|
|
|
|
|
a[0] = vec3(3)
|
|
|
|
|
|
|
|
|
|
a = ti.Vector.ndarray(3, float, shape=(2,))
|
|
|
|
|
foo(a)
|
|
|
|
|
assert (a[0] == vec3(3)).all()
|