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 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. import pytest
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore.common.api import ms_function
  19. from mindspore.ops import composite as C
  20. from mindspore.ops import operations as P
  21. from mindspore.nn import Cell
  22. add = P.Add()
  23. hyper_map = C.HyperMap()
  24. @ms_function
  25. def main_noleaf(x, y):
  26. return hyper_map(add, x, y)
  27. def test_hypermap_noleaf_tuple_list_mix():
  28. """
  29. Feature: Check the types of inputs of HyperMap.
  30. Description: The types of inputs of HyperMap must be the same.
  31. Expectation: The types of inputs of HyperMap must be the same.
  32. """
  33. tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  34. tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  35. with pytest.raises(Exception, match="the types of arguments in HyperMap must be consistent"):
  36. main_noleaf((tensor1, 1), [tensor2, 2])
  37. def test_hypermap_noleaf_tuple_length():
  38. """
  39. Feature: Check the length of arg of Tuple in HyperMap.
  40. Description: The length of inputs of HyperMap must be the same.
  41. Expectation: The length of inputs of HyperMap must be the same.
  42. """
  43. tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  44. tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  45. with pytest.raises(Exception, match="The length of tuples in HyperMap must be the same"):
  46. main_noleaf((tensor1, 1), (tensor2, 2, 2))
  47. def test_hypermap_noleaf_list_length():
  48. """
  49. Feature: Check the length of arg of List in HyperMap.
  50. Description: Check the length of arg of List in HyperMap.
  51. Expectation: Check the length of arg of List in HyperMap.
  52. """
  53. tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  54. tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  55. with pytest.raises(Exception, match="The lists in HyperMap should have the same length"):
  56. main_noleaf([tensor1], [tensor2, tensor2])
  57. def test_hypermap_noleaf_list_tuple():
  58. """
  59. Feature: Check the types of inputs of HyperMap.
  60. Description: The types of inputs of HyperMap must be the same.
  61. Expectation: The types of inputs of HyperMap must be the same.
  62. """
  63. tensor1 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  64. tensor2 = Tensor(np.array([[1.2, 2.1], [2.2, 3.2]]).astype('float32'))
  65. with pytest.raises(Exception, match="the types of arguments in HyperMap must be consistent"):
  66. main_noleaf([tensor1], (tensor2, tensor2))
  67. def test_tuple_slice_stop_index():
  68. """
  69. Feature: Check the type of stop index of slice.
  70. Description: The type of stop index of slice must be scalar, None or Tensor.
  71. Expectation: The type of stop index of slice must be scalar, None or Tensor.
  72. """
  73. class TupleSliceNet(Cell):
  74. def __init__(self):
  75. super(TupleSliceNet, self).__init__()
  76. self.addn = P.AddN()
  77. self.index_0 = Tensor(3)
  78. def construct(self, tensor_tuple):
  79. tensor_tuple_slice0 = tensor_tuple[:]
  80. tensor_tuple_slice1 = tensor_tuple[self.index_0:"str"] # slice should be Scalar or None, rather than string
  81. sum0 = self.addn(tensor_tuple_slice0)
  82. sum1 = self.addn(tensor_tuple_slice1)
  83. ret = sum0 + sum1
  84. return ret
  85. data = (Tensor(np.ones([2, 3, 4], np.int32)),
  86. Tensor(np.zeros([2, 3, 4], np.int32)),
  87. Tensor(np.ones([2, 3, 4], np.int32)),
  88. Tensor(np.ones([2, 3, 4], np.int32)),
  89. Tensor(np.zeros([2, 3, 4], np.int32)),
  90. Tensor(np.ones([2, 3, 4], np.int32)))
  91. net = TupleSliceNet()
  92. with pytest.raises(Exception, match="Slice indices must be integers or bool."):
  93. output = net(data)
  94. print("output:", output)
  95. def test_tuple_slice_start_index():
  96. """
  97. Feature: Check the type of start index of slice.
  98. Description: The type of start index of slice must be scalar, None or Tensor.
  99. Expectation: The type of start index of slice must be scalar, None or Tensor.
  100. """
  101. class TupleSliceNet(Cell):
  102. def __init__(self):
  103. super(TupleSliceNet, self).__init__()
  104. self.addn = P.AddN()
  105. self.index_0 = Tensor(3)
  106. self.index_1 = Tensor([5])
  107. self.index_3 = Tensor([True])
  108. def construct(self, tensor_tuple):
  109. tensor_tuple_slice0 = tensor_tuple[:]
  110. tensor_tuple_slice1 = tensor_tuple["str":self.index_0]
  111. tensor_tuple_slice2 = tensor_tuple[self.index_3:]
  112. tensor_tuple_slice3 = tensor_tuple[2:self.index_1:]
  113. sum0 = self.addn(tensor_tuple_slice0)
  114. sum1 = self.addn(tensor_tuple_slice1)
  115. sum2 = self.addn(tensor_tuple_slice2)
  116. sum3 = self.addn(tensor_tuple_slice3)
  117. ret = sum0 + sum1 + sum2 + sum3
  118. return ret
  119. data = (Tensor(np.ones([2, 3, 4], np.int32)),
  120. Tensor(np.zeros([2, 3, 4], np.int32)),
  121. Tensor(np.ones([2, 3, 4], np.int32)),
  122. Tensor(np.ones([2, 3, 4], np.int32)),
  123. Tensor(np.zeros([2, 3, 4], np.int32)),
  124. Tensor(np.ones([2, 3, 4], np.int32)))
  125. net = TupleSliceNet()
  126. with pytest.raises(Exception, match="Slice indices must be integers or bool."):
  127. output = net(data)
  128. print("output:", output)
  129. def test_tuple_slice_step():
  130. """
  131. Feature: Check the type of step of slice.
  132. Description: The type of step of slice must not be 0.
  133. Expectation: The type of step of slice must be scalar, None or Tensor.
  134. """
  135. class TupleSliceNet(Cell):
  136. def __init__(self):
  137. super(TupleSliceNet, self).__init__()
  138. self.addn = P.AddN()
  139. self.index_0 = Tensor(3)
  140. self.index_1 = Tensor([5])
  141. self.index_3 = Tensor([True])
  142. def construct(self, tensor_tuple):
  143. tensor_tuple_slice0 = tensor_tuple[:]
  144. tensor_tuple_slice1 = tensor_tuple[:self.index_0]
  145. tensor_tuple_slice2 = tensor_tuple[self.index_3:]
  146. tensor_tuple_slice3 = tensor_tuple[2:self.index_1:0]
  147. sum0 = self.addn(tensor_tuple_slice0)
  148. sum1 = self.addn(tensor_tuple_slice1)
  149. sum2 = self.addn(tensor_tuple_slice2)
  150. sum3 = self.addn(tensor_tuple_slice3)
  151. ret = sum0 + sum1 + sum2 + sum3
  152. return ret
  153. data = (Tensor(np.ones([2, 3, 4], np.int32)),
  154. Tensor(np.zeros([2, 3, 4], np.int32)),
  155. Tensor(np.ones([2, 3, 4], np.int32)),
  156. Tensor(np.ones([2, 3, 4], np.int32)),
  157. Tensor(np.zeros([2, 3, 4], np.int32)),
  158. Tensor(np.ones([2, 3, 4], np.int32)))
  159. net = TupleSliceNet()
  160. with pytest.raises(Exception, match="Slice step cannot be zero."):
  161. output = net(data)
  162. print("output:", output)