Skip to content

Commit

Permalink
Update on "[bug] [lang] Fix copyback for fortran contiguous numpy arr…
Browse files Browse the repository at this point in the history
…ays" (#6390)"

Attempt #2
This PR got reverted due to failed release tests.

[ghstack-poisoned]
  • Loading branch information
Ailing Zhang committed Oct 20, 2022
2 parents 5ce7d80 + e211c93 commit f6ce7fd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/taichi/lang/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def from_numpy(self, arr):
"""Copies the data from a `numpy.ndarray` into this field.
"""
if not arr.flags.c_contiguous:
import numpy as np # pylint: disable=C0415
arr = np.ascontiguousarray(arr)
self._from_external_arr(arr)

Expand Down
17 changes: 17 additions & 0 deletions tests/python/test_numpy_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,20 @@ def test_numpy_io_example():
assert arr.shape == (n, m, 3, 4)

# For PyTorch tensors, use to_torch/from_torch instead


@test_utils.test(exclude=[ti.cc])
def test_from_numpy_non_contiguous():
n = 9
m = 7
p = 4
arr = np.ones(shape=(n, m, p, p), dtype=np.int32)

val = ti.field(ti.i32, shape=(2, 2))
val.from_numpy(arr[0:6:3, 0:6:3, 0, 0])

vec = ti.Vector.field(3, dtype=ti.i32, shape=(2, 2))
vec.from_numpy(arr[0:6:3, 0:6:3, 0:3, 0])

mat = ti.Matrix.field(3, 4, dtype=ti.i32, shape=(2, 2))
mat.from_numpy(arr[0:6:3, 0:6:3, 0:3, 0:4])

0 comments on commit f6ce7fd

Please sign in to comment.