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_mean_op.py 8.6 kB

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