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

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # Copyright 2019-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 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, 2, 1, 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, 1, 2, 3, 2, 2, 0, 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. 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. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  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=tuple(set(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=tuple(set(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. x_1 = x8
  219. axis_1 = 0
  220. x_2 = x1
  221. axis_2 = 0
  222. class ReduceSumDynamic(nn.Cell):
  223. def __init__(self, x, axis):
  224. super(ReduceSumDynamic, self).__init__()
  225. self.reducesum = P.ReduceSum(True)
  226. self.test_dynamic = inner.GpuConvertToDynamicShape()
  227. self.x = x
  228. self.axis = axis
  229. def construct(self):
  230. dynamic_x = self.test_dynamic(self.x)
  231. return self.reducesum(dynamic_x, self.axis)
  232. @pytest.mark.level0
  233. @pytest.mark.platform_x86_gpu_training
  234. @pytest.mark.env_onecard
  235. def test_reduce_sum_dynamic():
  236. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  237. net1 = ReduceSumDynamic(Tensor(x_1), axis_1)
  238. net2 = ReduceSumDynamic(Tensor(x_2), axis_2)
  239. expect_1 = np.sum(x_1, axis=axis_1, keepdims=True)
  240. expect_2 = np.sum(x_2, axis=axis_2, keepdims=True)
  241. output1 = net1()
  242. output2 = net2()
  243. np.testing.assert_almost_equal(output1.asnumpy(), expect_1)
  244. np.testing.assert_almost_equal(output2.asnumpy(), expect_2)
  245. class ReduceSumTypeNet(nn.Cell):
  246. def __init__(self, nptype):
  247. super(ReduceSumTypeNet, self).__init__()
  248. self.x0 = Tensor(x0.astype(nptype))
  249. self.axis0 = axis0
  250. self.keep_dims0 = keep_dims0
  251. def construct(self):
  252. return P.ReduceSum(self.keep_dims0)(self.x0, self.axis0)
  253. @pytest.mark.level0
  254. @pytest.mark.platform_x86_gpu_training
  255. @pytest.mark.env_onecard
  256. def test_reduce_sum_float64():
  257. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  258. net = ReduceSumTypeNet(np.float64)
  259. output = net()
  260. expect = np.sum(x0, axis=axis0, keepdims=keep_dims0).astype(np.float64)
  261. diff = abs(output.asnumpy() - expect)
  262. error = np.ones(shape=expect.shape) * 1.0e-5
  263. assert np.all(diff < error)
  264. assert output.shape == expect.shape
  265. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  266. net = ReduceSumTypeNet(np.float64)
  267. output = net()
  268. expect = np.sum(x0, axis=axis0, keepdims=keep_dims0).astype(np.float64)
  269. diff = abs(output.asnumpy() - expect)
  270. error = np.ones(shape=expect.shape) * 1.0e-5
  271. assert np.all(diff < error)
  272. assert output.shape == expect.shape