You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_parse_numpy.py 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 """
  16. import pytest
  17. import numpy as np
  18. from mindspore import nn
  19. from mindspore import context
  20. context.set_context(mode=context.GRAPH_MODE)
  21. def test_use_numpy_constant():
  22. class Net(nn.Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. def construct(self):
  26. ret = np.pi
  27. return ret
  28. net = Net()
  29. output = net()
  30. assert np.allclose(output, np.pi)
  31. def test_use_numpy_method():
  32. class Net(nn.Cell):
  33. def __init__(self):
  34. super(Net, self).__init__()
  35. def construct(self):
  36. ret = np.linspace(1, 10, 4)
  37. return ret
  38. net = Net()
  39. with pytest.raises(NotImplementedError) as err:
  40. net()
  41. assert "Mindspore does not support to use the numpy methods " \
  42. "within the construct() or @ms_function decorated function in graph mode." \
  43. in str(err.value)
  44. def test_use_numpy_module():
  45. class Net(nn.Cell):
  46. def __init__(self):
  47. super(Net, self).__init__()
  48. def construct(self):
  49. ret = np.random.randint(0, 10, [1, 10])
  50. return ret
  51. net = Net()
  52. with pytest.raises(NotImplementedError) as err:
  53. net()
  54. assert "Mindspore does not support to use the numpy methods " \
  55. "within the construct() or @ms_function decorated function in graph mode." \
  56. in str(err.value)