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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. class FloorModNet(nn.Cell):
  48. def __init__(self):
  49. super(FloorModNet, self).__init__()
  50. self.floor_mod = P.FloorMod()
  51. def construct(self, x, y):
  52. return self.floor_mod(x, y)
  53. @pytest.mark.level0
  54. @pytest.mark.platform_x86_cpu
  55. @pytest.mark.env_onecard
  56. def test_sub():
  57. x = np.random.rand(2, 3, 4, 4).astype(np.float32)
  58. y = np.random.rand(4, 1).astype(np.float32)
  59. net = SubNet()
  60. output = net(Tensor(x), Tensor(y, mindspore.float32))
  61. expect_output = x - y
  62. assert np.all(output.asnumpy() == expect_output)
  63. # float64
  64. x = np.random.rand(2, 3, 4, 4).astype(np.float64)
  65. y = np.random.rand(4, 1).astype(np.float64)
  66. net = SubNet()
  67. output = net(Tensor(x), Tensor(y, mindspore.float64))
  68. expect_output = x - y
  69. assert np.all(output.asnumpy() == expect_output)
  70. @pytest.mark.level0
  71. @pytest.mark.platform_x86_cpu
  72. @pytest.mark.env_onecard
  73. def test_div():
  74. prop = 1 if np.random.random() < 0.5 else -1
  75. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  76. y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  77. x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  78. y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  79. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  80. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  81. x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  82. y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  83. x4_np = np.array(768).astype(np.float32) * prop
  84. y4_np = np.array(3072.5).astype(np.float32) * prop
  85. x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  86. y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  87. x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  88. y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  89. x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  90. y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  91. x8_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float64) * prop
  92. y8_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float64) * prop
  93. x0 = Tensor(x0_np)
  94. y0 = Tensor(y0_np)
  95. x1 = Tensor(x1_np)
  96. y1 = Tensor(y1_np)
  97. x2 = Tensor(x2_np)
  98. y2 = Tensor(y2_np)
  99. x3 = Tensor(x3_np)
  100. y3 = Tensor(y3_np)
  101. x4 = Tensor(x4_np)
  102. y4 = Tensor(y4_np)
  103. x5 = Tensor(x5_np)
  104. y5 = Tensor(y5_np)
  105. x6 = Tensor(x6_np)
  106. y6 = Tensor(y6_np)
  107. x7 = Tensor(x7_np)
  108. y7 = Tensor(y7_np)
  109. x8 = Tensor(x8_np)
  110. y8 = Tensor(y8_np)
  111. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  112. div = DivNet()
  113. output0 = div(x0, y0)
  114. expect0 = np.divide(x0_np, y0_np)
  115. diff0 = output0.asnumpy() - expect0
  116. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  117. assert np.all(diff0 < error0)
  118. assert output0.shape == expect0.shape
  119. output1 = div(x1, y1)
  120. expect1 = np.divide(x1_np, y1_np)
  121. diff1 = output1.asnumpy() - expect1
  122. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  123. assert np.all(diff1 < error1)
  124. assert output1.shape == expect1.shape
  125. output2 = div(x2, y2)
  126. expect2 = np.divide(x2_np, y2_np).astype(np.float16)
  127. diff2 = output2.asnumpy() - expect2
  128. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  129. assert np.all(diff2 < error2)
  130. assert output2.shape == expect2.shape
  131. output3 = div(x3, y3)
  132. expect3 = np.divide(x3_np, y3_np)
  133. diff3 = output3.asnumpy() - expect3
  134. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  135. assert np.all(diff3 < error3)
  136. assert output3.shape == expect3.shape
  137. output4 = div(x4, y4)
  138. expect4 = np.divide(x4_np, y4_np)
  139. diff4 = output4.asnumpy() - expect4
  140. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  141. assert np.all(diff4 < error4)
  142. assert output4.shape == expect4.shape
  143. output5 = div(x5, y5)
  144. expect5 = x5_np // y5_np
  145. assert np.all(output5.asnumpy() == expect5)
  146. output6 = div(x6, y6)
  147. expect6 = np.divide(x6_np, y6_np)
  148. diff6 = output6.asnumpy() - expect6
  149. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  150. assert np.all(diff6 < error6)
  151. assert output6.shape == expect6.shape
  152. output7 = div(x7, y7)
  153. expect7 = np.divide(x7_np, y7_np).astype(np.int64)
  154. assert np.all(output7.asnumpy() == expect7)
  155. assert output7.shape == expect7.shape
  156. output8 = div(x8, y8)
  157. expect8 = np.divide(x8_np, y8_np)
  158. diff8 = output8.asnumpy() - expect8
  159. error8 = np.ones(shape=expect8.shape) * 1.0e-7
  160. assert np.all(diff8 < error8)
  161. assert output8.shape == expect8.shape
  162. @pytest.mark.level0
  163. @pytest.mark.platform_x86_cpu
  164. @pytest.mark.env_onecard
  165. def test_floor_div():
  166. prop = 1 if np.random.random() < 0.5 else -1
  167. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  168. y0_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  169. x1_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  170. y1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  171. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  172. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  173. x3_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  174. y3_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  175. x4_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  176. y4_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  177. x5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float64) * prop
  178. y5_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float64) * prop
  179. x0 = Tensor(x0_np)
  180. y0 = Tensor(y0_np)
  181. x1 = Tensor(x1_np)
  182. y1 = Tensor(y1_np)
  183. x2 = Tensor(x2_np)
  184. y2 = Tensor(y2_np)
  185. x3 = Tensor(x3_np)
  186. y3 = Tensor(y3_np)
  187. x4 = Tensor(x4_np)
  188. y4 = Tensor(y4_np)
  189. x5 = Tensor(x5_np)
  190. y5 = Tensor(y5_np)
  191. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  192. floor_div = FloorDivNet()
  193. output0 = floor_div(x0, y0)
  194. expect0 = np.floor_divide(x0_np, y0_np)
  195. diff0 = output0.asnumpy() - expect0
  196. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  197. assert np.all(diff0 < error0)
  198. assert output0.shape == expect0.shape
  199. output1 = floor_div(x1, y1)
  200. expect1 = np.floor_divide(x1_np, y1_np)
  201. diff1 = output1.asnumpy() - expect1
  202. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  203. assert np.all(diff1 < error1)
  204. assert output1.shape == expect1.shape
  205. output2 = floor_div(x2, y2)
  206. expect2 = np.floor_divide(x2_np, y2_np).astype(np.float16)
  207. diff2 = output2.asnumpy() - expect2
  208. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  209. assert np.all(diff2 < error2)
  210. assert output2.shape == expect2.shape
  211. output3 = floor_div(x3, y3)
  212. expect3 = np.floor_divide(x3_np, y3_np)
  213. diff3 = output3.asnumpy() - expect3
  214. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  215. assert np.all(diff3 < error3)
  216. assert output3.shape == expect3.shape
  217. output4 = floor_div(x4, y4)
  218. expect4 = np.floor_divide(x4_np, y4_np)
  219. diff4 = output4.asnumpy() - expect4
  220. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  221. assert np.all(diff4 < error4)
  222. assert output4.shape == expect4.shape
  223. output5 = floor_div(x5, y5)
  224. expect5 = np.floor_divide(x5_np, y5_np)
  225. diff5 = output5.asnumpy() - expect5
  226. error5 = np.ones(shape=expect5.shape) * 1.0e-7
  227. assert np.all(diff5 < error5)
  228. assert output5.shape == expect5.shape
  229. @pytest.mark.level0
  230. @pytest.mark.platform_x86_cpu
  231. @pytest.mark.env_onecard
  232. def test_mod():
  233. prop = 1 if np.random.random() < 0.5 else -1
  234. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  235. y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  236. x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  237. y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  238. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  239. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  240. x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  241. y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  242. x4_np = np.array(768).astype(np.float32) * prop
  243. y4_np = np.array(3072.5).astype(np.float32) * prop
  244. x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  245. y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  246. x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  247. y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  248. x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  249. y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  250. x0 = Tensor(x0_np)
  251. y0 = Tensor(y0_np)
  252. x1 = Tensor(x1_np)
  253. y1 = Tensor(y1_np)
  254. x2 = Tensor(x2_np)
  255. y2 = Tensor(y2_np)
  256. x3 = Tensor(x3_np)
  257. y3 = Tensor(y3_np)
  258. x4 = Tensor(x4_np)
  259. y4 = Tensor(y4_np)
  260. x5 = Tensor(x5_np)
  261. y5 = Tensor(y5_np)
  262. x6 = Tensor(x6_np)
  263. y6 = Tensor(y6_np)
  264. x7 = Tensor(x7_np)
  265. y7 = Tensor(y7_np)
  266. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  267. mod = ModNet()
  268. output0 = mod(x0, y0)
  269. expect0 = np.mod(x0_np, y0_np)
  270. diff0 = output0.asnumpy() - expect0
  271. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  272. assert np.all(diff0 < error0)
  273. assert output0.shape == expect0.shape
  274. output1 = mod(x1, y1)
  275. expect1 = np.mod(x1_np, y1_np)
  276. diff1 = output1.asnumpy() - expect1
  277. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  278. assert np.all(diff1 < error1)
  279. assert output1.shape == expect1.shape
  280. output2 = mod(x2, y2)
  281. expect2 = np.mod(x2_np, y2_np).astype(np.float16)
  282. diff2 = output2.asnumpy() - expect2
  283. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  284. assert np.all(diff2 < error2)
  285. assert output2.shape == expect2.shape
  286. output3 = mod(x3, y3)
  287. expect3 = np.mod(x3_np, y3_np)
  288. diff3 = output3.asnumpy() - expect3
  289. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  290. assert np.all(diff3 < error3)
  291. assert output3.shape == expect3.shape
  292. output4 = mod(x4, y4)
  293. expect4 = np.mod(x4_np, y4_np)
  294. diff4 = output4.asnumpy() - expect4
  295. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  296. assert np.all(diff4 < error4)
  297. assert output4.shape == expect4.shape
  298. output5 = mod(x5, y5)
  299. expect5 = np.mod(x5_np, y5_np)
  300. assert np.all(output5.asnumpy() == expect5)
  301. assert output5.shape == expect5.shape
  302. output6 = mod(x6, y6)
  303. expect6 = np.mod(x6_np, y6_np)
  304. diff6 = output6.asnumpy() - expect6
  305. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  306. assert np.all(diff6 < error6)
  307. assert output6.shape == expect6.shape
  308. output7 = mod(x7, y7)
  309. expect7 = np.mod(x7_np, y7_np).astype(np.int64)
  310. assert np.all(output7.asnumpy() == expect7)
  311. assert output6.shape == expect6.shape
  312. @pytest.mark.level0
  313. @pytest.mark.platform_x86_cpu
  314. @pytest.mark.env_onecard
  315. def test_floor_mod():
  316. prop = 1 if np.random.random() < 0.5 else -1
  317. x0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  318. y0_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  319. x1_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  320. y1_np = np.random.randint(1, 100, (2, 1, 4, 4)).astype(np.float32) * prop
  321. x2_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.float16) * prop
  322. y2_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float16) * prop
  323. x3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  324. y3_np = np.random.randint(1, 100, 1).astype(np.float32) * prop
  325. x4_np = np.array(768).astype(np.float32) * prop
  326. y4_np = np.array(3072.5).astype(np.float32) * prop
  327. x5_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int32) * prop
  328. y5_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  329. x6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int32) * prop
  330. y6_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.float32) * prop
  331. x7_np = np.random.randint(1, 100, (2, 1, 1, 4)).astype(np.int64) * prop
  332. y7_np = np.random.randint(1, 100, (2, 3, 4, 4)).astype(np.int64) * prop
  333. x0 = Tensor(x0_np)
  334. y0 = Tensor(y0_np)
  335. x1 = Tensor(x1_np)
  336. y1 = Tensor(y1_np)
  337. x2 = Tensor(x2_np)
  338. y2 = Tensor(y2_np)
  339. x3 = Tensor(x3_np)
  340. y3 = Tensor(y3_np)
  341. x4 = Tensor(x4_np)
  342. y4 = Tensor(y4_np)
  343. x5 = Tensor(x5_np)
  344. y5 = Tensor(y5_np)
  345. x6 = Tensor(x6_np)
  346. y6 = Tensor(y6_np)
  347. x7 = Tensor(x7_np)
  348. y7 = Tensor(y7_np)
  349. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  350. floor_mod = FloorModNet()
  351. output0 = floor_mod(x0, y0)
  352. expect0 = np.mod(x0_np, y0_np)
  353. diff0 = output0.asnumpy() - expect0
  354. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  355. assert np.all(diff0 < error0)
  356. assert output0.shape == expect0.shape
  357. output1 = floor_mod(x1, y1)
  358. expect1 = np.mod(x1_np, y1_np)
  359. diff1 = output1.asnumpy() - expect1
  360. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  361. assert np.all(diff1 < error1)
  362. assert output1.shape == expect1.shape
  363. output2 = floor_mod(x2, y2)
  364. expect2 = np.mod(x2_np, y2_np).astype(np.float16)
  365. diff2 = output2.asnumpy() - expect2
  366. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  367. assert np.all(diff2 < error2)
  368. assert output2.shape == expect2.shape
  369. output3 = floor_mod(x3, y3)
  370. expect3 = np.mod(x3_np, y3_np)
  371. diff3 = output3.asnumpy() - expect3
  372. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  373. assert np.all(diff3 < error3)
  374. assert output3.shape == expect3.shape
  375. output4 = floor_mod(x4, y4)
  376. expect4 = np.mod(x4_np, y4_np)
  377. diff4 = output4.asnumpy() - expect4
  378. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  379. assert np.all(diff4 < error4)
  380. assert output4.shape == expect4.shape
  381. output5 = floor_mod(x5, y5)
  382. expect5 = np.mod(x5_np, y5_np)
  383. assert np.all(output5.asnumpy() == expect5)
  384. assert output5.shape == expect5.shape
  385. output6 = floor_mod(x6, y6)
  386. expect6 = np.mod(x6_np, y6_np)
  387. diff6 = output6.asnumpy() - expect6
  388. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  389. assert np.all(diff6 < error6)
  390. assert output6.shape == expect6.shape
  391. output7 = floor_mod(x7, y7)
  392. expect7 = np.mod(x7_np, y7_np).astype(np.int64)
  393. assert np.all(output7.asnumpy() == expect7)
  394. assert output6.shape == expect6.shape
  395. test_sub()
  396. test_div()
  397. test_floor_div()
  398. test_mod()
  399. test_floor_mod()