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.py 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. import numpy as np
  15. from mindspore import context
  16. import mindspore.nn as nn
  17. from mindspore.ops import operations as P
  18. from mindspore import Tensor
  19. from tests.ut.python.ops.test_math_ops import VirtualLoss
  20. import mindspore as ms
  21. from mindspore.common.api import _executor
  22. from mindspore.ops import composite as C
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = VirtualLoss()
  27. self.network = network
  28. def construct(self, x, y, b):
  29. predict = self.network(x, y, b)
  30. return self.loss(predict)
  31. class GradWrap(nn.Cell):
  32. def __init__(self, network):
  33. super(GradWrap, self).__init__()
  34. self.network = network
  35. def construct(self, x, y, b):
  36. return C.grad_all(self.network)(x, y, b)
  37. def test_matmul_sub():
  38. class Net(nn.Cell):
  39. def __init__(self, strategy1, strategy2):
  40. super().__init__()
  41. self.matmul = P.MatMul().set_strategy(strategy1)
  42. self.sub = P.Sub().set_strategy(strategy2)
  43. def construct(self, x, y, b):
  44. out = self.matmul(x, y)
  45. out = self.sub(out, b)
  46. return out
  47. context.set_auto_parallel_context(device_num=8, global_rank=0)
  48. strategy1 = ((2, 2), (2, 2))
  49. strategy2 = ((4, 2), (4, 2))
  50. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  51. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  52. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  53. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  54. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  55. _executor.compile(net, x, y, b)
  56. def test_matmul_add():
  57. class Net(nn.Cell):
  58. def __init__(self, strategy1, strategy2):
  59. super().__init__()
  60. self.matmul = P.MatMul().set_strategy(strategy1)
  61. self.add = P.TensorAdd().set_strategy(strategy2)
  62. def construct(self, x, y, b):
  63. out = self.matmul(x, y)
  64. out = self.add(out, b)
  65. return out
  66. context.set_auto_parallel_context(device_num=8, global_rank=0)
  67. strategy1 = ((2, 2), (2, 2))
  68. strategy2 = ((4, 2), (4, 2))
  69. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  71. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  72. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  73. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  74. _executor.compile(net, x, y, b)
  75. def test_matmul_mul():
  76. class Net(nn.Cell):
  77. def __init__(self, strategy1, strategy2):
  78. super().__init__()
  79. self.matmul = P.MatMul().set_strategy(strategy1)
  80. self.mul = P.Mul().set_strategy(strategy2)
  81. def construct(self, x, y, b):
  82. out = self.matmul(x, y)
  83. out = self.mul(out, b)
  84. return out
  85. context.set_auto_parallel_context(device_num=8, global_rank=0)
  86. strategy1 = ((2, 2), (2, 2))
  87. strategy2 = ((4, 2), (4, 2))
  88. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  90. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  91. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  92. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  93. _executor.compile(net, x, y, b)
  94. def test_matmul_div():
  95. class Net(nn.Cell):
  96. def __init__(self, strategy1, strategy2):
  97. super().__init__()
  98. self.matmul = P.MatMul().set_strategy(strategy1)
  99. self.div = P.Div().set_strategy(strategy2)
  100. def construct(self, x, y, b):
  101. out = self.matmul(x, y)
  102. out = self.div(out, b)
  103. return out
  104. context.set_auto_parallel_context(device_num=8, global_rank=0)
  105. strategy1 = ((2, 2), (2, 2))
  106. strategy2 = ((4, 2), (4, 2))
  107. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  108. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  109. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  110. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  111. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  112. _executor.compile(net, x, y, b)
  113. def test_matmul_greater():
  114. class Net(nn.Cell):
  115. def __init__(self, strategy1, strategy2):
  116. super().__init__()
  117. self.matmul = P.MatMul().set_strategy(strategy1)
  118. self.greater = P.Greater().set_strategy(strategy2)
  119. def construct(self, x, y, b):
  120. out = self.matmul(x, y)
  121. out = self.greater(out, b)
  122. return out
  123. context.set_auto_parallel_context(device_num=8, global_rank=0)
  124. strategy1 = ((2, 2), (2, 2))
  125. strategy2 = ((4, 2), (4, 2))
  126. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  127. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  128. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  129. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  130. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  131. _executor.compile(net, x, y, b)
  132. def test_matmul_add_broadcast():
  133. class Net(nn.Cell):
  134. def __init__(self, strategy1, strategy2):
  135. super().__init__()
  136. self.matmul = P.MatMul().set_strategy(strategy1)
  137. self.add = P.TensorAdd().set_strategy(strategy2)
  138. def construct(self, x, y, b):
  139. out = self.matmul(x, y)
  140. out = self.add(out, b)
  141. return out
  142. context.set_auto_parallel_context(device_num=8, global_rank=0)
  143. strategy1 = ((2, 2), (2, 2))
  144. strategy2 = ((4, 2), (2, ))
  145. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  146. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  147. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  148. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  149. b = Tensor(np.ones([64]), dtype=ms.float32)
  150. _executor.compile(net, x, y, b)
  151. def test_matmul_add_broadcast2():
  152. class Net(nn.Cell):
  153. def __init__(self, strategy1, strategy2):
  154. super().__init__()
  155. self.matmul = P.MatMul().set_strategy(strategy1)
  156. self.add = P.TensorAdd().set_strategy(strategy2)
  157. def construct(self, x, y, b):
  158. out = self.matmul(x, y)
  159. out = self.add(out, b)
  160. return out
  161. context.set_auto_parallel_context(device_num=8, global_rank=0)
  162. strategy1 = ((2, 4), (4, 1))
  163. strategy2 = ((4, 1), (1, 2))
  164. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  165. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  166. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  167. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  168. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  169. _executor.compile(net, x, y, b)
  170. def test_matmul_sub_broadcast():
  171. class Net(nn.Cell):
  172. def __init__(self, strategy1, strategy2):
  173. super().__init__()
  174. self.matmul = P.MatMul().set_strategy(strategy1)
  175. self.sub = P.Sub().set_strategy(strategy2)
  176. def construct(self, x, y, b):
  177. out = self.matmul(x, y)
  178. out = self.sub(out, b)
  179. return out
  180. context.set_auto_parallel_context(device_num=8, global_rank=0)
  181. strategy1 = ((2, 2), (2, 2))
  182. strategy2 = ((4, 2), (2, ))
  183. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  184. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  185. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  186. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  187. b = Tensor(np.ones([64]), dtype=ms.float32)
  188. _executor.compile(net, x, y, b)
  189. def test_matmul_sub_broadcast2():
  190. class Net(nn.Cell):
  191. def __init__(self, strategy1, strategy2):
  192. super().__init__()
  193. self.matmul = P.MatMul().set_strategy(strategy1)
  194. self.sub = P.Sub().set_strategy(strategy2)
  195. def construct(self, x, y, b):
  196. out = self.matmul(x, y)
  197. out = self.sub(out, b)
  198. return out
  199. context.set_auto_parallel_context(device_num=8, global_rank=0)
  200. strategy1 = ((2, 4), (4, 1))
  201. strategy2 = ((4, 1), (1, 2))
  202. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  203. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  204. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  205. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  206. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  207. _executor.compile(net, x, y, b)
  208. def test_matmul_mul_broadcast():
  209. class Net(nn.Cell):
  210. def __init__(self, strategy1, strategy2):
  211. super().__init__()
  212. self.matmul = P.MatMul().set_strategy(strategy1)
  213. self.mul = P.Mul().set_strategy(strategy2)
  214. def construct(self, x, y, b):
  215. out = self.matmul(x, y)
  216. out = self.mul(out, b)
  217. return out
  218. context.set_auto_parallel_context(device_num=8, global_rank=0)
  219. strategy1 = ((2, 2), (2, 2))
  220. strategy2 = ((4, 2), (2, ))
  221. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  222. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  223. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  224. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  225. b = Tensor(np.ones([64]), dtype=ms.float32)
  226. _executor.compile(net, x, y, b)
  227. def test_matmul_mul_broadcast2():
  228. class Net(nn.Cell):
  229. def __init__(self, strategy1, strategy2):
  230. super().__init__()
  231. self.matmul = P.MatMul().set_strategy(strategy1)
  232. self.mul = P.Mul().set_strategy(strategy2)
  233. def construct(self, x, y, b):
  234. out = self.matmul(x, y)
  235. out = self.mul(out, b)
  236. return out
  237. context.set_auto_parallel_context(device_num=8, global_rank=0)
  238. strategy1 = ((2, 4), (4, 1))
  239. strategy2 = ((4, 1), (1, 2))
  240. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  241. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  242. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  243. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  244. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  245. _executor.compile(net, x, y, b)
  246. def test_matmul_div_broadcast():
  247. class Net(nn.Cell):
  248. def __init__(self, strategy1, strategy2):
  249. super().__init__()
  250. self.matmul = P.MatMul().set_strategy(strategy1)
  251. self.div = P.Div().set_strategy(strategy2)
  252. def construct(self, x, y, b):
  253. out = self.matmul(x, y)
  254. out = self.div(out, b)
  255. return out
  256. context.set_auto_parallel_context(device_num=8, global_rank=0)
  257. strategy1 = ((2, 2), (2, 2))
  258. strategy2 = ((4, 2), (2, ))
  259. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  260. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  261. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  262. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  263. b = Tensor(np.ones([64]), dtype=ms.float32)
  264. _executor.compile(net, x, y, b)
  265. def test_matmul_div_broadcast2():
  266. class Net(nn.Cell):
  267. def __init__(self, strategy1, strategy2):
  268. super().__init__()
  269. self.matmul = P.MatMul().set_strategy(strategy1)
  270. self.div = P.Div().set_strategy(strategy2)
  271. def construct(self, x, y, b):
  272. out = self.matmul(x, y)
  273. out = self.div(out, b)
  274. return out
  275. context.set_auto_parallel_context(device_num=8, global_rank=0)
  276. strategy1 = ((2, 4), (4, 1))
  277. strategy2 = ((4, 1), (1, 2))
  278. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  279. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  280. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  281. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  282. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  283. _executor.compile(net, x, y, b)
  284. def test_matmul_greater_broadcast():
  285. class Net(nn.Cell):
  286. def __init__(self, strategy1, strategy2):
  287. super().__init__()
  288. self.matmul = P.MatMul().set_strategy(strategy1)
  289. self.greater = P.Greater().set_strategy(strategy2)
  290. def construct(self, x, y, b):
  291. out = self.matmul(x, y)
  292. out = self.greater(out, b)
  293. return out
  294. context.set_auto_parallel_context(device_num=8, global_rank=0)
  295. strategy1 = ((2, 2), (2, 2))
  296. strategy2 = ((4, 2), (2, ))
  297. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  298. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  299. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  300. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  301. b = Tensor(np.ones([64]), dtype=ms.float32)
  302. _executor.compile(net, x, y, b)
  303. def test_matmul_greater_broadcast2():
  304. class Net(nn.Cell):
  305. def __init__(self, strategy1, strategy2):
  306. super().__init__()
  307. self.matmul = P.MatMul().set_strategy(strategy1)
  308. self.greater = P.Greater().set_strategy(strategy2)
  309. def construct(self, x, y, b):
  310. out = self.matmul(x, y)
  311. out = self.greater(out, b)
  312. return out
  313. context.set_auto_parallel_context(device_num=8, global_rank=0)
  314. strategy1 = ((2, 4), (4, 1))
  315. strategy2 = ((4, 1), (1, 2))
  316. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  317. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  318. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  319. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  320. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  321. _executor.compile(net, x, y, b)
  322. def test_matmul_floordiv():
  323. class Net(nn.Cell):
  324. def __init__(self, strategy1, strategy2):
  325. super().__init__()
  326. self.matmul = P.MatMul().set_strategy(strategy1)
  327. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  328. def construct(self, x, y, b):
  329. out = self.matmul(x, y)
  330. out = self.floordiv(out, b)
  331. return out
  332. context.set_auto_parallel_context(device_num=8, global_rank=0)
  333. strategy1 = ((2, 2), (2, 2))
  334. strategy2 = ((4, 2), (4, 2))
  335. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  336. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  337. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  338. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  339. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  340. _executor.compile(net, x, y, b)
  341. def test_matmul_floordiv_broadcast():
  342. class Net(nn.Cell):
  343. def __init__(self, strategy1, strategy2):
  344. super().__init__()
  345. self.matmul = P.MatMul().set_strategy(strategy1)
  346. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  347. def construct(self, x, y, b):
  348. out = self.matmul(x, y)
  349. out = self.floordiv(out, b)
  350. return out
  351. context.set_auto_parallel_context(device_num=8, global_rank=0)
  352. strategy1 = ((2, 2), (2, 2))
  353. strategy2 = ((4, 2), (2, ))
  354. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  355. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  356. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  357. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  358. b = Tensor(np.ones([64]), dtype=ms.float32)
  359. _executor.compile(net, x, y, b)
  360. def test_matmul_floordiv_broadcast2():
  361. class Net(nn.Cell):
  362. def __init__(self, strategy1, strategy2):
  363. super().__init__()
  364. self.matmul = P.MatMul().set_strategy(strategy1)
  365. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  366. def construct(self, x, y, b):
  367. out = self.matmul(x, y)
  368. out = self.floordiv(out, b)
  369. return out
  370. context.set_auto_parallel_context(device_num=8, global_rank=0)
  371. strategy1 = ((2, 4), (4, 1))
  372. strategy2 = ((4, 1), (1, 2))
  373. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  374. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  375. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  376. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  377. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  378. _executor.compile(net, x, y, b)