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_sum_op.py 8.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. # Copyright 2019 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. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. from mindspore.common.api import ms_function
  20. import numpy as np
  21. import mindspore.context as context
  22. x0 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  23. axis0 = 3
  24. keep_dims0 = True
  25. x1 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  26. axis1 = 3
  27. keep_dims1 = False
  28. x2 = np.random.rand(2, 3, 1, 4).astype(np.float32)
  29. axis2 = 2
  30. keep_dims2 = True
  31. x3 = np.random.rand(2, 3, 1, 4).astype(np.float32)
  32. axis3 = 2
  33. keep_dims3 = False
  34. x4 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  35. axis4 = ()
  36. np_axis4 = None
  37. keep_dims4 = True
  38. x5 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  39. axis5 = ()
  40. np_axis5 = None
  41. keep_dims5 = False
  42. x6 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  43. axis6 = (1, 2)
  44. keep_dims6 = False
  45. x7 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  46. axis7 = (1, 2)
  47. keep_dims7 = True
  48. x8 = np.random.rand(2, 1, 1, 4).astype(np.float32)
  49. axis8 = (1, 2)
  50. keep_dims8 = True
  51. x9 = np.random.rand(2, 1, 1, 4).astype(np.float32)
  52. axis9 = (1, 2)
  53. keep_dims9 = False
  54. x10 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  55. axis10 = (0, 1, 2, 3)
  56. keep_dims10 = False
  57. x11 = np.random.rand(1, 1, 1, 1).astype(np.float32)
  58. axis11 = (0, 1, 2, 3)
  59. keep_dims11 = False
  60. x12 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  61. axis12 = -2
  62. keep_dims12 = False
  63. x13 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  64. axis13 = (-2, -1)
  65. keep_dims13 = True
  66. context.set_context(device_target='GPU')
  67. class ReduceSum(nn.Cell):
  68. def __init__(self):
  69. super(ReduceSum, self).__init__()
  70. self.x0 = Tensor(x0)
  71. self.axis0 = axis0
  72. self.keep_dims0 = keep_dims0
  73. self.x1 = Tensor(x1)
  74. self.axis1 = axis1
  75. self.keep_dims1 = keep_dims1
  76. self.x2 = Tensor(x2)
  77. self.axis2 = axis2
  78. self.keep_dims2 = keep_dims2
  79. self.x3 = Tensor(x3)
  80. self.axis3 = axis3
  81. self.keep_dims3 = keep_dims3
  82. self.x4 = Tensor(x4)
  83. self.axis4 = axis4
  84. self.keep_dims4 = keep_dims4
  85. self.x5 = Tensor(x5)
  86. self.axis5 = axis5
  87. self.keep_dims5 = keep_dims5
  88. self.x6 = Tensor(x6)
  89. self.axis6 = axis6
  90. self.keep_dims6 = keep_dims6
  91. self.x7 = Tensor(x7)
  92. self.axis7 = axis7
  93. self.keep_dims7 = keep_dims7
  94. self.x8 = Tensor(x8)
  95. self.axis8 = axis8
  96. self.keep_dims8 = keep_dims8
  97. self.x9 = Tensor(x9)
  98. self.axis9 = axis9
  99. self.keep_dims9 = keep_dims9
  100. self.x10 = Tensor(x10)
  101. self.axis10 = axis10
  102. self.keep_dims10 = keep_dims10
  103. self.x11 = Tensor(x11)
  104. self.axis11 = axis11
  105. self.keep_dims11 = keep_dims11
  106. self.x12 = Tensor(x12)
  107. self.axis12 = axis12
  108. self.keep_dims12 = keep_dims12
  109. self.x13 = Tensor(x13)
  110. self.axis13 = axis13
  111. self.keep_dims13 = keep_dims13
  112. @ms_function
  113. def construct(self):
  114. return (P.ReduceSum(self.keep_dims0)(self.x0, self.axis0),
  115. P.ReduceSum(self.keep_dims1)(self.x1, self.axis1),
  116. P.ReduceSum(self.keep_dims2)(self.x2, self.axis2),
  117. P.ReduceSum(self.keep_dims3)(self.x3, self.axis3),
  118. P.ReduceSum(self.keep_dims4)(self.x4, self.axis4),
  119. P.ReduceSum(self.keep_dims5)(self.x5, self.axis5),
  120. P.ReduceSum(self.keep_dims6)(self.x6, self.axis6),
  121. P.ReduceSum(self.keep_dims7)(self.x7, self.axis7),
  122. P.ReduceSum(self.keep_dims8)(self.x8, self.axis8),
  123. P.ReduceSum(self.keep_dims9)(self.x9, self.axis9),
  124. P.ReduceSum(self.keep_dims10)(self.x10, self.axis10),
  125. P.ReduceSum(self.keep_dims11)(self.x11, self.axis11),
  126. P.ReduceSum(self.keep_dims12)(self.x12, self.axis12),
  127. P.ReduceSum(self.keep_dims13)(self.x13, self.axis13))
  128. @pytest.mark.level0
  129. @pytest.mark.platform_x86_gpu_training
  130. @pytest.mark.env_onecard
  131. def test_ReduceSum():
  132. reduce_sum = ReduceSum()
  133. output = reduce_sum()
  134. expect0 = np.sum(x0, axis=axis0, keepdims=keep_dims0)
  135. diff0 = output[0].asnumpy() - expect0
  136. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  137. assert np.all(diff0 < error0)
  138. assert (output[0].shape() == expect0.shape)
  139. expect1 = np.sum(x1, axis=axis1, keepdims=keep_dims1)
  140. diff1 = output[1].asnumpy() - expect1
  141. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  142. assert np.all(diff1 < error1)
  143. assert (output[1].shape() == expect1.shape)
  144. expect2 = np.sum(x2, axis=axis2, keepdims=keep_dims2)
  145. diff2 = output[2].asnumpy() - expect2
  146. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  147. assert np.all(diff2 < error2)
  148. assert (output[2].shape() == expect2.shape)
  149. expect3 = np.sum(x3, axis=axis3, keepdims=keep_dims3)
  150. diff3 = output[3].asnumpy() - expect3
  151. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  152. assert np.all(diff3 < error3)
  153. assert (output[3].shape() == expect3.shape)
  154. expect4 = np.sum(x4, axis=np_axis4, keepdims=keep_dims4)
  155. diff4 = output[4].asnumpy() - expect4
  156. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  157. assert np.all(diff4 < error4)
  158. assert (output[4].shape() == expect4.shape)
  159. expect5 = np.sum(x5, axis=np_axis5, keepdims=keep_dims5)
  160. diff5 = output[5].asnumpy() - expect5
  161. error5 = np.ones(shape=expect5.shape) * 1.0e-5
  162. assert np.all(diff5 < error5)
  163. assert (output[5].shape() == expect5.shape)
  164. expect6 = np.sum(x6, axis=axis6, keepdims=keep_dims6)
  165. diff6 = output[6].asnumpy() - expect6
  166. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  167. assert np.all(diff6 < error6)
  168. assert (output[6].shape() == expect6.shape)
  169. expect7 = np.sum(x7, axis=axis7, keepdims=keep_dims7)
  170. diff7 = output[7].asnumpy() - expect7
  171. error7 = np.ones(shape=expect7.shape) * 1.0e-5
  172. assert np.all(diff7 < error7)
  173. assert (output[7].shape() == expect7.shape)
  174. expect8 = np.sum(x8, axis=axis8, keepdims=keep_dims8)
  175. diff8 = output[8].asnumpy() - expect8
  176. error8 = np.ones(shape=expect8.shape) * 1.0e-5
  177. assert np.all(diff8 < error8)
  178. assert (output[8].shape() == expect8.shape)
  179. expect9 = np.sum(x9, axis=axis9, keepdims=keep_dims9)
  180. diff9 = output[9].asnumpy() - expect9
  181. error9 = np.ones(shape=expect9.shape) * 1.0e-5
  182. assert np.all(diff9 < error9)
  183. assert (output[9].shape() == expect9.shape)
  184. expect10 = np.sum(x10, axis=axis10, keepdims=keep_dims10)
  185. diff10 = output[10].asnumpy() - expect10
  186. error10 = np.ones(shape=expect10.shape) * 1.0e-5
  187. assert np.all(diff10 < error10)
  188. assert (output[10].shape() == expect10.shape)
  189. expect11 = np.sum(x11, axis=axis11, keepdims=keep_dims11)
  190. diff11 = output[11].asnumpy() - expect11
  191. error11 = np.ones(shape=expect11.shape) * 1.0e-5
  192. assert np.all(diff11 < error11)
  193. assert (output[11].shape() == expect11.shape)
  194. expect12 = np.sum(x12, axis=axis12, keepdims=keep_dims12)
  195. diff12 = output[12].asnumpy() - expect12
  196. error12 = np.ones(shape=expect12.shape) * 1.0e-5
  197. assert np.all(diff12 < error12)
  198. assert (output[12].shape() == expect12.shape)
  199. expect13 = np.sum(x13, axis=axis13, keepdims=keep_dims13)
  200. diff13 = output[13].asnumpy() - expect13
  201. error13 = np.ones(shape=expect13.shape) * 1.0e-5
  202. assert np.all(diff13 < error13)
  203. assert (output[13].shape() == expect13.shape)