From 52f4a45ed3a43431866188a0ec7a6b4952c3b8a6 Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 17:37:37 +0800 Subject: [PATCH 01/11] mod api --- python/taichi/lang/matrix.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index 12a20388ca649..c19032ab5ff8f 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -821,8 +821,20 @@ def field(cls, self.n = n self.m = m self.dt = dtype - for i in range(n * m): - self.entries.append(impl.field(dtype)) + + if isinstance(dtype, (list, tuple, np.ndarray)): + if m == 1: + assert (len(np.shape(dtype)) == 1 and len(dtype) == n), f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' + for i in range(n): + self.entries.append(impl.field(dtype[i])) + else: + assert (len(np.shape(dtype)) == 2 and len(dtype) == n and len(dtype[0]) == m), f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' + for i in range(n): + for j in range(m): + self.entries.append(impl.field(dtype[i][j])) + else: + for _ in range(n * m): + self.entries.append(impl.field(dtype)) self.grad = self.make_grad() if layout is not None: From ad2e4d54775ecbb795fdf987acc21172ce4365cb Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 17:37:50 +0800 Subject: [PATCH 02/11] add basic test --- tests/python/test_matrix_different_type.py | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/python/test_matrix_different_type.py diff --git a/tests/python/test_matrix_different_type.py b/tests/python/test_matrix_different_type.py new file mode 100644 index 0000000000000..0041c19c7e276 --- /dev/null +++ b/tests/python/test_matrix_different_type.py @@ -0,0 +1,54 @@ +import taichi as ti + + +@ti.test() +def test_vector(): + type_list = [ti.f32, ti.i32] + + a = ti.Vector.field(len(type_list), dtype=type_list, shape=()) + b = ti.Vector.field(len(type_list), dtype=type_list, shape=()) + c = ti.Vector.field(len(type_list), dtype=type_list, shape=()) + + @ti.kernel + def init(): + a[None] = [1.0, 3] + b[None] = [2.0, 4] + c[None] = a[None] + b[None] + + def verify(): + assert isinstance(a[None][0], float) + assert isinstance(a[None][1], int) + assert isinstance(b[None][0], float) + assert isinstance(b[None][1], int) + assert c[None][0] == 3.0 + assert c[None][1] == 7 + + init() + verify() + + +@ti.test() +def test_matrix(): + type_list = [[ti.f32, ti.i32], [ti.i64, ti.f32]] + a = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list, shape=()) + b = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list, shape=()) + c = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list, shape=()) + + @ti.kernel + def init(): + a[None] = [[1.0, 3],[1, 3.0]] + b[None] = [[2.0, 4],[-2, -3.0]] + c[None] = a[None] + b[None] + + def verify(): + assert isinstance(a[None][0], float) + assert isinstance(a[None][1], int) + assert isinstance(b[None][0], float) + assert isinstance(b[None][1], int) + assert c[None][0, 0] == 3.0 + assert c[None][0, 1] == 7 + assert c[None][1, 0] == -1 + assert c[None][1, 1] == 0.0 + + init() + verify() \ No newline at end of file From 0a277007deaf6be435f871191a799ee44e6ca117 Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 18:04:39 +0800 Subject: [PATCH 03/11] add custom type test case --- tests/python/test_matrix_different_type.py | 35 +++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/python/test_matrix_different_type.py b/tests/python/test_matrix_different_type.py index 0041c19c7e276..f3c75e9220d24 100644 --- a/tests/python/test_matrix_different_type.py +++ b/tests/python/test_matrix_different_type.py @@ -1,5 +1,5 @@ import taichi as ti - +from pytest import approx @ti.test() def test_vector(): @@ -50,5 +50,38 @@ def verify(): assert c[None][1, 0] == -1 assert c[None][1, 1] == 0.0 + init() + verify() + + +@ti.test(require=ti.extension.quant) +def test_custom_type(): + cit1 = ti.type_factory.custom_int(bits=10, signed=True) + cft1 = ti.type_factory.custom_float(cit1, scale=0.1) + cit2 = ti.type_factory.custom_int(bits=22, signed=False) + cft2 = ti.type_factory.custom_float(cit2, scale=0.1) + type_list = [[cit1, cft2], [cft1, cit2]] + a = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list) + b = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list) + c = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list) + ti.root.dense(ti.i, 1)._bit_struct(num_bits=32).place(a(0, 0), a(0, 1)) + ti.root.dense(ti.i, 1)._bit_struct(num_bits=32).place(a(1, 0), a(1, 1)) + ti.root.dense(ti.i, 1)._bit_struct(num_bits=32).place(b(0, 0), b(0, 1)) + ti.root.dense(ti.i, 1)._bit_struct(num_bits=32).place(b(1, 0), b(1, 1)) + ti.root.dense(ti.i, 1)._bit_struct(num_bits=32).place(c(0, 0), c(0, 1)) + ti.root.dense(ti.i, 1)._bit_struct(num_bits=32).place(c(1, 0), c(1, 1)) + + @ti.kernel + def init(): + a[0] = [[1, 3.], [2., 1]] + b[0] = [[2, 4.], [-2., 1]] + c[0] = a[0] + b[0] + + def verify(): + assert c[0][0, 0] == approx(3, 1e-3) + assert c[0][0, 1] == approx(7.0, 1e-3) + assert c[0][1, 0] == approx(0, 1e-3) + assert c[0][1, 1] == approx(2, 1e-3) + init() verify() \ No newline at end of file From ed667cef3ffef3fc6f3cf99b7e81e8cf81d6b534 Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 19:47:56 +0800 Subject: [PATCH 04/11] add some comments --- python/taichi/lang/matrix.py | 2 ++ tests/python/test_matrix_different_type.py | 1 + 2 files changed, 3 insertions(+) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index c19032ab5ff8f..c4aec8a4fa866 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -823,6 +823,8 @@ def field(cls, self.dt = dtype if isinstance(dtype, (list, tuple, np.ndarray)): + # set different dtype for each element in Matrix + # see #2135 if m == 1: assert (len(np.shape(dtype)) == 1 and len(dtype) == n), f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' for i in range(n): diff --git a/tests/python/test_matrix_different_type.py b/tests/python/test_matrix_different_type.py index f3c75e9220d24..c6ccdceb653fd 100644 --- a/tests/python/test_matrix_different_type.py +++ b/tests/python/test_matrix_different_type.py @@ -1,6 +1,7 @@ import taichi as ti from pytest import approx +# TODO: test more matrix operations @ti.test() def test_vector(): type_list = [ti.f32, ti.i32] From c3d68cd6a7338542af68e4c9a858ab626d3d7aa8 Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Fri, 1 Jan 2021 06:48:44 -0500 Subject: [PATCH 05/11] [skip ci] enforce code format --- python/taichi/lang/matrix.py | 9 ++++++-- tests/python/test_matrix_different_type.py | 26 +++++++++++++++------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index c4aec8a4fa866..580ff80164c70 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -826,11 +826,16 @@ def field(cls, # set different dtype for each element in Matrix # see #2135 if m == 1: - assert (len(np.shape(dtype)) == 1 and len(dtype) == n), f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' + assert ( + len(np.shape(dtype)) == 1 and len(dtype) == n + ), f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' for i in range(n): self.entries.append(impl.field(dtype[i])) else: - assert (len(np.shape(dtype)) == 2 and len(dtype) == n and len(dtype[0]) == m), f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' + assert ( + len(np.shape(dtype)) == 2 and len(dtype) == n + and len(dtype[0]) == m + ), f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' for i in range(n): for j in range(m): self.entries.append(impl.field(dtype[i][j])) diff --git a/tests/python/test_matrix_different_type.py b/tests/python/test_matrix_different_type.py index c6ccdceb653fd..1877133944787 100644 --- a/tests/python/test_matrix_different_type.py +++ b/tests/python/test_matrix_different_type.py @@ -1,6 +1,7 @@ import taichi as ti from pytest import approx + # TODO: test more matrix operations @ti.test() def test_vector(): @@ -31,14 +32,23 @@ def verify(): @ti.test() def test_matrix(): type_list = [[ti.f32, ti.i32], [ti.i64, ti.f32]] - a = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list, shape=()) - b = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list, shape=()) - c = ti.Matrix.field(len(type_list), len(type_list[0]), dtype=type_list, shape=()) - + a = ti.Matrix.field(len(type_list), + len(type_list[0]), + dtype=type_list, + shape=()) + b = ti.Matrix.field(len(type_list), + len(type_list[0]), + dtype=type_list, + shape=()) + c = ti.Matrix.field(len(type_list), + len(type_list[0]), + dtype=type_list, + shape=()) + @ti.kernel def init(): - a[None] = [[1.0, 3],[1, 3.0]] - b[None] = [[2.0, 4],[-2, -3.0]] + a[None] = [[1.0, 3], [1, 3.0]] + b[None] = [[2.0, 4], [-2, -3.0]] c[None] = a[None] + b[None] def verify(): @@ -77,7 +87,7 @@ def init(): a[0] = [[1, 3.], [2., 1]] b[0] = [[2, 4.], [-2., 1]] c[0] = a[0] + b[0] - + def verify(): assert c[0][0, 0] == approx(3, 1e-3) assert c[0][0, 1] == approx(7.0, 1e-3) @@ -85,4 +95,4 @@ def verify(): assert c[0][1, 1] == approx(2, 1e-3) init() - verify() \ No newline at end of file + verify() From eb984fbdb641e6906cbda573832b481dd162077e Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 19:49:46 +0800 Subject: [PATCH 06/11] trigger CI From afc0ec35166f7bf12ea5e1d60a4f48f8a2ab55c2 Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 19:52:41 +0800 Subject: [PATCH 07/11] clean --- python/taichi/lang/matrix.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index 580ff80164c70..50fff8a9a829a 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -826,16 +826,11 @@ def field(cls, # set different dtype for each element in Matrix # see #2135 if m == 1: - assert ( - len(np.shape(dtype)) == 1 and len(dtype) == n - ), f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' + assert len(np.shape(dtype)) == 1 and len(dtype) == n, f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' for i in range(n): self.entries.append(impl.field(dtype[i])) else: - assert ( - len(np.shape(dtype)) == 2 and len(dtype) == n - and len(dtype[0]) == m - ), f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' + assert len(np.shape(dtype)) == 2 and len(dtype) == n and len(dtype[0]) == m, f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' for i in range(n): for j in range(m): self.entries.append(impl.field(dtype[i][j])) From 8d8b628834768024c16d0676204294897dbbcec3 Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Fri, 1 Jan 2021 06:53:09 -0500 Subject: [PATCH 08/11] [skip ci] enforce code format --- python/taichi/lang/matrix.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index 50fff8a9a829a..d4592f0a0320a 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -826,11 +826,15 @@ def field(cls, # set different dtype for each element in Matrix # see #2135 if m == 1: - assert len(np.shape(dtype)) == 1 and len(dtype) == n, f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' + assert len(np.shape(dtype)) == 1 and len( + dtype + ) == n, f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' for i in range(n): self.entries.append(impl.field(dtype[i])) else: - assert len(np.shape(dtype)) == 2 and len(dtype) == n and len(dtype[0]) == m, f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' + assert len(np.shape(dtype)) == 2 and len(dtype) == n and len( + dtype[0] + ) == m, f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' for i in range(n): for j in range(m): self.entries.append(impl.field(dtype[i][j])) From 95eef221f958a4e7ca96c230a9a24d11d02eb965 Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 19:57:29 +0800 Subject: [PATCH 09/11] trigger CI From abfcbab3e331ba97f2d7bcf601580a22ee4592c3 Mon Sep 17 00:00:00 2001 From: hanke98 Date: Fri, 1 Jan 2021 23:48:03 +0800 Subject: [PATCH 10/11] exclude opengl for now --- tests/python/test_matrix_different_type.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/python/test_matrix_different_type.py b/tests/python/test_matrix_different_type.py index 1877133944787..3e4095f5c961c 100644 --- a/tests/python/test_matrix_different_type.py +++ b/tests/python/test_matrix_different_type.py @@ -29,7 +29,8 @@ def verify(): verify() -@ti.test() +# TODO: Support different element types of Matrix on opengl +@ti.test(exclude=ti.opengl) def test_matrix(): type_list = [[ti.f32, ti.i32], [ti.i64, ti.f32]] a = ti.Matrix.field(len(type_list), From 37b433c90c7572a0b035711be2f1ea8f07bb4233 Mon Sep 17 00:00:00 2001 From: Jiafeng Liu Date: Sat, 2 Jan 2021 09:40:15 +0800 Subject: [PATCH 11/11] Apply suggestions from code review Co-authored-by: Yuanming Hu --- python/taichi/lang/matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/taichi/lang/matrix.py b/python/taichi/lang/matrix.py index d4592f0a0320a..748a9dd2ad17e 100644 --- a/python/taichi/lang/matrix.py +++ b/python/taichi/lang/matrix.py @@ -828,13 +828,13 @@ def field(cls, if m == 1: assert len(np.shape(dtype)) == 1 and len( dtype - ) == n, f'Please set correct dtype list for Vector, the shape of dtype list should be ({n}, ) not {np.shape(dtype)}' + ) == n, f'Please set correct dtype list for Vector. The shape of dtype list should be ({n}, ) instead of {np.shape(dtype)}' for i in range(n): self.entries.append(impl.field(dtype[i])) else: assert len(np.shape(dtype)) == 2 and len(dtype) == n and len( dtype[0] - ) == m, f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) not {np.shape(dtype)}' + ) == m, f'Please set correct dtype list for Matrix. The shape of dtype list should be ({n}, {m}) instead of {np.shape(dtype)}' for i in range(n): for j in range(m): self.entries.append(impl.field(dtype[i][j]))