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 31 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 _cell_graph_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. net.set_train()
  40. _cell_graph_executor.compile(net, x, y, b)
  41. def test_matmul_sub():
  42. """
  43. Feature: distribute operator sub in auto parallel.
  44. Description: matmul-sub net with strategy in semi auto parallel.
  45. Expectation: compile done without error.
  46. """
  47. class Net(nn.Cell):
  48. def __init__(self, strategy1, strategy2):
  49. super().__init__()
  50. self.matmul = P.MatMul().shard(strategy1)
  51. self.sub = P.Sub().shard(strategy2)
  52. def construct(self, x, y, b):
  53. out = self.matmul(x, y)
  54. out = self.sub(out, b)
  55. return out
  56. context.set_auto_parallel_context(device_num=8, global_rank=0)
  57. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  58. strategy1 = ((2, 2), (2, 2))
  59. strategy2 = ((4, 2), (4, 2))
  60. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  61. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  62. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  63. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  64. compile_net(net, x, y, b)
  65. def test_matmul_add():
  66. """
  67. Feature: distribute operator sub in auto parallel.
  68. Description: matmul-add net with strategy in semi auto parallel.
  69. Expectation: compile done without error.
  70. """
  71. class Net(nn.Cell):
  72. def __init__(self, strategy1, strategy2):
  73. super().__init__()
  74. self.matmul = P.MatMul().shard(strategy1)
  75. self.add = P.Add().shard(strategy2)
  76. def construct(self, x, y, b):
  77. out = self.matmul(x, y)
  78. out = self.add(out, b)
  79. return out
  80. context.set_auto_parallel_context(device_num=8, global_rank=0)
  81. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  82. strategy1 = ((2, 2), (2, 2))
  83. strategy2 = ((4, 2), (4, 2))
  84. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  85. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  86. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  87. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  88. compile_net(net, x, y, b)
  89. def test_matmul_mul():
  90. """
  91. Feature: distribute operator sub in auto parallel.
  92. Description: matmul-mul net with strategy in semi auto parallel.
  93. Expectation: compile done without error.
  94. """
  95. class Net(nn.Cell):
  96. def __init__(self, strategy1, strategy2):
  97. super().__init__()
  98. self.matmul = P.MatMul().shard(strategy1)
  99. self.mul = P.Mul().shard(strategy2)
  100. def construct(self, x, y, b):
  101. out = self.matmul(x, y)
  102. out = self.mul(out, b)
  103. return out
  104. context.set_auto_parallel_context(device_num=8, global_rank=0)
  105. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  106. strategy1 = ((2, 2), (2, 2))
  107. strategy2 = ((4, 2), (4, 2))
  108. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  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. compile_net(net, x, y, b)
  113. def test_matmul_mod():
  114. """
  115. Feature: distribute operator sub in auto parallel.
  116. Description: matmul-mod net with strategy in semi auto parallel.
  117. Expectation: compile done without error.
  118. """
  119. class Net(nn.Cell):
  120. def __init__(self, strategy1, strategy2):
  121. super().__init__()
  122. self.matmul = P.MatMul().shard(strategy1)
  123. self.mod = P.Mod().shard(strategy2)
  124. def construct(self, x, y, b):
  125. out = self.matmul(x, y)
  126. out = self.mod(out, b)
  127. return out
  128. context.set_auto_parallel_context(device_num=8, global_rank=0)
  129. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  130. strategy1 = ((2, 2), (2, 2))
  131. strategy2 = ((4, 2), (4, 2))
  132. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  133. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  134. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  135. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  136. compile_net(net, x, y, b)
  137. def test_matmul_floormod():
  138. """
  139. Feature: distribute operator sub in auto parallel.
  140. Description: matmul-floormod net with strategy in semi auto parallel.
  141. Expectation: compile done without error.
  142. """
  143. class Net(nn.Cell):
  144. def __init__(self, strategy1, strategy2):
  145. super().__init__()
  146. self.matmul = P.MatMul().shard(strategy1)
  147. self.floormod = P.FloorMod().shard(strategy2)
  148. def construct(self, x, y, b):
  149. out = self.matmul(x, y)
  150. out = self.floormod(out, b)
  151. return out
  152. context.set_auto_parallel_context(device_num=8, global_rank=0)
  153. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  154. strategy1 = ((2, 2), (2, 2))
  155. strategy2 = ((4, 2), (4, 2))
  156. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  157. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  158. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  159. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  160. compile_net(net, x, y, b)
  161. def test_matmul_atan2():
  162. """
  163. Feature: distribute operator sub in auto parallel.
  164. Description: matmul-atan2 net with strategy in semi auto parallel.
  165. Expectation: compile done without error.
  166. """
  167. class Net(nn.Cell):
  168. def __init__(self, strategy1, strategy2):
  169. super().__init__()
  170. self.matmul = P.MatMul().shard(strategy1)
  171. self.atan2 = P.Atan2().shard(strategy2)
  172. def construct(self, x, y, b):
  173. out = self.matmul(x, y)
  174. out = self.atan2(out, b)
  175. return out
  176. context.set_auto_parallel_context(device_num=8, global_rank=0)
  177. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  178. strategy1 = ((2, 2), (2, 2))
  179. strategy2 = ((4, 2), (4, 2))
  180. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  181. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  182. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  183. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  184. compile_net(net, x, y, b)
  185. def test_matmul_divNoNan():
  186. """
  187. Feature: distribute operator sub in auto parallel.
  188. Description: matmul-divNoNan net with strategy in semi auto parallel.
  189. Expectation: compile done without error.
  190. """
  191. class Net(nn.Cell):
  192. def __init__(self, strategy1, strategy2):
  193. super().__init__()
  194. self.matmul = P.MatMul().shard(strategy1)
  195. self.divNoNan = P.DivNoNan().shard(strategy2)
  196. def construct(self, x, y, b):
  197. out = self.matmul(x, y)
  198. out = self.divNoNan(out, b)
  199. return out
  200. context.set_auto_parallel_context(device_num=8, global_rank=0)
  201. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  202. strategy1 = ((2, 2), (2, 2))
  203. strategy2 = ((4, 2), (4, 2))
  204. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  205. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  206. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  207. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  208. compile_net(net, x, y, b)
  209. def test_matmul_logicaland():
  210. """
  211. Feature: distribute operator sub in auto parallel.
  212. Description: matmul-logical_and net with strategy in semi auto parallel.
  213. Expectation: compile done without error.
  214. """
  215. class Net(nn.Cell):
  216. def __init__(self, strategy1, strategy2):
  217. super().__init__()
  218. self.matmul = P.MatMul().shard(strategy1)
  219. self.equal = P.Equal().shard(strategy2)
  220. self.notequal = P.NotEqual().shard(strategy2)
  221. self.logical = P.LogicalAnd().shard(strategy2)
  222. def construct(self, x, y, b):
  223. out = self.matmul(x, y)
  224. out1 = self.equal(out, b)
  225. out = self.matmul(x, y)
  226. out2 = self.notequal(out, b)
  227. out = self.logical(out1, out2)
  228. return out
  229. context.set_auto_parallel_context(device_num=8, global_rank=0)
  230. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  231. strategy1 = ((2, 2), (2, 2))
  232. strategy2 = ((4, 2), (4, 2))
  233. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  234. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  235. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  236. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  237. compile_net(net, x, y, b)
  238. def test_matmul_logicalor():
  239. """
  240. Feature: distribute operator sub in auto parallel.
  241. Description: matmul-logical_or net with strategy in semi auto parallel.
  242. Expectation: compile done without error.
  243. """
  244. class Net(nn.Cell):
  245. def __init__(self, strategy1, strategy2):
  246. super().__init__()
  247. self.matmul = P.MatMul().shard(strategy1)
  248. self.equal = P.Equal().shard(strategy2)
  249. self.notequal = P.NotEqual().shard(strategy2)
  250. self.logical = P.LogicalOr().shard(strategy2)
  251. def construct(self, x, y, b):
  252. out = self.matmul(x, y)
  253. out1 = self.equal(out, b)
  254. out = self.matmul(x, y)
  255. out2 = self.notequal(out, b)
  256. out = self.logical(out1, out2)
  257. return out
  258. context.set_auto_parallel_context(device_num=8, global_rank=0)
  259. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  260. strategy1 = ((2, 2), (2, 2))
  261. strategy2 = ((4, 2), (4, 2))
  262. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  263. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  264. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  265. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  266. compile_net(net, x, y, b)
  267. def test_matmul_div():
  268. """
  269. Feature: distribute operator sub in auto parallel.
  270. Description: matmul-div net with strategy in semi auto parallel.
  271. Expectation: compile done without error.
  272. """
  273. class Net(nn.Cell):
  274. def __init__(self, strategy1, strategy2):
  275. super().__init__()
  276. self.matmul = P.MatMul().shard(strategy1)
  277. self.div = P.Div().shard(strategy2)
  278. def construct(self, x, y, b):
  279. out = self.matmul(x, y)
  280. out = self.div(out, b)
  281. return out
  282. context.set_auto_parallel_context(device_num=8, global_rank=0)
  283. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  284. strategy1 = ((2, 2), (2, 2))
  285. strategy2 = ((4, 2), (4, 2))
  286. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  287. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  288. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  289. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  290. compile_net(net, x, y, b)
  291. def test_matmul_add_broadcast():
  292. """
  293. Feature: distribute operator sub in auto parallel.
  294. Description: matmul-add broadcast net with strategy in semi auto parallel.
  295. Expectation: compile done without error.
  296. """
  297. class Net(nn.Cell):
  298. def __init__(self, strategy1, strategy2):
  299. super().__init__()
  300. self.matmul = P.MatMul().shard(strategy1)
  301. self.add = P.Add().shard(strategy2)
  302. def construct(self, x, y, b):
  303. out = self.matmul(x, y)
  304. out = self.add(out, b)
  305. return out
  306. context.set_auto_parallel_context(device_num=8, global_rank=0)
  307. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  308. strategy1 = ((2, 2), (2, 2))
  309. strategy2 = ((4, 2), (2,))
  310. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  311. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  312. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  313. b = Tensor(np.ones([64]), dtype=ms.float32)
  314. compile_net(net, x, y, b)
  315. def test_matmul_add_broadcast2():
  316. """
  317. Feature: distribute operator sub in auto parallel.
  318. Description: matmul-add broadcast net with strategy in semi auto parallel.
  319. Expectation: compile done without error.
  320. """
  321. class Net(nn.Cell):
  322. def __init__(self, strategy1, strategy2):
  323. super().__init__()
  324. self.matmul = P.MatMul().shard(strategy1)
  325. self.add = P.Add().shard(strategy2)
  326. def construct(self, x, y, b):
  327. out = self.matmul(x, y)
  328. out = self.add(out, b)
  329. return out
  330. context.set_auto_parallel_context(device_num=8, global_rank=0)
  331. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  332. strategy1 = ((2, 4), (4, 1))
  333. strategy2 = ((4, 1), (1, 2))
  334. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  335. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  336. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  337. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  338. compile_net(net, x, y, b)
  339. def test_matmul_sub_broadcast():
  340. """
  341. Feature: distribute operator sub in auto parallel.
  342. Description: matmul-sub broadcast net with strategy in semi auto parallel.
  343. Expectation: compile done without error.
  344. """
  345. class Net(nn.Cell):
  346. def __init__(self, strategy1, strategy2):
  347. super().__init__()
  348. self.matmul = P.MatMul().shard(strategy1)
  349. self.sub = P.Sub().shard(strategy2)
  350. def construct(self, x, y, b):
  351. out = self.matmul(x, y)
  352. out = self.sub(out, b)
  353. return out
  354. context.set_auto_parallel_context(device_num=8, global_rank=0)
  355. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  356. strategy1 = ((2, 2), (2, 2))
  357. strategy2 = ((4, 2), (2,))
  358. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  359. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  360. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  361. b = Tensor(np.ones([64]), dtype=ms.float32)
  362. compile_net(net, x, y, b)
  363. def test_matmul_sub_broadcast2():
  364. """
  365. Feature: distribute operator sub in auto parallel.
  366. Description: matmul-sub broadcast net with strategy in semi auto parallel.
  367. Expectation: compile done without error.
  368. """
  369. class Net(nn.Cell):
  370. def __init__(self, strategy1, strategy2):
  371. super().__init__()
  372. self.matmul = P.MatMul().shard(strategy1)
  373. self.sub = P.Sub().shard(strategy2)
  374. def construct(self, x, y, b):
  375. out = self.matmul(x, y)
  376. out = self.sub(out, b)
  377. return out
  378. context.set_auto_parallel_context(device_num=8, global_rank=0)
  379. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  380. strategy1 = ((2, 4), (4, 1))
  381. strategy2 = ((4, 1), (1, 2))
  382. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  383. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  384. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  385. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  386. compile_net(net, x, y, b)
  387. def test_matmul_mul_broadcast():
  388. """
  389. Feature: distribute operator sub in auto parallel.
  390. Description: matmul-mul broadcast net with strategy in semi auto parallel.
  391. Expectation: compile done without error.
  392. """
  393. class Net(nn.Cell):
  394. def __init__(self, strategy1, strategy2):
  395. super().__init__()
  396. self.matmul = P.MatMul().shard(strategy1)
  397. self.mul = P.Mul().shard(strategy2)
  398. def construct(self, x, y, b):
  399. out = self.matmul(x, y)
  400. out = self.mul(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_mul_broadcast2():
  412. """
  413. Feature: distribute operator sub in auto parallel.
  414. Description: matmul-mul broadcast net with strategy in semi auto parallel.
  415. Expectation: compile done without error.
  416. """
  417. class Net(nn.Cell):
  418. def __init__(self, strategy1, strategy2):
  419. super().__init__()
  420. self.matmul = P.MatMul().shard(strategy1)
  421. self.mul = P.Mul().shard(strategy2)
  422. def construct(self, x, y, b):
  423. out = self.matmul(x, y)
  424. out = self.mul(out, b)
  425. return out
  426. context.set_auto_parallel_context(device_num=8, global_rank=0)
  427. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  428. strategy1 = ((2, 4), (4, 1))
  429. strategy2 = ((4, 1), (1, 2))
  430. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  431. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  432. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  433. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  434. compile_net(net, x, y, b)
  435. def test_matmul_div_broadcast():
  436. """
  437. Feature: distribute operator sub in auto parallel.
  438. Description: matmul-div broadcast net with strategy in semi auto parallel.
  439. Expectation: compile done without error.
  440. """
  441. class Net(nn.Cell):
  442. def __init__(self, strategy1, strategy2):
  443. super().__init__()
  444. self.matmul = P.MatMul().shard(strategy1)
  445. self.div = P.Div().shard(strategy2)
  446. def construct(self, x, y, b):
  447. out = self.matmul(x, y)
  448. out = self.div(out, b)
  449. return out
  450. context.set_auto_parallel_context(device_num=8, global_rank=0)
  451. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  452. strategy1 = ((2, 2), (2, 2))
  453. strategy2 = ((4, 2), (2,))
  454. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  455. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  456. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  457. b = Tensor(np.ones([64]), dtype=ms.float32)
  458. compile_net(net, x, y, b)
  459. def test_matmul_div_broadcast2():
  460. """
  461. Feature: distribute operator sub in auto parallel.
  462. Description: matmul-div broadcast net with strategy in semi auto parallel.
  463. Expectation: compile done without error.
  464. """
  465. class Net(nn.Cell):
  466. def __init__(self, strategy1, strategy2):
  467. super().__init__()
  468. self.matmul = P.MatMul().shard(strategy1)
  469. self.div = P.Div().shard(strategy2)
  470. def construct(self, x, y, b):
  471. out = self.matmul(x, y)
  472. out = self.div(out, b)
  473. return out
  474. context.set_auto_parallel_context(device_num=8, global_rank=0)
  475. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  476. strategy1 = ((2, 4), (4, 1))
  477. strategy2 = ((4, 1), (1, 2))
  478. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  479. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  480. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  481. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  482. compile_net(net, x, y, b)
  483. def test_matmul_greater_broadcast():
  484. """
  485. Feature: distribute operator sub in auto parallel.
  486. Description: matmul-greater broadcast net with strategy in semi auto parallel.
  487. Expectation: compile done without error.
  488. """
  489. class Net(nn.Cell):
  490. def __init__(self, strategy1, strategy2):
  491. super().__init__()
  492. self.matmul = P.MatMul().shard(strategy1)
  493. self.greater = P.Greater().shard(strategy2)
  494. def construct(self, x, y, b):
  495. out = self.matmul(x, y)
  496. out = self.greater(out, b)
  497. return out
  498. context.set_auto_parallel_context(device_num=8, global_rank=0)
  499. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  500. strategy1 = ((2, 2), (2, 2))
  501. strategy2 = ((4, 2), (2,))
  502. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  503. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  504. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  505. b = Tensor(np.ones([64]), dtype=ms.float32)
  506. compile_net(net, x, y, b)
  507. def test_matmul_greater_broadcast2():
  508. """
  509. Feature: distribute operator sub in auto parallel.
  510. Description: matmul-greater broadcast net with strategy in semi auto parallel.
  511. Expectation: compile done without error.
  512. """
  513. class Net(nn.Cell):
  514. def __init__(self, strategy1, strategy2):
  515. super().__init__()
  516. self.matmul = P.MatMul().shard(strategy1)
  517. self.greater = P.Greater().shard(strategy2)
  518. def construct(self, x, y, b):
  519. out = self.matmul(x, y)
  520. out = self.greater(out, b)
  521. return out
  522. context.set_auto_parallel_context(device_num=8, global_rank=0)
  523. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  524. strategy1 = ((2, 4), (4, 1))
  525. strategy2 = ((4, 1), (1, 2))
  526. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  527. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  528. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  529. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  530. compile_net(net, x, y, b)
  531. def test_matmul_floordiv():
  532. """
  533. Feature: distribute operator sub in auto parallel.
  534. Description: matmul-floordiv net with strategy in semi auto parallel.
  535. Expectation: compile done without error.
  536. """
  537. class Net(nn.Cell):
  538. def __init__(self, strategy1, strategy2):
  539. super().__init__()
  540. self.matmul = P.MatMul().shard(strategy1)
  541. self.floordiv = P.FloorDiv().shard(strategy2)
  542. def construct(self, x, y, b):
  543. out = self.matmul(x, y)
  544. out = self.floordiv(out, b)
  545. return out
  546. context.set_auto_parallel_context(device_num=8, global_rank=0)
  547. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  548. strategy1 = ((2, 2), (2, 2))
  549. strategy2 = ((4, 2), (4, 2))
  550. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  551. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  552. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  553. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  554. compile_net(net, x, y, b)
  555. def test_matmul_floordiv_broadcast():
  556. """
  557. Feature: distribute operator sub in auto parallel.
  558. Description: matmul-floordiv broadcast net with strategy in semi auto parallel.
  559. Expectation: compile done without error.
  560. """
  561. class Net(nn.Cell):
  562. def __init__(self, strategy1, strategy2):
  563. super().__init__()
  564. self.matmul = P.MatMul().shard(strategy1)
  565. self.floordiv = P.FloorDiv().shard(strategy2)
  566. def construct(self, x, y, b):
  567. out = self.matmul(x, y)
  568. out = self.floordiv(out, b)
  569. return out
  570. context.set_auto_parallel_context(device_num=8, global_rank=0)
  571. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  572. strategy1 = ((2, 2), (2, 2))
  573. strategy2 = ((4, 2), (2,))
  574. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  575. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  576. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  577. b = Tensor(np.ones([64]), dtype=ms.float32)
  578. compile_net(net, x, y, b)
  579. def test_matmul_floordiv_broadcast2():
  580. """
  581. Feature: distribute operator sub in auto parallel.
  582. Description: matmul-floordiv broadcast net with strategy in semi auto parallel.
  583. Expectation: compile done without error.
  584. """
  585. class Net(nn.Cell):
  586. def __init__(self, strategy1, strategy2):
  587. super().__init__()
  588. self.matmul = P.MatMul().shard(strategy1)
  589. self.floordiv = P.FloorDiv().shard(strategy2)
  590. def construct(self, x, y, b):
  591. out = self.matmul(x, y)
  592. out = self.floordiv(out, b)
  593. return out
  594. context.set_auto_parallel_context(device_num=8, global_rank=0)
  595. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  596. strategy1 = ((2, 4), (4, 1))
  597. strategy2 = ((4, 1), (1, 2))
  598. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  599. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  600. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  601. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  602. compile_net(net, x, y, b)
  603. def test_assign_sub():
  604. """
  605. Feature: distribute operator sub in auto parallel.
  606. Description: mul-assign_sub net with strategy in semi auto parallel.
  607. Expectation: compile done without error.
  608. """
  609. class Net(nn.Cell):
  610. def __init__(self):
  611. super().__init__()
  612. self.assign_sub = P.AssignSub()
  613. self.mul = P.Mul()
  614. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  615. 0.5, dtype=np.float32)),
  616. name="mul_weight")
  617. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  618. 1.1, dtype=np.float32)),
  619. name="assignsub_weight")
  620. def construct(self, x):
  621. out = self.mul(x, self.mul_weight)
  622. out = self.assign_sub(self.assignsub_weight, out)
  623. return out
  624. class SubNetWithLoss(nn.Cell):
  625. def __init__(self, network):
  626. super(SubNetWithLoss, self).__init__()
  627. self.loss = VirtualLoss()
  628. self.network = network
  629. def construct(self, x):
  630. predict = self.network(x,)
  631. return self.loss(predict)
  632. class SubGradWrap(nn.Cell):
  633. def __init__(self, network):
  634. super(SubGradWrap, self).__init__()
  635. self.network = network
  636. def construct(self, x):
  637. return grad_all(self.network)(x)
  638. def compile_sub_net(net, x):
  639. net.set_auto_parallel()
  640. net.set_train()
  641. _cell_graph_executor.compile(net, x)
  642. context.set_auto_parallel_context(device_num=64, global_rank=15)
  643. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  644. net = SubGradWrap(SubNetWithLoss(Net()))
  645. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  646. compile_sub_net(net, x)
  647. def test_assign_add():
  648. """
  649. Feature: distribute operator sub in auto parallel.
  650. Description: mul-assign_add net with strategy in semi auto parallel.
  651. Expectation: compile done without error.
  652. """
  653. class Net(nn.Cell):
  654. def __init__(self):
  655. super().__init__()
  656. self.assign_sub = P.AssignAdd()
  657. self.mul = P.Mul()
  658. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  659. 0.5, dtype=np.float32)),
  660. name="mul_weight")
  661. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  662. 1.1, dtype=np.float32)),
  663. name="assignsub_weight")
  664. def construct(self, x):
  665. out = self.mul(x, self.mul_weight)
  666. out = self.assign_sub(self.assignsub_weight, out)
  667. return out
  668. class SubNetWithLoss(nn.Cell):
  669. def __init__(self, network):
  670. super(SubNetWithLoss, self).__init__()
  671. self.loss = VirtualLoss()
  672. self.network = network
  673. def construct(self, x):
  674. predict = self.network(x,)
  675. return self.loss(predict)
  676. class SubGradWrap(nn.Cell):
  677. def __init__(self, network):
  678. super(SubGradWrap, self).__init__()
  679. self.network = network
  680. def construct(self, x):
  681. return grad_all(self.network)(x)
  682. def compile_sub_net(net, x):
  683. net.set_auto_parallel()
  684. net.set_train()
  685. _cell_graph_executor.compile(net, x)
  686. context.set_auto_parallel_context(device_num=64, global_rank=15)
  687. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  688. net = SubGradWrap(SubNetWithLoss(Net()))
  689. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  690. compile_sub_net(net, x)
  691. def test_assign():
  692. """
  693. Feature: distribute operator sub in auto parallel.
  694. Description: mul-assign_sub net with strategy in semi auto parallel.
  695. Expectation: compile done without error.
  696. """
  697. class Net(nn.Cell):
  698. def __init__(self):
  699. super().__init__()
  700. self.assign_sub = P.Assign()
  701. self.mul = P.Mul()
  702. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  703. 0.5, dtype=np.float32)),
  704. name="mul_weight")
  705. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  706. 1.1, dtype=np.float32)),
  707. name="assignsub_weight")
  708. def construct(self, x):
  709. out = self.mul(x, self.mul_weight)
  710. out = self.assign_sub(self.assignsub_weight, out)
  711. return out
  712. class SubNetWithLoss(nn.Cell):
  713. def __init__(self, network):
  714. super(SubNetWithLoss, self).__init__()
  715. self.loss = VirtualLoss()
  716. self.network = network
  717. def construct(self, x):
  718. predict = self.network(x,)
  719. return self.loss(predict)
  720. class SubGradWrap(nn.Cell):
  721. def __init__(self, network):
  722. super(SubGradWrap, self).__init__()
  723. self.network = network
  724. def construct(self, x):
  725. return grad_all(self.network)(x)
  726. def compile_sub_net(net, x):
  727. net.set_auto_parallel()
  728. net.set_train()
  729. _cell_graph_executor.compile(net, x)
  730. context.set_auto_parallel_context(device_num=64, global_rank=15)
  731. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  732. net = SubGradWrap(SubNetWithLoss(Net()))
  733. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  734. compile_sub_net(net, x)