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_reduce_op.py 7.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright 2020-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.ops import operations as P
  19. import mindspore.nn as nn
  20. import mindspore.context as context
  21. from mindspore.common.api import ms_function
  22. context.set_context(device_target="CPU")
  23. class NetReduce(nn.Cell):
  24. def __init__(self):
  25. super(NetReduce, self).__init__()
  26. self.axis0 = 0
  27. self.axis1 = 1
  28. self.axis2 = -1
  29. self.axis3 = (0, 1)
  30. self.axis4 = (0, 1, 2)
  31. self.axis5 = (-1,)
  32. self.axis6 = ()
  33. self.reduce_mean = P.ReduceMean(False)
  34. self.reduce_sum = P.ReduceSum(False)
  35. self.reduce_max = P.ReduceMax(False)
  36. self.reduce_min = P.ReduceMin(False)
  37. @ms_function
  38. def construct(self, indice):
  39. return (self.reduce_mean(indice, self.axis0),
  40. self.reduce_mean(indice, self.axis1),
  41. self.reduce_mean(indice, self.axis2),
  42. self.reduce_mean(indice, self.axis3),
  43. self.reduce_mean(indice, self.axis4),
  44. self.reduce_sum(indice, self.axis0),
  45. self.reduce_sum(indice, self.axis2),
  46. self.reduce_max(indice, self.axis0),
  47. self.reduce_max(indice, self.axis2),
  48. self.reduce_max(indice, self.axis5),
  49. self.reduce_max(indice, self.axis6),
  50. self.reduce_min(indice, self.axis0),
  51. self.reduce_min(indice, self.axis1),
  52. self.reduce_min(indice, self.axis2),
  53. self.reduce_min(indice, self.axis3),
  54. self.reduce_min(indice, self.axis4),
  55. self.reduce_min(indice, self.axis5),
  56. self.reduce_min(indice, self.axis6))
  57. class NetReduceLogic(nn.Cell):
  58. def __init__(self):
  59. super(NetReduceLogic, self).__init__()
  60. self.axis0 = 0
  61. self.axis1 = -1
  62. self.axis2 = (0, 1, 2)
  63. self.axis3 = ()
  64. self.reduce_all = P.ReduceAll(False)
  65. self.reduce_any = P.ReduceAny(False)
  66. @ms_function
  67. def construct(self, indice):
  68. return (self.reduce_all(indice, self.axis0),
  69. self.reduce_all(indice, self.axis1),
  70. self.reduce_all(indice, self.axis2),
  71. self.reduce_all(indice, self.axis3),
  72. self.reduce_any(indice, self.axis0),
  73. self.reduce_any(indice, self.axis1),
  74. self.reduce_any(indice, self.axis2),
  75. self.reduce_any(indice, self.axis3),)
  76. @pytest.mark.level0
  77. @pytest.mark.platform_x86_cpu
  78. @pytest.mark.env_onecard
  79. def test_reduce():
  80. reduce = NetReduce()
  81. indice = Tensor(np.array([
  82. [[0., 2., 1., 4., 0., 2.], [3., 1., 2., 2., 4., 0.]],
  83. [[2., 0., 1., 5., 0., 1.], [1., 0., 0., 4., 4., 3.]],
  84. [[4., 1., 4., 0., 0., 0.], [2., 5., 1., 0., 1., 3.]]
  85. ]).astype(np.float32))
  86. output = reduce(indice)
  87. print(output[0])
  88. print(output[1])
  89. print(output[2])
  90. print(output[3])
  91. print(output[4])
  92. print(output[5])
  93. print(output[6])
  94. print(output[7])
  95. print(output[8])
  96. print(output[9])
  97. print(output[10])
  98. print(output[11])
  99. print(output[12])
  100. print(output[13])
  101. print(output[14])
  102. print(output[15])
  103. print(output[16])
  104. print(output[17])
  105. expect_0 = np.array([[2., 1., 2., 3., 0., 1], [2., 2., 1., 2., 3., 2.]]).astype(np.float32)
  106. expect_1 = np.array([[1.5, 1.5, 1.5, 3., 2., 1.], [1.5, 0., 0.5, 4.5, 2., 2.], [3., 3., 2.5, 0., 0.5, 1.5]]).astype(
  107. np.float32)
  108. expect_2 = np.array([[1.5, 2.], [1.5, 2.], [1.5, 2.]]).astype(np.float32)
  109. expect_3 = np.array([2, 1.5, 1.5, 2.5, 1.5, 1.5]).astype(np.float32)
  110. expect_4 = np.array([1.75]).astype(np.float32)
  111. expect_5 = np.array([[6., 3., 6., 9., 0., 3.], [6., 6., 3., 6., 9., 6.]]).astype(np.float32)
  112. expect_6 = np.array([[9., 12.], [9., 12.], [9., 12.]]).astype(np.float32)
  113. expect_7 = np.array([[4., 2., 4., 5., 0., 2.], [3., 5., 2., 4., 4., 3.]]).astype(np.float32)
  114. expect_8 = np.array([[4., 4.], [5., 4.], [4., 5.]]).astype(np.float32)
  115. expect_9 = np.array([[0., 0., 1., 0., 0., 0.], [1., 0., 0., 0., 1., 0.]]).astype(np.float32)
  116. expect_10 = np.array([[0., 1., 1., 2., 0., 0.], [1., 0., 0., 4., 0., 1.], [2., 1., 1., 0., 0., 0.]]).astype(
  117. np.float32)
  118. expect_11 = np.array([[0., 0.], [0., 0.], [0., 0.]]).astype(np.float32)
  119. expect_12 = np.array([0., 0., 0., 0., 0., 0.]).astype(np.float32)
  120. assert (output[0].asnumpy() == expect_0).all()
  121. assert (output[1].asnumpy() == expect_1).all()
  122. assert (output[2].asnumpy() == expect_2).all()
  123. assert (output[3].asnumpy() == expect_3).all()
  124. assert (output[4].asnumpy() == expect_4).all()
  125. assert (output[5].asnumpy() == expect_5).all()
  126. assert (output[6].asnumpy() == expect_6).all()
  127. assert (output[7].asnumpy() == expect_7).all()
  128. assert (output[8].asnumpy() == expect_8).all()
  129. assert (output[9].asnumpy() == expect_8).all()
  130. assert (output[10].asnumpy() == 5.0).all()
  131. assert (output[11].asnumpy() == expect_9).all()
  132. assert (output[12].asnumpy() == expect_10).all()
  133. assert (output[13].asnumpy() == expect_11).all()
  134. assert (output[14].asnumpy() == expect_12).all()
  135. assert (output[15].asnumpy() == 0.0).all()
  136. assert (output[16].asnumpy() == expect_11).all()
  137. assert (output[17].asnumpy() == 0.0).all()
  138. @pytest.mark.level0
  139. @pytest.mark.platform_x86_cpu
  140. @pytest.mark.env_onecard
  141. def test_reduce_logic():
  142. reduce_logic = NetReduceLogic()
  143. indice_bool = Tensor([[[False, True, True, True, False, True],
  144. [True, True, True, True, True, False]],
  145. [[True, False, True, True, False, True],
  146. [True, False, False, True, True, True]],
  147. [[True, True, True, False, False, False],
  148. [True, True, True, False, True, True]]])
  149. output = reduce_logic(indice_bool)
  150. expect_all_1 = np.array([[False, False, True, False, False, False],
  151. [True, False, False, False, True, False]])
  152. expect_all_2 = np.array([[False, False], [False, False], [False, False]])
  153. expect_all_3 = False
  154. expect_all_4 = False
  155. expect_any_1 = np.array([[True, True, True, True, False, True], [True, True, True, True, True, True]])
  156. expect_any_2 = np.array([[True, True], [True, True], [True, True]])
  157. expect_any_3 = True
  158. expect_any_4 = True
  159. assert (output[0].asnumpy() == expect_all_1).all()
  160. assert (output[1].asnumpy() == expect_all_2).all()
  161. assert (output[2].asnumpy() == expect_all_3).all()
  162. assert (output[3].asnumpy() == expect_all_4).all()
  163. assert (output[4].asnumpy() == expect_any_1).all()
  164. assert (output[5].asnumpy() == expect_any_2).all()
  165. assert (output[6].asnumpy() == expect_any_3).all()
  166. assert (output[7].asnumpy() == expect_any_4).all()
  167. test_reduce()
  168. test_reduce_logic()