From dcc0528127f166ce10b06708b5652697d2d6947f Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Tue, 15 Feb 2022 21:33:07 -0600 Subject: [PATCH 01/12] Update interacting with PyTorch --- docs/lang/articles/basic/external.md | 43 ++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index 3adf44322a1e7..7972e532be0fd 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -19,8 +19,8 @@ support NumPy, e.g. `matplotlib`. ```python {8} @ti.kernel def my_kernel(): - for i in x: - x[i] = i * 2 + for i in x: + x[i] = i * 2 x = ti.field(ti.f32, 4) my_kernel() @@ -41,12 +41,38 @@ print(x[2]) # 3 print(x[3]) # 5 ``` +Similarly, Taichi fields can be **imported from and exported to PyTorch tensors**: +```python +@ti.kernel +def my_kernel(): + for i in x: + x[i] = i * 2 + +x = ti.field(ti.f32, 4) +my_kernel() +x_torch = x.to_torch() +print(x_torch) # torch.tensor([0, 2, 4, 6]) + +x.from_numpy(torch.tensor([1, 7, 3, 5])) +print(x[0]) # 1 +print(x[1]) # 7 +print(x[2]) # 3 +print(x[3]) # 5 +``` +When calling the `to_torch()` method, the PyTorch device where the Taichi field is exported can be specified using the `device` argument: +```python +x = ti.field(ti.f32, 4) +x.fill(3.0) +x_torch = x.to_torch(device="cuda:0") +print(x_torch.device) # device(type='cuda', index=0) +``` + ## External array shapes -Shapes of Taichi fields and those of corresponding NumPy arrays are closely +Shapes of Taichi fields and those of corresponding NumPy arrays or PyTorch tensors are closely connected via the following rules: -- For scalar fields, **the shape of NumPy array is exactly the same as +- For scalar fields, **the shape of NumPy array or PyTorch tensor is exactly the same as the Taichi field**: ```python @@ -60,7 +86,7 @@ field.from_numpy(array) # the input array must be of shape (256, 512) ``` - For vector fields, if the vector is `n`-D, then **the shape of NumPy - array should be** `(*field_shape, vector_n)`: + array or Pytorch tensor should be** `(*field_shape, vector_n)`: ```python field = ti.Vector.field(3, ti.i32, shape=(256, 512)) @@ -74,7 +100,7 @@ field.from_numpy(array) # the input array must be of shape (256, 512, 3) ``` - For matrix fields, if the matrix is `n`-by-`m` (`n x m`), then **the shape of NumPy -array should be** `(*field_shape, matrix_n, matrix_m)`: +array or Pytorch Tensor should be** `(*field_shape, matrix_n, matrix_m)`: ```python field = ti.Matrix.field(3, 4, ti.i32, shape=(256, 512)) @@ -88,7 +114,8 @@ array.shape # (256, 512, 3, 4) field.from_numpy(array) # the input array must be of shape (256, 512, 3, 4) ``` -- For struct fields, the external array will be exported as **a dictionary of arrays** with the keys being struct member names and values being struct member arrays. Nested structs will be exported as nested dictionaries: +- For struct fields, the external array will be exported as **a dictionary of NumPy arrays or Pytorch tensors** with the keys +being struct member names and values being struct member arrays. Nested structs will be exported as nested dictionaries: ```python field = ti.Struct.field({'a': ti.i32, 'b': ti.types.vector(float, 3)} shape=(256, 512)) @@ -104,7 +131,7 @@ field.from_numpy(array_dict) # the input array must have the same keys as the fi ## Using external arrays as Taichi kernel arguments -Use the type hint `ti.ext_arr()` for passing external arrays as kernel +Use the type hint `ti.ext_arr()` or `ti.any_arr()` for passing external arrays as kernel arguments. For example: ```python {10} From 5d33f04a6f87af714c0d266074dbc22b1badc85d Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Wed, 16 Feb 2022 17:52:50 -0600 Subject: [PATCH 02/12] Update external array indexing --- docs/lang/articles/basic/external.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index 7972e532be0fd..518e1e1ce0fc7 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -162,3 +162,30 @@ for i in range(n): for j in range(m): assert a[i, j] == i * j + i + j ``` + +Note that the elements in an external array must be indexed using a single square bracket. +This is in contrast to a Taichi vector or matrix field where field and matrix indices are indexed separately: +```python +@ti.kernel +def copy_vector(x: ti.template(), y: ti.ext_arr()): + for i, j in ti.ndrange(n, m): + for k in ti.static(range(3)): + y[i, j, k] = x[i, j][k] # correct + # y[i][j][k] = x[i, j][k] incorrect + # y[i, j][k] = x[i, j][k] incorrext +``` +Also, external arrays in a Taichi kernel are indexed using its **physical memory layout**. For PyTorch users, this implies that the PyTorch tensor [needs to be made contiguous](https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html) before passed into a Taichi kernel: +```python +@ti.kernel +def copy_scalar(x: ti.template(), y: ti.ext_arr()): + for i, j in x: + y[i, j] = x[i, j] + +x = ti.field(dtype=int, shape=(3, 3)) +y = torch.Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) +y = y.T # Transposing the tensor returns a view of the tensor which is not contiguous +copy(x, y) # error! +copy(x, y.clone()) # correct +copy(x, y.contiguous()) # correct +``` + From 309ca9fc738e1d008e29090094945a15a4ace915 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Wed, 16 Feb 2022 17:53:10 -0600 Subject: [PATCH 03/12] Update external.md --- docs/lang/articles/basic/external.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index 518e1e1ce0fc7..816631dafefff 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -172,7 +172,7 @@ def copy_vector(x: ti.template(), y: ti.ext_arr()): for k in ti.static(range(3)): y[i, j, k] = x[i, j][k] # correct # y[i][j][k] = x[i, j][k] incorrect - # y[i, j][k] = x[i, j][k] incorrext + # y[i, j][k] = x[i, j][k] incorrect ``` Also, external arrays in a Taichi kernel are indexed using its **physical memory layout**. For PyTorch users, this implies that the PyTorch tensor [needs to be made contiguous](https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html) before passed into a Taichi kernel: ```python From e9c98db4b729dcf2329f4bf6a48b7cb11a3f1f6b Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Wed, 16 Feb 2022 17:54:58 -0600 Subject: [PATCH 04/12] Update external.md --- docs/lang/articles/basic/external.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index 816631dafefff..a6a9383a2ccba 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -114,7 +114,7 @@ array.shape # (256, 512, 3, 4) field.from_numpy(array) # the input array must be of shape (256, 512, 3, 4) ``` -- For struct fields, the external array will be exported as **a dictionary of NumPy arrays or Pytorch tensors** with the keys +- For struct fields, the external array will be exported as **a dictionary of NumPy arrays or PyTorch tensors** with the keys being struct member names and values being struct member arrays. Nested structs will be exported as nested dictionaries: ```python @@ -164,7 +164,7 @@ for i in range(n): ``` Note that the elements in an external array must be indexed using a single square bracket. -This is in contrast to a Taichi vector or matrix field where field and matrix indices are indexed separately: +This contrasts with a Taichi vector or matrix field where field and matrix indices are indexed separately: ```python @ti.kernel def copy_vector(x: ti.template(), y: ti.ext_arr()): @@ -174,7 +174,9 @@ def copy_vector(x: ti.template(), y: ti.ext_arr()): # y[i][j][k] = x[i, j][k] incorrect # y[i, j][k] = x[i, j][k] incorrect ``` -Also, external arrays in a Taichi kernel are indexed using its **physical memory layout**. For PyTorch users, this implies that the PyTorch tensor [needs to be made contiguous](https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html) before passed into a Taichi kernel: +Also, external arrays in a Taichi kernel are indexed using its **physical memory layout**. For PyTorch users, +this implies that the PyTorch tensor [needs to be made contiguous](https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html) +before passed into a Taichi kernel: ```python @ti.kernel def copy_scalar(x: ti.template(), y: ti.ext_arr()): From b3eceb4da80f75474470b8d22b8042354990b14b Mon Sep 17 00:00:00 2001 From: Taichi Gardener Date: Wed, 16 Feb 2022 23:58:44 +0000 Subject: [PATCH 05/12] Auto Format --- docs/lang/articles/basic/external.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index a6a9383a2ccba..68cd26b0a0d1a 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -114,7 +114,7 @@ array.shape # (256, 512, 3, 4) field.from_numpy(array) # the input array must be of shape (256, 512, 3, 4) ``` -- For struct fields, the external array will be exported as **a dictionary of NumPy arrays or PyTorch tensors** with the keys +- For struct fields, the external array will be exported as **a dictionary of NumPy arrays or PyTorch tensors** with the keys being struct member names and values being struct member arrays. Nested structs will be exported as nested dictionaries: ```python @@ -190,4 +190,3 @@ copy(x, y) # error! copy(x, y.clone()) # correct copy(x, y.contiguous()) # correct ``` - From 6a6649c52ab8fcf844d3d93cfcf4a7133ecfa502 Mon Sep 17 00:00:00 2001 From: Ye Kuang Date: Thu, 17 Feb 2022 14:03:43 +0800 Subject: [PATCH 06/12] Update docs/lang/articles/basic/external.md Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com> --- docs/lang/articles/basic/external.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index 68cd26b0a0d1a..867011942942f 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -176,7 +176,7 @@ def copy_vector(x: ti.template(), y: ti.ext_arr()): ``` Also, external arrays in a Taichi kernel are indexed using its **physical memory layout**. For PyTorch users, this implies that the PyTorch tensor [needs to be made contiguous](https://pytorch.org/docs/stable/generated/torch.Tensor.contiguous.html) -before passed into a Taichi kernel: +before being passed into a Taichi kernel: ```python @ti.kernel def copy_scalar(x: ti.template(), y: ti.ext_arr()): From 9eb631728a61276e77c67a481f7e196ae5d86220 Mon Sep 17 00:00:00 2001 From: Ye Kuang Date: Thu, 17 Feb 2022 14:04:01 +0800 Subject: [PATCH 07/12] Update docs/lang/articles/basic/external.md Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com> --- docs/lang/articles/basic/external.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index 867011942942f..378945fb81cb7 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -114,7 +114,7 @@ array.shape # (256, 512, 3, 4) field.from_numpy(array) # the input array must be of shape (256, 512, 3, 4) ``` -- For struct fields, the external array will be exported as **a dictionary of NumPy arrays or PyTorch tensors** with the keys +- For struct fields, the external array will be exported as **a dictionary of NumPy arrays or PyTorch tensors** with keys being struct member names and values being struct member arrays. Nested structs will be exported as nested dictionaries: ```python From 65e0c5db536ca464d09dab34d684b82582f16e26 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Thu, 17 Feb 2022 00:57:04 -0600 Subject: [PATCH 08/12] Update docs/lang/articles/basic/external.md Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com> --- docs/lang/articles/basic/external.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index 378945fb81cb7..c37bc60caec78 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -131,7 +131,7 @@ field.from_numpy(array_dict) # the input array must have the same keys as the fi ## Using external arrays as Taichi kernel arguments -Use the type hint `ti.ext_arr()` or `ti.any_arr()` for passing external arrays as kernel +Use type hint `ti.ext_arr()` or `ti.any_arr()` to pass external arrays as kernel arguments. For example: ```python {10} From f4a5a366d00ad83a740990643ea0737b0d347b45 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Thu, 17 Feb 2022 00:57:11 -0600 Subject: [PATCH 09/12] Update docs/lang/articles/basic/external.md Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com> --- docs/lang/articles/basic/external.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index c37bc60caec78..debd56d331b57 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -72,7 +72,7 @@ print(x_torch.device) # device(type='cuda', index=0) Shapes of Taichi fields and those of corresponding NumPy arrays or PyTorch tensors are closely connected via the following rules: -- For scalar fields, **the shape of NumPy array or PyTorch tensor is exactly the same as +- For scalar fields, **the shape of NumPy array or PyTorch tensor equals the shape of the Taichi field**: ```python From 1393ed43102619cde6fc04a1b1cbbf2f8f67a308 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Thu, 17 Feb 2022 00:57:17 -0600 Subject: [PATCH 10/12] Update docs/lang/articles/basic/external.md Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com> --- docs/lang/articles/basic/external.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index debd56d331b57..cf60523338961 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -41,7 +41,7 @@ print(x[2]) # 3 print(x[3]) # 5 ``` -Similarly, Taichi fields can be **imported from and exported to PyTorch tensors**: +Likewise, Taichi fields can be **imported from and exported to PyTorch tensors**: ```python @ti.kernel def my_kernel(): From 17ace04d51679d8e493d51b624196c07ff251aeb Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Thu, 17 Feb 2022 00:57:36 -0600 Subject: [PATCH 11/12] Update docs/lang/articles/basic/external.md Co-authored-by: Vissidarte-Herman <93570324+Vissidarte-Herman@users.noreply.github.com> --- docs/lang/articles/basic/external.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/external.md b/docs/lang/articles/basic/external.md index cf60523338961..3e4e70fc07464 100644 --- a/docs/lang/articles/basic/external.md +++ b/docs/lang/articles/basic/external.md @@ -59,7 +59,7 @@ print(x[1]) # 7 print(x[2]) # 3 print(x[3]) # 5 ``` -When calling the `to_torch()` method, the PyTorch device where the Taichi field is exported can be specified using the `device` argument: +When calling `to_torch()`, specify the PyTorch device where the Taichi field is exported using the `device` argument: ```python x = ti.field(ti.f32, 4) x.fill(3.0) From c450f8f6321f9b6e1a9259ae01cbfb0949ea08a7 Mon Sep 17 00:00:00 2001 From: Andrew Sun Date: Fri, 15 Apr 2022 14:51:46 -0500 Subject: [PATCH 12/12] [vulkan] fix device score bug --- taichi/backends/vulkan/vulkan_device_creator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taichi/backends/vulkan/vulkan_device_creator.cpp b/taichi/backends/vulkan/vulkan_device_creator.cpp index c4285a5cc71e2..4f333f972517f 100644 --- a/taichi/backends/vulkan/vulkan_device_creator.cpp +++ b/taichi/backends/vulkan/vulkan_device_creator.cpp @@ -179,7 +179,7 @@ size_t get_device_score(VkPhysicalDevice device, VkSurfaceKHR surface) { score += size_t(properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) * 1000; - score += VK_API_VERSION_MINOR(properties.driverVersion) * 100; + score += VK_API_VERSION_MINOR(properties.apiVersion) * 100; return score; }