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

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