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

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