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_arithmetic_op.py 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. # Copyright 2020 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. import mindspore
  20. from mindspore import Tensor
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class SubNet(nn.Cell):
  24. def __init__(self):
  25. super(SubNet, self).__init__()
  26. self.sub = P.Sub()
  27. def construct(self, x, y):
  28. return self.sub(x, y)
  29. class DivNet(nn.Cell):
  30. def __init__(self):
  31. super(DivNet, self).__init__()
  32. self.div = P.Div()
  33. def construct(self, x, y):
  34. return self.div(x, y)
  35. class FloorDivNet(nn.Cell):
  36. def __init__(self):
  37. super(FloorDivNet, self).__init__()
  38. self.floor_div = P.FloorDiv()
  39. def construct(self, x, y):
  40. return self.floor_div(x, y)
  41. class ModNet(nn.Cell):
  42. def __init__(self):
  43. super(ModNet, self).__init__()
  44. self.mod = P.Mod()
  45. def construct(self, x, y):
  46. return self.mod(x, y)
  47. @pytest.mark.level0
  48. @pytest.mark.platform_x86_cpu
  49. @pytest.mark.env_onecard
  50. def test_sub():
  51. x = np.random.rand(2, 3, 4, 4).astype(np.float32)
  52. y = np.random.rand(4, 1).astype(np.float32)
  53. net = SubNet()
  54. output = net(Tensor(x), Tensor(y, mindspore.float32))
  55. expect_output = x - y
  56. assert np.all(output.asnumpy() == expect_output)
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_cpu_training
  59. @pytest.mark.env_onecard
  60. def test_div():
  61. prop = 1 if np.random.random() < 0.5 else -1
  62. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  63. y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  64. x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  65. y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  66. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  67. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  68. x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  69. y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  70. x4_np = np.array(768).astype(np.float32) * prop
  71. y4_np = np.array(3072.5).astype(np.float32) * prop
  72. x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  73. y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  74. x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  75. y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  76. x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  77. y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  78. x0 = Tensor(x0_np)
  79. y0 = Tensor(y0_np)
  80. x1 = Tensor(x1_np)
  81. y1 = Tensor(y1_np)
  82. x2 = Tensor(x2_np)
  83. y2 = Tensor(y2_np)
  84. x3 = Tensor(x3_np)
  85. y3 = Tensor(y3_np)
  86. x4 = Tensor(x4_np)
  87. y4 = Tensor(y4_np)
  88. x5 = Tensor(x5_np)
  89. y5 = Tensor(y5_np)
  90. x6 = Tensor(x6_np)
  91. y6 = Tensor(y6_np)
  92. x7 = Tensor(x7_np)
  93. y7 = Tensor(y7_np)
  94. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  95. div = DivNet()
  96. output0 = div(x0, y0)
  97. expect0 = np.divide(x0_np, y0_np)
  98. diff0 = output0.asnumpy() - expect0
  99. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  100. assert np.all(diff0 < error0)
  101. assert output0.shape == expect0.shape
  102. output1 = div(x1, y1)
  103. expect1 = np.divide(x1_np, y1_np)
  104. diff1 = output1.asnumpy() - expect1
  105. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  106. assert np.all(diff1 < error1)
  107. assert output1.shape == expect1.shape
  108. output2 = div(x2, y2)
  109. expect2 = np.divide(x2_np, y2_np).astype(np.float16)
  110. diff2 = output2.asnumpy() - expect2
  111. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  112. assert np.all(diff2 < error2)
  113. assert output2.shape == expect2.shape
  114. output3 = div(x3, y3)
  115. expect3 = np.divide(x3_np, y3_np)
  116. diff3 = output3.asnumpy() - expect3
  117. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  118. assert np.all(diff3 < error3)
  119. assert output3.shape == expect3.shape
  120. output4 = div(x4, y4)
  121. expect4 = np.divide(x4_np, y4_np)
  122. diff4 = output4.asnumpy() - expect4
  123. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  124. assert np.all(diff4 < error4)
  125. assert output4.shape == expect4.shape
  126. output5 = div(x5, y5)
  127. expect5 = x5_np // y5_np
  128. assert np.all(output5.asnumpy() == expect5)
  129. output6 = div(x6, y6)
  130. expect6 = np.divide(x6_np, y6_np)
  131. diff6 = output6.asnumpy() - expect6
  132. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  133. assert np.all(diff6 < error6)
  134. assert output6.shape == expect6.shape
  135. output7 = div(x7, y7)
  136. expect7 = np.divide(x7_np, y7_np).astype(np.int64)
  137. assert np.all(output7.asnumpy() == expect7)
  138. assert output7.shape == expect7.shape
  139. @pytest.mark.level0
  140. @pytest.mark.platform_x86_cpu_training
  141. @pytest.mark.env_onecard
  142. def test_floor_div():
  143. prop = 1 if np.random.random() < 0.5 else -1
  144. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  145. y0_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  146. x1_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  147. y1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  148. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  149. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  150. x3_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  151. y3_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  152. x4_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  153. y4_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  154. x0 = Tensor(x0_np)
  155. y0 = Tensor(y0_np)
  156. x1 = Tensor(x1_np)
  157. y1 = Tensor(y1_np)
  158. x2 = Tensor(x2_np)
  159. y2 = Tensor(y2_np)
  160. x3 = Tensor(x3_np)
  161. y3 = Tensor(y3_np)
  162. x4 = Tensor(x4_np)
  163. y4 = Tensor(y4_np)
  164. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  165. floor_div = FloorDivNet()
  166. output0 = floor_div(x0, y0)
  167. expect0 = np.floor_divide(x0_np, y0_np)
  168. diff0 = output0.asnumpy() - expect0
  169. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  170. assert np.all(diff0 < error0)
  171. assert output0.shape == expect0.shape
  172. output1 = floor_div(x1, y1)
  173. expect1 = np.floor_divide(x1_np, y1_np)
  174. diff1 = output1.asnumpy() - expect1
  175. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  176. assert np.all(diff1 < error1)
  177. assert output1.shape == expect1.shape
  178. output2 = floor_div(x2, y2)
  179. expect2 = np.floor_divide(x2_np, y2_np).astype(np.float16)
  180. diff2 = output2.asnumpy() - expect2
  181. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  182. assert np.all(diff2 < error2)
  183. assert output2.shape == expect2.shape
  184. output3 = floor_div(x3, y3)
  185. expect3 = np.floor_divide(x3_np, y3_np)
  186. diff3 = output3.asnumpy() - expect3
  187. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  188. assert np.all(diff3 < error3)
  189. assert output3.shape == expect3.shape
  190. output4 = floor_div(x4, y4)
  191. expect4 = np.floor_divide(x4_np, y4_np)
  192. diff4 = output4.asnumpy() - expect4
  193. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  194. assert np.all(diff4 < error4)
  195. assert output4.shape == expect4.shape
  196. @pytest.mark.level0
  197. @pytest.mark.platform_x86_cpu_training
  198. @pytest.mark.env_onecard
  199. def test_mod():
  200. prop = 1 if np.random.random() < 0.5 else -1
  201. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  202. y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  203. x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  204. y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  205. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  206. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  207. x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  208. y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  209. x4_np = np.array(768).astype(np.float32) * prop
  210. y4_np = np.array(3072.5).astype(np.float32) * prop
  211. x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  212. y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  213. x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  214. y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  215. x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  216. y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  217. x0 = Tensor(x0_np)
  218. y0 = Tensor(y0_np)
  219. x1 = Tensor(x1_np)
  220. y1 = Tensor(y1_np)
  221. x2 = Tensor(x2_np)
  222. y2 = Tensor(y2_np)
  223. x3 = Tensor(x3_np)
  224. y3 = Tensor(y3_np)
  225. x4 = Tensor(x4_np)
  226. y4 = Tensor(y4_np)
  227. x5 = Tensor(x5_np)
  228. y5 = Tensor(y5_np)
  229. x6 = Tensor(x6_np)
  230. y6 = Tensor(y6_np)
  231. x7 = Tensor(x7_np)
  232. y7 = Tensor(y7_np)
  233. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  234. mod = ModNet()
  235. output0 = mod(x0, y0)
  236. expect0 = np.mod(x0_np, y0_np)
  237. diff0 = output0.asnumpy() - expect0
  238. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  239. assert np.all(diff0 < error0)
  240. assert output0.shape == expect0.shape
  241. output1 = mod(x1, y1)
  242. expect1 = np.mod(x1_np, y1_np)
  243. diff1 = output1.asnumpy() - expect1
  244. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  245. assert np.all(diff1 < error1)
  246. assert output1.shape == expect1.shape
  247. output2 = mod(x2, y2)
  248. expect2 = np.mod(x2_np, y2_np).astype(np.float16)
  249. diff2 = output2.asnumpy() - expect2
  250. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  251. assert np.all(diff2 < error2)
  252. assert output2.shape == expect2.shape
  253. output3 = mod(x3, y3)
  254. expect3 = np.mod(x3_np, y3_np)
  255. diff3 = output3.asnumpy() - expect3
  256. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  257. assert np.all(diff3 < error3)
  258. assert output3.shape == expect3.shape
  259. output4 = mod(x4, y4)
  260. expect4 = np.mod(x4_np, y4_np)
  261. diff4 = output4.asnumpy() - expect4
  262. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  263. assert np.all(diff4 < error4)
  264. assert output4.shape == expect4.shape
  265. output5 = mod(x5, y5)
  266. expect5 = np.mod(x5_np, y5_np)
  267. assert np.all(output5.asnumpy() == expect5)
  268. assert output5.shape == expect5.shape
  269. output6 = mod(x6, y6)
  270. expect6 = np.mod(x6_np, y6_np)
  271. diff6 = output6.asnumpy() - expect6
  272. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  273. assert np.all(diff6 < error6)
  274. assert output6.shape == expect6.shape
  275. output7 = mod(x7, y7)
  276. expect7 = np.mod(x7_np, y7_np).astype(np.int64)
  277. assert np.all(output7.asnumpy() == expect7)
  278. assert output6.shape == expect6.shape
  279. test_sub()
  280. test_div()
  281. test_floor_div()
  282. test_mod()