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.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright 2020 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. """
  16. @File : test_parse.py
  17. @Author:
  18. @Date : 2019-01-23 17:13
  19. @Desc :
  20. """
  21. import logging
  22. import numpy as np
  23. import mindspore as ms
  24. import mindspore.nn as nn
  25. from mindspore import Tensor
  26. from mindspore.common.api import ms_function, _executor
  27. from mindspore.ops.composite import core
  28. from mindspore.ops.functional import tensor_add
  29. from ...ut_filter import non_graph_engine
  30. # pylint: disable=W0613
  31. # W0613: unused-argument
  32. log = logging.getLogger("test")
  33. log.setLevel(level=logging.ERROR)
  34. # Test case: use the parse obj interface use default parameter
  35. class Net(nn.Cell):
  36. """ Net definition """
  37. def __init__(self, dim):
  38. super(Net, self).__init__()
  39. self.softmax1 = nn.Softmax(dim)
  40. self.softmax2 = nn.Softmax(dim + 1)
  41. def construct(self, input_data, input1=ms.Tensor(np.random.randn(2, 3, 4, 5).astype(np.float32))):
  42. return self.softmax1(input_data)
  43. @non_graph_engine
  44. def test_parse_defalut_parameter_case2():
  45. """ test_parse_defalut_parameter_case2 """
  46. log.debug("begin test_parse_defalut_parameter_case2")
  47. net = Net(0)
  48. npd = np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')
  49. log.debug("input value is: %r", npd)
  50. input_data = ms.Tensor(npd)
  51. input_data.set_dtype(ms.float32)
  52. log.debug("start run")
  53. output = net(input_data)
  54. value = output.asnumpy()
  55. log.debug("output value = %r", value)
  56. # Test case: use the variable parameter for parse object
  57. class Net1(nn.Cell):
  58. """ Net1 definition """
  59. def __init__(self):
  60. super(Net1, self).__init__()
  61. def construct(self, *args):
  62. x = args[0]
  63. return x
  64. def test_var_parameter_case2():
  65. """ test_var_parameter_case2 """
  66. log.debug("begin test_var_parameter_case2")
  67. net = Net1()
  68. npd = np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')
  69. log.debug("input value is: %r", npd)
  70. input_data = ms.Tensor(npd)
  71. input_data.set_dtype(ms.float32)
  72. np1 = np.random.randn(2, 3, 4, 5).astype(np.float32)
  73. input1 = ms.Tensor(np1)
  74. np2 = np.random.randn(2, 3, 4, 5).astype(np.float32)
  75. input2 = ms.Tensor(np2)
  76. _executor.compile(net, input_data, input1, input2)
  77. # Test case: test the global flag
  78. g_x = Tensor(np.ones([3, 3]).astype(np.float32))
  79. @ms_function
  80. def tensor_add_global(x):
  81. """ tensor_add_global """
  82. global g_x
  83. res = tensor_add(x, g_x)
  84. return res
  85. @non_graph_engine
  86. def test_global_flag():
  87. """ test_global_flag """
  88. log.debug("begin test_global_flag")
  89. x = Tensor(np.ones([3, 3]).astype(np.float32))
  90. res = tensor_add_global(x)
  91. log.debug("finished test_global_flag, ret = %r", res)
  92. class NetWithNDarray(nn.Cell):
  93. """ NetWithNDarray definition """
  94. def __init__(self, dim):
  95. super(NetWithNDarray, self).__init__()
  96. self.softmax = nn.Softmax(dim)
  97. self.x = ms.Tensor(np.ones(shape=(1)).astype(np.float32))
  98. def construct(self, input_data):
  99. return self.softmax(input_data) * self.x
  100. @non_graph_engine
  101. def test_net_with_ndarray():
  102. """ test_net_with_ndarray """
  103. net = NetWithNDarray(0)
  104. input_data = np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32')
  105. net(ms.Tensor(input_data))