From 27bdd4e3b9629d8b87a729d9882af7b101245e01 Mon Sep 17 00:00:00 2001 From: lianliguang Date: Thu, 11 Aug 2022 11:43:30 +0800 Subject: [PATCH] support load numpy array which is not c contiguous to create a tensor --- mindspore/python/mindspore/common/tensor.py | 3 +++ tests/ut/python/ir/test_tensor.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/mindspore/python/mindspore/common/tensor.py b/mindspore/python/mindspore/common/tensor.py index 0b8ee6cd42d..7864c349721 100644 --- a/mindspore/python/mindspore/common/tensor.py +++ b/mindspore/python/mindspore/common/tensor.py @@ -434,6 +434,9 @@ class Tensor(Tensor_): >>> print(output) [1 2] """ + if isinstance(array, np.ndarray) and not array.flags['C_CONTIGUOUS']: + array = np.ascontiguousarray(array) + return Tensor(Tensor_.from_numpy(array)) def assign_value(self, value): diff --git a/tests/ut/python/ir/test_tensor.py b/tests/ut/python/ir/test_tensor.py index 110772b8ed2..83bd1d0a5d3 100644 --- a/tests/ut/python/ir/test_tensor.py +++ b/tests/ut/python/ir/test_tensor.py @@ -555,3 +555,7 @@ def test_tensor_from_numpy(): with pytest.raises(TypeError): # incorrect input. t = ms.Tensor.from_numpy([1, 2, 3]) + + x = np.array([[1, 2], [3, 4]], order='F') + b = Tensor.from_numpy(x) + assert np.all(b.asnumpy() == np.array([[1, 2], [3, 4]]))