!10755 [Numpy-Native] Add new numpy-native interfaces to mindspore.numpy
From: @yanglf1121 Reviewed-by: Signed-off-by:pull/10755/MERGE
commit
eacc8bac89
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,4 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,102 +0,0 @@
|
||||
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
"""unit tests for numpy math operations"""
|
||||
|
||||
import pytest
|
||||
import numpy as onp
|
||||
|
||||
import mindspore.numpy as mnp
|
||||
|
||||
|
||||
def rand_int(*shape):
|
||||
"""return an random integer array with parameter shape"""
|
||||
res = onp.random.randint(low=1, high=5, size=shape)
|
||||
if isinstance(res, onp.ndarray):
|
||||
res = res.astype(onp.float32)
|
||||
return res
|
||||
|
||||
|
||||
class Cases():
|
||||
def __init__(self):
|
||||
|
||||
self.arrs = [
|
||||
rand_int(2),
|
||||
rand_int(2, 3),
|
||||
rand_int(2, 3, 4),
|
||||
rand_int(2, 3, 4, 5),
|
||||
]
|
||||
|
||||
# scalars expanded across the 0th dimension
|
||||
self.scalars = [
|
||||
rand_int(),
|
||||
rand_int(1),
|
||||
rand_int(1, 1),
|
||||
rand_int(1, 1, 1),
|
||||
]
|
||||
|
||||
# arrays with last dimension aligned
|
||||
self.aligned_arrs = [
|
||||
rand_int(2, 3),
|
||||
rand_int(1, 4, 3),
|
||||
rand_int(5, 1, 2, 3),
|
||||
rand_int(4, 2, 1, 1, 3),
|
||||
]
|
||||
|
||||
|
||||
test_case = Cases()
|
||||
|
||||
|
||||
def mnp_inner(a, b):
|
||||
return mnp.inner(a, b)
|
||||
|
||||
|
||||
def onp_inner(a, b):
|
||||
return onp.inner(a, b)
|
||||
|
||||
|
||||
def test_inner():
|
||||
for arr1 in test_case.aligned_arrs:
|
||||
for arr2 in test_case.aligned_arrs:
|
||||
match_res(mnp_inner, onp_inner, arr1, arr2)
|
||||
|
||||
for scalar1 in test_case.scalars:
|
||||
for scalar2 in test_case.scalars:
|
||||
match_res(mnp_inner, onp_inner,
|
||||
scalar1, scalar2)
|
||||
|
||||
|
||||
# check if the output from mnp function and onp function applied on the arrays are matched
|
||||
|
||||
|
||||
def match_res(mnp_fn, onp_fn, arr1, arr2):
|
||||
actual = mnp_fn(mnp.asarray(arr1, dtype='float32'),
|
||||
mnp.asarray(arr2, dtype='float32')).asnumpy()
|
||||
expected = onp_fn(arr1, arr2)
|
||||
match_array(actual, expected)
|
||||
|
||||
|
||||
def match_array(actual, expected, error=5):
|
||||
if error > 0:
|
||||
onp.testing.assert_almost_equal(actual.tolist(), expected.tolist(),
|
||||
decimal=error)
|
||||
else:
|
||||
onp.testing.assert_equal(actual.tolist(), expected.tolist())
|
||||
|
||||
|
||||
def test_exception_innner():
|
||||
with pytest.raises(ValueError):
|
||||
mnp.inner(mnp.asarray(test_case.arrs[0]),
|
||||
mnp.asarray(test_case.arrs[1]))
|
@ -0,0 +1,79 @@
|
||||
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
""" test astype"""
|
||||
import pytest
|
||||
|
||||
import mindspore.nn as nn
|
||||
import mindspore.common.dtype as mstype
|
||||
from mindspore import Tensor
|
||||
from mindspore import context
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
|
||||
|
||||
def test_astype():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.astype("float16")
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res.dtype == mstype.float16
|
||||
|
||||
|
||||
def test_astype_1():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.int64)
|
||||
|
||||
def construct(self):
|
||||
return self.value.astype(mstype.bool_)
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res.dtype == mstype.bool_
|
||||
|
||||
|
||||
def test_astype_2():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float64)
|
||||
|
||||
def construct(self):
|
||||
return self.value.astype(mstype.uint64)
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res.dtype == mstype.uint64
|
||||
|
||||
|
||||
def test_astype_error_1():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.astype("float88")
|
||||
|
||||
net = Net()
|
||||
with pytest.raises(TypeError):
|
||||
net()
|
@ -0,0 +1,77 @@
|
||||
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
""" test flatten"""
|
||||
import pytest
|
||||
|
||||
import mindspore.nn as nn
|
||||
import mindspore.common.dtype as mstype
|
||||
from mindspore import Tensor
|
||||
from mindspore import context
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
|
||||
|
||||
def test_flatten():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.flatten()
|
||||
|
||||
net = Net()
|
||||
net()
|
||||
|
||||
|
||||
def test_flatten_1():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.flatten(order='F')
|
||||
|
||||
net = Net()
|
||||
net()
|
||||
|
||||
|
||||
def test_flatten_error():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.flatten(order='X')
|
||||
|
||||
net = Net()
|
||||
with pytest.raises(ValueError):
|
||||
net()
|
||||
|
||||
|
||||
def test_flatten_error_1():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.flatten(order=123)
|
||||
|
||||
net = Net()
|
||||
with pytest.raises(TypeError):
|
||||
net()
|
@ -0,0 +1,103 @@
|
||||
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
""" test tensor properties in graph mode"""
|
||||
|
||||
import numpy as np
|
||||
|
||||
import mindspore.nn as nn
|
||||
import mindspore.common.dtype as mstype
|
||||
from mindspore import Tensor
|
||||
from mindspore import context
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
|
||||
|
||||
def test_ndim():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor(np.random.random(
|
||||
(2, 3, 4, 5)), dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.ndim
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res == 4
|
||||
|
||||
|
||||
def test_nbytes():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor(np.random.random(
|
||||
(2, 3, 4, 5)), dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.nbytes
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res == 480
|
||||
|
||||
|
||||
def test_size():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor(np.random.random(
|
||||
(2, 3, 4, 5)), dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.size
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res == 120
|
||||
|
||||
|
||||
def test_strides():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor(np.random.random(
|
||||
(2, 3, 4, 5)), dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.strides
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res == (240, 80, 20, 4)
|
||||
|
||||
|
||||
def test_itemsize():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value1 = Tensor(np.random.random(
|
||||
(2, 3, 4, 5)), dtype=mstype.float64)
|
||||
self.value2 = Tensor(np.random.random(
|
||||
(2, 3, 4, 5)), dtype=mstype.int32)
|
||||
self.value3 = Tensor(np.random.random(
|
||||
(2, 3, 4, 5)), dtype=mstype.bool_)
|
||||
|
||||
def construct(self):
|
||||
return (self.value1.itemsize, self.value2.itemsize, self.value3.itemsize)
|
||||
|
||||
net = Net()
|
||||
res = net()
|
||||
assert res == (8, 4, 1)
|
@ -0,0 +1,90 @@
|
||||
# Copyright 2021 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
# ============================================================================
|
||||
""" test reshape"""
|
||||
import pytest
|
||||
|
||||
import mindspore.nn as nn
|
||||
import mindspore.common.dtype as mstype
|
||||
from mindspore import Tensor
|
||||
from mindspore import context
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
|
||||
|
||||
def test_reshape():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.reshape(-1)
|
||||
|
||||
net = Net()
|
||||
net()
|
||||
|
||||
|
||||
def test_reshape_1():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.reshape([3, 2, 1])
|
||||
|
||||
net = Net()
|
||||
net()
|
||||
|
||||
|
||||
def test_reshape_2():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.reshape((-1, 2))
|
||||
|
||||
net = Net()
|
||||
net()
|
||||
|
||||
|
||||
def test_reshape_error():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.reshape(1, 2, 4)
|
||||
|
||||
net = Net()
|
||||
with pytest.raises(ValueError):
|
||||
net()
|
||||
|
||||
|
||||
def test_reshape_error_1():
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.value = Tensor([[1, 2, 3], [4, 5, 6]], dtype=mstype.float32)
|
||||
|
||||
def construct(self):
|
||||
return self.value.reshape((1, 2, 3.5))
|
||||
|
||||
net = Net()
|
||||
with pytest.raises(TypeError):
|
||||
net()
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue