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.6 kB

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