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.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. # Not raise NotImplementedError('Mindspore not supports to use the numpy ...') any more,
  40. # but raise RuntimeError('Should not use Python object in runtime...'), after support JIT Fallback.
  41. with pytest.raises(RuntimeError) as err:
  42. net()
  43. assert "Should not use Python object in runtime" 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. # Not raise NotImplementedError('Mindspore not supports to use the numpy ...') any more,
  53. # but raise RuntimeError('Should not use Python object in runtime...'), after support JIT Fallback.
  54. with pytest.raises(RuntimeError) as err:
  55. net()
  56. assert "Should not use Python object in runtime" in str(err.value)