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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. import mindspore as ms
  16. import mindspore.nn as nn
  17. from mindspore import Parameter, Tensor, context
  18. from mindspore.common.api import _executor
  19. from mindspore.ops import composite as C
  20. from mindspore.ops import operations as P
  21. from tests.ut.python.ops.test_math_ops import VirtualLoss
  22. grad_all = C.GradOperation(get_all=True)
  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 grad_all(self.network)(x, y, b)
  37. def compile_net(net, x, y, b):
  38. net.set_auto_parallel()
  39. _executor.compile(net, x, y, b)
  40. def test_matmul_sub():
  41. class Net(nn.Cell):
  42. def __init__(self, strategy1, strategy2):
  43. super().__init__()
  44. self.matmul = P.MatMul().shard(strategy1)
  45. self.sub = P.Sub().shard(strategy2)
  46. def construct(self, x, y, b):
  47. out = self.matmul(x, y)
  48. out = self.sub(out, b)
  49. return out
  50. context.set_auto_parallel_context(device_num=8, global_rank=0)
  51. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  52. strategy1 = ((2, 2), (2, 2))
  53. strategy2 = ((4, 2), (4, 2))
  54. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  55. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  56. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  57. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  58. compile_net(net, x, y, b)
  59. def test_matmul_add():
  60. class Net(nn.Cell):
  61. def __init__(self, strategy1, strategy2):
  62. super().__init__()
  63. self.matmul = P.MatMul().shard(strategy1)
  64. self.add = P.TensorAdd().shard(strategy2)
  65. def construct(self, x, y, b):
  66. out = self.matmul(x, y)
  67. out = self.add(out, b)
  68. return out
  69. context.set_auto_parallel_context(device_num=8, global_rank=0)
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  71. strategy1 = ((2, 2), (2, 2))
  72. strategy2 = ((4, 2), (4, 2))
  73. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  74. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  75. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  76. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  77. compile_net(net, x, y, b)
  78. def test_matmul_mul():
  79. class Net(nn.Cell):
  80. def __init__(self, strategy1, strategy2):
  81. super().__init__()
  82. self.matmul = P.MatMul().shard(strategy1)
  83. self.mul = P.Mul().shard(strategy2)
  84. def construct(self, x, y, b):
  85. out = self.matmul(x, y)
  86. out = self.mul(out, b)
  87. return out
  88. context.set_auto_parallel_context(device_num=8, global_rank=0)
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  90. strategy1 = ((2, 2), (2, 2))
  91. strategy2 = ((4, 2), (4, 2))
  92. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  93. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  94. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  95. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  96. compile_net(net, x, y, b)
  97. def test_matmul_mod():
  98. class Net(nn.Cell):
  99. def __init__(self, strategy1, strategy2):
  100. super().__init__()
  101. self.matmul = P.MatMul().shard(strategy1)
  102. self.mod = P.Mod().shard(strategy2)
  103. def construct(self, x, y, b):
  104. out = self.matmul(x, y)
  105. out = self.mod(out, b)
  106. return out
  107. context.set_auto_parallel_context(device_num=8, global_rank=0)
  108. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  109. strategy1 = ((2, 2), (2, 2))
  110. strategy2 = ((4, 2), (4, 2))
  111. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  112. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  113. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  114. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  115. compile_net(net, x, y, b)
  116. def test_matmul_floormod():
  117. class Net(nn.Cell):
  118. def __init__(self, strategy1, strategy2):
  119. super().__init__()
  120. self.matmul = P.MatMul().shard(strategy1)
  121. self.floormod = P.FloorMod().shard(strategy2)
  122. def construct(self, x, y, b):
  123. out = self.matmul(x, y)
  124. out = self.floormod(out, b)
  125. return out
  126. context.set_auto_parallel_context(device_num=8, global_rank=0)
  127. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  128. strategy1 = ((2, 2), (2, 2))
  129. strategy2 = ((4, 2), (4, 2))
  130. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  131. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  132. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  133. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  134. compile_net(net, x, y, b)
  135. def test_matmul_atan2():
  136. class Net(nn.Cell):
  137. def __init__(self, strategy1, strategy2):
  138. super().__init__()
  139. self.matmul = P.MatMul().shard(strategy1)
  140. self.atan2 = P.Atan2().shard(strategy2)
  141. def construct(self, x, y, b):
  142. out = self.matmul(x, y)
  143. out = self.atan2(out, b)
  144. return out
  145. context.set_auto_parallel_context(device_num=8, global_rank=0)
  146. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  147. strategy1 = ((2, 2), (2, 2))
  148. strategy2 = ((4, 2), (4, 2))
  149. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  150. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  151. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  152. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  153. compile_net(net, x, y, b)
  154. def test_matmul_divNoNan():
  155. class Net(nn.Cell):
  156. def __init__(self, strategy1, strategy2):
  157. super().__init__()
  158. self.matmul = P.MatMul().shard(strategy1)
  159. self.divNoNan = P.DivNoNan().shard(strategy2)
  160. def construct(self, x, y, b):
  161. out = self.matmul(x, y)
  162. out = self.divNoNan(out, b)
  163. return out
  164. context.set_auto_parallel_context(device_num=8, global_rank=0)
  165. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  166. strategy1 = ((2, 2), (2, 2))
  167. strategy2 = ((4, 2), (4, 2))
  168. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  169. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  170. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  171. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  172. compile_net(net, x, y, b)
  173. def test_matmul_logicaland():
  174. class Net(nn.Cell):
  175. def __init__(self, strategy1, strategy2):
  176. super().__init__()
  177. self.matmul = P.MatMul().shard(strategy1)
  178. self.equal = P.Equal().shard(strategy2)
  179. self.notequal = P.NotEqual().shard(strategy2)
  180. self.logical = P.LogicalAnd().shard(strategy2)
  181. def construct(self, x, y, b):
  182. out = self.matmul(x, y)
  183. out1 = self.equal(out, b)
  184. out = self.matmul(x, y)
  185. out2 = self.notequal(out, b)
  186. out = self.logical(out1, out2)
  187. return out
  188. context.set_auto_parallel_context(device_num=8, global_rank=0)
  189. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  190. strategy1 = ((2, 2), (2, 2))
  191. strategy2 = ((4, 2), (4, 2))
  192. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  193. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  194. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  195. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  196. compile_net(net, x, y, b)
  197. def test_matmul_logicalor():
  198. class Net(nn.Cell):
  199. def __init__(self, strategy1, strategy2):
  200. super().__init__()
  201. self.matmul = P.MatMul().shard(strategy1)
  202. self.equal = P.Equal().shard(strategy2)
  203. self.notequal = P.NotEqual().shard(strategy2)
  204. self.logical = P.LogicalOr().shard(strategy2)
  205. def construct(self, x, y, b):
  206. out = self.matmul(x, y)
  207. out1 = self.equal(out, b)
  208. out = self.matmul(x, y)
  209. out2 = self.notequal(out, b)
  210. out = self.logical(out1, out2)
  211. return out
  212. context.set_auto_parallel_context(device_num=8, global_rank=0)
  213. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  214. strategy1 = ((2, 2), (2, 2))
  215. strategy2 = ((4, 2), (4, 2))
  216. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  217. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  218. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  219. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  220. compile_net(net, x, y, b)
  221. def test_matmul_div():
  222. class Net(nn.Cell):
  223. def __init__(self, strategy1, strategy2):
  224. super().__init__()
  225. self.matmul = P.MatMul().shard(strategy1)
  226. self.div = P.Div().shard(strategy2)
  227. def construct(self, x, y, b):
  228. out = self.matmul(x, y)
  229. out = self.div(out, b)
  230. return out
  231. context.set_auto_parallel_context(device_num=8, global_rank=0)
  232. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  233. strategy1 = ((2, 2), (2, 2))
  234. strategy2 = ((4, 2), (4, 2))
  235. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  236. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  237. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  238. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  239. compile_net(net, x, y, b)
  240. def test_matmul_add_broadcast():
  241. class Net(nn.Cell):
  242. def __init__(self, strategy1, strategy2):
  243. super().__init__()
  244. self.matmul = P.MatMul().shard(strategy1)
  245. self.add = P.TensorAdd().shard(strategy2)
  246. def construct(self, x, y, b):
  247. out = self.matmul(x, y)
  248. out = self.add(out, b)
  249. return out
  250. context.set_auto_parallel_context(device_num=8, global_rank=0)
  251. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  252. strategy1 = ((2, 2), (2, 2))
  253. strategy2 = ((4, 2), (2,))
  254. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  255. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  256. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  257. b = Tensor(np.ones([64]), dtype=ms.float32)
  258. compile_net(net, x, y, b)
  259. def test_matmul_add_broadcast2():
  260. class Net(nn.Cell):
  261. def __init__(self, strategy1, strategy2):
  262. super().__init__()
  263. self.matmul = P.MatMul().shard(strategy1)
  264. self.add = P.TensorAdd().shard(strategy2)
  265. def construct(self, x, y, b):
  266. out = self.matmul(x, y)
  267. out = self.add(out, b)
  268. return out
  269. context.set_auto_parallel_context(device_num=8, global_rank=0)
  270. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  271. strategy1 = ((2, 4), (4, 1))
  272. strategy2 = ((4, 1), (1, 2))
  273. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  274. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  275. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  276. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  277. compile_net(net, x, y, b)
  278. def test_matmul_sub_broadcast():
  279. class Net(nn.Cell):
  280. def __init__(self, strategy1, strategy2):
  281. super().__init__()
  282. self.matmul = P.MatMul().shard(strategy1)
  283. self.sub = P.Sub().shard(strategy2)
  284. def construct(self, x, y, b):
  285. out = self.matmul(x, y)
  286. out = self.sub(out, b)
  287. return out
  288. context.set_auto_parallel_context(device_num=8, global_rank=0)
  289. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  290. strategy1 = ((2, 2), (2, 2))
  291. strategy2 = ((4, 2), (2,))
  292. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  293. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  294. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  295. b = Tensor(np.ones([64]), dtype=ms.float32)
  296. compile_net(net, x, y, b)
  297. def test_matmul_sub_broadcast2():
  298. class Net(nn.Cell):
  299. def __init__(self, strategy1, strategy2):
  300. super().__init__()
  301. self.matmul = P.MatMul().shard(strategy1)
  302. self.sub = P.Sub().shard(strategy2)
  303. def construct(self, x, y, b):
  304. out = self.matmul(x, y)
  305. out = self.sub(out, b)
  306. return out
  307. context.set_auto_parallel_context(device_num=8, global_rank=0)
  308. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  309. strategy1 = ((2, 4), (4, 1))
  310. strategy2 = ((4, 1), (1, 2))
  311. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  312. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  313. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  314. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  315. compile_net(net, x, y, b)
  316. def test_matmul_mul_broadcast():
  317. class Net(nn.Cell):
  318. def __init__(self, strategy1, strategy2):
  319. super().__init__()
  320. self.matmul = P.MatMul().shard(strategy1)
  321. self.mul = P.Mul().shard(strategy2)
  322. def construct(self, x, y, b):
  323. out = self.matmul(x, y)
  324. out = self.mul(out, b)
  325. return out
  326. context.set_auto_parallel_context(device_num=8, global_rank=0)
  327. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  328. strategy1 = ((2, 2), (2, 2))
  329. strategy2 = ((4, 2), (2,))
  330. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  331. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  332. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  333. b = Tensor(np.ones([64]), dtype=ms.float32)
  334. compile_net(net, x, y, b)
  335. def test_matmul_mul_broadcast2():
  336. class Net(nn.Cell):
  337. def __init__(self, strategy1, strategy2):
  338. super().__init__()
  339. self.matmul = P.MatMul().shard(strategy1)
  340. self.mul = P.Mul().shard(strategy2)
  341. def construct(self, x, y, b):
  342. out = self.matmul(x, y)
  343. out = self.mul(out, b)
  344. return out
  345. context.set_auto_parallel_context(device_num=8, global_rank=0)
  346. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  347. strategy1 = ((2, 4), (4, 1))
  348. strategy2 = ((4, 1), (1, 2))
  349. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  350. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  351. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  352. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  353. compile_net(net, x, y, b)
  354. def test_matmul_div_broadcast():
  355. class Net(nn.Cell):
  356. def __init__(self, strategy1, strategy2):
  357. super().__init__()
  358. self.matmul = P.MatMul().shard(strategy1)
  359. self.div = P.Div().shard(strategy2)
  360. def construct(self, x, y, b):
  361. out = self.matmul(x, y)
  362. out = self.div(out, b)
  363. return out
  364. context.set_auto_parallel_context(device_num=8, global_rank=0)
  365. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  366. strategy1 = ((2, 2), (2, 2))
  367. strategy2 = ((4, 2), (2,))
  368. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  369. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  370. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  371. b = Tensor(np.ones([64]), dtype=ms.float32)
  372. compile_net(net, x, y, b)
  373. def test_matmul_div_broadcast2():
  374. class Net(nn.Cell):
  375. def __init__(self, strategy1, strategy2):
  376. super().__init__()
  377. self.matmul = P.MatMul().shard(strategy1)
  378. self.div = P.Div().shard(strategy2)
  379. def construct(self, x, y, b):
  380. out = self.matmul(x, y)
  381. out = self.div(out, b)
  382. return out
  383. context.set_auto_parallel_context(device_num=8, global_rank=0)
  384. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  385. strategy1 = ((2, 4), (4, 1))
  386. strategy2 = ((4, 1), (1, 2))
  387. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  388. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  389. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  390. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  391. compile_net(net, x, y, b)
  392. def test_matmul_greater_broadcast():
  393. class Net(nn.Cell):
  394. def __init__(self, strategy1, strategy2):
  395. super().__init__()
  396. self.matmul = P.MatMul().shard(strategy1)
  397. self.greater = P.Greater().shard(strategy2)
  398. def construct(self, x, y, b):
  399. out = self.matmul(x, y)
  400. out = self.greater(out, b)
  401. return out
  402. context.set_auto_parallel_context(device_num=8, global_rank=0)
  403. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  404. strategy1 = ((2, 2), (2, 2))
  405. strategy2 = ((4, 2), (2,))
  406. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  407. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  408. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  409. b = Tensor(np.ones([64]), dtype=ms.float32)
  410. compile_net(net, x, y, b)
  411. def test_matmul_greater_broadcast2():
  412. class Net(nn.Cell):
  413. def __init__(self, strategy1, strategy2):
  414. super().__init__()
  415. self.matmul = P.MatMul().shard(strategy1)
  416. self.greater = P.Greater().shard(strategy2)
  417. def construct(self, x, y, b):
  418. out = self.matmul(x, y)
  419. out = self.greater(out, b)
  420. return out
  421. context.set_auto_parallel_context(device_num=8, global_rank=0)
  422. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  423. strategy1 = ((2, 4), (4, 1))
  424. strategy2 = ((4, 1), (1, 2))
  425. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  426. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  427. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  428. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  429. compile_net(net, x, y, b)
  430. def test_matmul_floordiv():
  431. class Net(nn.Cell):
  432. def __init__(self, strategy1, strategy2):
  433. super().__init__()
  434. self.matmul = P.MatMul().shard(strategy1)
  435. self.floordiv = P.FloorDiv().shard(strategy2)
  436. def construct(self, x, y, b):
  437. out = self.matmul(x, y)
  438. out = self.floordiv(out, b)
  439. return out
  440. context.set_auto_parallel_context(device_num=8, global_rank=0)
  441. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  442. strategy1 = ((2, 2), (2, 2))
  443. strategy2 = ((4, 2), (4, 2))
  444. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  445. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  446. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  447. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  448. compile_net(net, x, y, b)
  449. def test_matmul_floordiv_broadcast():
  450. class Net(nn.Cell):
  451. def __init__(self, strategy1, strategy2):
  452. super().__init__()
  453. self.matmul = P.MatMul().shard(strategy1)
  454. self.floordiv = P.FloorDiv().shard(strategy2)
  455. def construct(self, x, y, b):
  456. out = self.matmul(x, y)
  457. out = self.floordiv(out, b)
  458. return out
  459. context.set_auto_parallel_context(device_num=8, global_rank=0)
  460. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  461. strategy1 = ((2, 2), (2, 2))
  462. strategy2 = ((4, 2), (2,))
  463. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  464. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  465. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  466. b = Tensor(np.ones([64]), dtype=ms.float32)
  467. compile_net(net, x, y, b)
  468. def test_matmul_floordiv_broadcast2():
  469. class Net(nn.Cell):
  470. def __init__(self, strategy1, strategy2):
  471. super().__init__()
  472. self.matmul = P.MatMul().shard(strategy1)
  473. self.floordiv = P.FloorDiv().shard(strategy2)
  474. def construct(self, x, y, b):
  475. out = self.matmul(x, y)
  476. out = self.floordiv(out, b)
  477. return out
  478. context.set_auto_parallel_context(device_num=8, global_rank=0)
  479. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  480. strategy1 = ((2, 4), (4, 1))
  481. strategy2 = ((4, 1), (1, 2))
  482. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  483. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  484. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  485. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  486. compile_net(net, x, y, b)
  487. def test_assign_sub():
  488. class Net(nn.Cell):
  489. def __init__(self):
  490. super().__init__()
  491. self.assign_sub = P.AssignSub()
  492. self.mul = P.Mul()
  493. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  494. 0.5, dtype=np.float32)),
  495. name="mul_weight")
  496. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  497. 1.1, dtype=np.float32)),
  498. name="assignsub_weight")
  499. def construct(self, x):
  500. out = self.mul(x, self.mul_weight)
  501. out = self.assign_sub(self.assignsub_weight, out)
  502. return out
  503. class SubNetWithLoss(nn.Cell):
  504. def __init__(self, network):
  505. super(SubNetWithLoss, self).__init__()
  506. self.loss = VirtualLoss()
  507. self.network = network
  508. def construct(self, x):
  509. predict = self.network(x,)
  510. return self.loss(predict)
  511. class SubGradWrap(nn.Cell):
  512. def __init__(self, network):
  513. super(SubGradWrap, self).__init__()
  514. self.network = network
  515. def construct(self, x):
  516. return grad_all(self.network)(x)
  517. def compile_sub_net(net, x):
  518. net.set_auto_parallel()
  519. _executor.compile(net, x)
  520. context.set_auto_parallel_context(device_num=64, global_rank=15)
  521. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  522. net = SubGradWrap(SubNetWithLoss(Net()))
  523. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  524. compile_sub_net(net, x)
  525. def test_assign_add():
  526. class Net(nn.Cell):
  527. def __init__(self):
  528. super().__init__()
  529. self.assign_sub = P.AssignAdd()
  530. self.mul = P.Mul()
  531. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  532. 0.5, dtype=np.float32)),
  533. name="mul_weight")
  534. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  535. 1.1, dtype=np.float32)),
  536. name="assignsub_weight")
  537. def construct(self, x):
  538. out = self.mul(x, self.mul_weight)
  539. out = self.assign_sub(self.assignsub_weight, out)
  540. return out
  541. class SubNetWithLoss(nn.Cell):
  542. def __init__(self, network):
  543. super(SubNetWithLoss, self).__init__()
  544. self.loss = VirtualLoss()
  545. self.network = network
  546. def construct(self, x):
  547. predict = self.network(x,)
  548. return self.loss(predict)
  549. class SubGradWrap(nn.Cell):
  550. def __init__(self, network):
  551. super(SubGradWrap, self).__init__()
  552. self.network = network
  553. def construct(self, x):
  554. return grad_all(self.network)(x)
  555. def compile_sub_net(net, x):
  556. net.set_auto_parallel()
  557. _executor.compile(net, x)
  558. context.set_auto_parallel_context(device_num=64, global_rank=15)
  559. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  560. net = SubGradWrap(SubNetWithLoss(Net()))
  561. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  562. compile_sub_net(net, x)
  563. def test_assign():
  564. class Net(nn.Cell):
  565. def __init__(self):
  566. super().__init__()
  567. self.assign_sub = P.Assign()
  568. self.mul = P.Mul()
  569. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  570. 0.5, dtype=np.float32)),
  571. name="mul_weight")
  572. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  573. 1.1, dtype=np.float32)),
  574. name="assignsub_weight")
  575. def construct(self, x):
  576. out = self.mul(x, self.mul_weight)
  577. out = self.assign_sub(self.assignsub_weight, out)
  578. return out
  579. class SubNetWithLoss(nn.Cell):
  580. def __init__(self, network):
  581. super(SubNetWithLoss, self).__init__()
  582. self.loss = VirtualLoss()
  583. self.network = network
  584. def construct(self, x):
  585. predict = self.network(x,)
  586. return self.loss(predict)
  587. class SubGradWrap(nn.Cell):
  588. def __init__(self, network):
  589. super(SubGradWrap, self).__init__()
  590. self.network = network
  591. def construct(self, x):
  592. return grad_all(self.network)(x)
  593. def compile_sub_net(net, x):
  594. net.set_auto_parallel()
  595. _executor.compile(net, x)
  596. context.set_auto_parallel_context(device_num=64, global_rank=15)
  597. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  598. net = SubGradWrap(SubNetWithLoss(Net()))
  599. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  600. compile_sub_net(net, x)