• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15""" test_parse_numpy """
16import pytest
17import numpy as np
18from mindspore import nn
19from mindspore import context
20
21context.set_context(mode=context.GRAPH_MODE)
22
23
24def test_use_numpy_constant():
25    class Net(nn.Cell):
26        def __init__(self):
27            super(Net, self).__init__()
28
29        def construct(self):
30            ret = np.pi
31            return ret
32
33    net = Net()
34    output = net()
35    assert np.allclose(output, np.pi)
36
37
38def test_use_numpy_method():
39    class Net(nn.Cell):
40        def __init__(self):
41            super(Net, self).__init__()
42
43        def construct(self):
44            ret = np.linspace(1, 10, 4)
45            return ret
46
47    net = Net()
48    with pytest.raises(NotImplementedError) as err:
49        net()
50    assert "Mindspore does not support to use the numpy methods " \
51           "within the construct() or @ms_function decorated function in graph mode." \
52           in str(err.value)
53
54
55def test_use_numpy_module():
56    class Net(nn.Cell):
57        def __init__(self):
58            super(Net, self).__init__()
59
60        def construct(self):
61            ret = np.random.randint(0, 10, [1, 10])
62            return ret
63
64    net = Net()
65    with pytest.raises(NotImplementedError) as err:
66        net()
67    assert "Mindspore does not support to use the numpy methods " \
68           "within the construct() or @ms_function decorated function in graph mode." \
69           in str(err.value)
70