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_hypermap.py 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright 2022 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. import pytest
  16. import numpy as np
  17. from mindspore import context, nn, Tensor
  18. from mindspore import dtype as mstype
  19. from mindspore.ops import composite as C
  20. from mindspore.ops import operations as P
  21. context.set_context(mode=context.GRAPH_MODE)
  22. single_element_fg = C.MultitypeFuncGraph("single_element_fg")
  23. @single_element_fg.register("Tensor")
  24. def single_element_fg_for_tensor(x):
  25. return P.Square()(x)
  26. double_elements_fg = C.MultitypeFuncGraph("double_elements_fg")
  27. @double_elements_fg.register("Tensor", "Tuple")
  28. def double_elements_fg_for_tensor_tuple(x, y):
  29. return P.Tile()(x, y)
  30. @double_elements_fg.register("Tensor", "List")
  31. def double_elements_fg_for_tensor_list(x, y):
  32. return x + y[0]
  33. class HyperMapNet(nn.Cell):
  34. def __init__(self, fg):
  35. super(HyperMapNet, self).__init__()
  36. self.common_map = C.HyperMap()
  37. self.fg = fg
  38. def construct(self, nest_tensor_list):
  39. output = self.common_map(self.fg, *nest_tensor_list)
  40. return output
  41. @pytest.mark.level0
  42. @pytest.mark.platform_x86_ascend_training
  43. @pytest.mark.platform_arm_ascend_training
  44. @pytest.mark.env_onecard
  45. def test_single_element_hypermap_with_tensor_input():
  46. """
  47. Feature: HyperMap
  48. Description: Test whether the HyperMap with single tensor input can run successfully.
  49. Expectation: success.
  50. """
  51. x = (Tensor(np.array([1, 2, 3]), mstype.float32), Tensor(np.array([4, 5, 6]), mstype.float32))
  52. common_map = HyperMapNet(single_element_fg)
  53. output = common_map((x,))
  54. expect_output_1 = np.array([1.0, 4.0, 9.0])
  55. expect_output_2 = np.array([16.0, 25.0, 36.0])
  56. assert isinstance(output, tuple)
  57. assert len(output) == 2
  58. assert isinstance(output[0], Tensor)
  59. assert isinstance(output[1], Tensor)
  60. assert np.allclose(output[0].asnumpy(), expect_output_1)
  61. assert np.allclose(output[1].asnumpy(), expect_output_2)
  62. @pytest.mark.level0
  63. @pytest.mark.platform_x86_ascend_training
  64. @pytest.mark.platform_arm_ascend_training
  65. @pytest.mark.env_onecard
  66. def test_double_elements_hypermap_tensor_tuple_inputs():
  67. """
  68. Feature: HyperMap
  69. Description: Test whether the HyperMap with tensor and tuple inputs can run successfully.
  70. Expectation: success.
  71. """
  72. x = (Tensor(np.array([1, 2, 3]), mstype.float32), Tensor(np.array([4, 5, 6]), mstype.float32))
  73. y = ((1, 2), (2, 1))
  74. common_map = HyperMapNet(double_elements_fg)
  75. output = common_map((x, y))
  76. expect_output_1 = np.array([1.0, 2.0, 3.0, 1.0, 2.0, 3.0])
  77. expect_output_2 = np.array([[4.0, 5.0, 6.0], [4.0, 5.0, 6.0]])
  78. assert isinstance(output, tuple)
  79. assert len(output) == 2
  80. assert isinstance(output[0], Tensor)
  81. assert isinstance(output[1], Tensor)
  82. assert np.allclose(output[0].asnumpy(), expect_output_1)
  83. assert np.allclose(output[1].asnumpy(), expect_output_2)
  84. @pytest.mark.level0
  85. @pytest.mark.platform_x86_ascend_training
  86. @pytest.mark.platform_arm_ascend_training
  87. @pytest.mark.env_onecard
  88. def test_double_elements_hypermap_tensor_list_inputs():
  89. """
  90. Feature: HyperMap
  91. Description: Test whether the HyperMap with tensor and list inputs can run successfully.
  92. Expectation: success.
  93. """
  94. x = (Tensor(np.array([1, 2, 3]), mstype.float32), Tensor(np.array([4, 5, 6]), mstype.float32))
  95. y = ([1, 2], [2, 1])
  96. common_map = HyperMapNet(double_elements_fg)
  97. output = common_map((x, y))
  98. expect_output_1 = np.array([2.0, 3.0, 4.0])
  99. expect_output_2 = np.array([6.0, 7.0, 8.0])
  100. assert isinstance(output, tuple)
  101. assert len(output) == 2
  102. assert isinstance(output[0], Tensor)
  103. assert isinstance(output[1], Tensor)
  104. assert np.allclose(output[0].asnumpy(), expect_output_1)
  105. assert np.allclose(output[1].asnumpy(), expect_output_2)
  106. @pytest.mark.level0
  107. @pytest.mark.platform_x86_ascend_training
  108. @pytest.mark.platform_arm_ascend_training
  109. @pytest.mark.env_onecard
  110. def test_doubel_elements_hypermap_correct_mix_inputs():
  111. """
  112. Feature: HyperMap
  113. Description: Test whether the HyperMap with mix correct inputs (Tensor + Tuple and Tensor + List)
  114. can run successfully.
  115. Expectation: success.
  116. """
  117. x = (Tensor(np.array([1, 2, 3]), mstype.float32), Tensor(np.array([4, 5, 6]), mstype.float32))
  118. y = ((1, 2), [2, 1])
  119. common_map = HyperMapNet(double_elements_fg)
  120. output = common_map((x, y))
  121. expect_output_1 = np.array([1.0, 2.0, 3.0, 1.0, 2.0, 3.0])
  122. expect_output_2 = np.array([6.0, 7.0, 8.0])
  123. assert isinstance(output, tuple)
  124. assert len(output) == 2
  125. assert isinstance(output[0], Tensor)
  126. assert isinstance(output[1], Tensor)
  127. assert np.allclose(output[0].asnumpy(), expect_output_1)
  128. assert np.allclose(output[1].asnumpy(), expect_output_2)
  129. @pytest.mark.level0
  130. @pytest.mark.platform_x86_ascend_training
  131. @pytest.mark.platform_arm_ascend_training
  132. @pytest.mark.env_onecard
  133. def test_double_elements_hypermap_inputs_length_mismatch():
  134. """
  135. Feature: HyperMap
  136. Description: When the inputs to hypermap have different length, error will be raised.
  137. Expectation: error.
  138. """
  139. x = (Tensor(np.array([1, 2, 3]), mstype.float32), Tensor(np.array([4, 5, 6]), mstype.float32))
  140. y = ((1, 2), (2, 1), (5, 6))
  141. common_map = HyperMapNet(double_elements_fg)
  142. with pytest.raises(Exception, match="The length of tuples in HyperMap must be the same"):
  143. common_map((x, y))
  144. @pytest.mark.level0
  145. @pytest.mark.platform_x86_ascend_training
  146. @pytest.mark.platform_arm_ascend_training
  147. @pytest.mark.env_onecard
  148. def test_double_elements_hypermap_inconsistent_inputs():
  149. """
  150. Feature: HyperMap
  151. Description: When the inputs to hypermap is inconsistent, error will be raised.
  152. Expectation: error.
  153. """
  154. x = (Tensor(np.array([1, 2, 3]), mstype.float32), Tensor(np.array([4, 5, 6]), mstype.float32))
  155. y = [(1, 2), (2, 1)]
  156. common_map = HyperMapNet(double_elements_fg)
  157. with pytest.raises(Exception, match="the types of arguments in HyperMap must be consistent"):
  158. common_map((x, y))