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

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