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 42 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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)
  735. def test_matmul_bitwise_and_broadcast():
  736. """
  737. Feature: distribute operator BitwiseAnd in auto parallel.
  738. Description: mul-BitwiseAnd net with strategy in semi auto parallel.
  739. Expectation: compile done without error.
  740. """
  741. class Net(nn.Cell):
  742. def __init__(self, strategy1, strategy2):
  743. super().__init__()
  744. self.bitwise_and = P.BitwiseAnd().shard(strategy1)
  745. self.matmul = P.MatMul().shard(strategy2)
  746. def construct(self, x, y, z):
  747. out = self.bitwise_and(x, y)
  748. out = self.matmul(out, z)
  749. return out
  750. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  751. strategy1 = ((2, 1), (1, 4))
  752. strategy2 = ((1, 4), (4, 2))
  753. net = Net(strategy1, strategy2)
  754. x = Tensor(np.ones([64, 1]), dtype=ms.int32)
  755. y = Tensor(np.ones([1, 64]), dtype=ms.int32)
  756. z = Tensor(np.ones([64, 32]), dtype=ms.int32)
  757. compile_net(net, x, y, z)
  758. def test_matmul_bitwise_or_broadcast():
  759. """
  760. Feature: distribute operator BitwiseOr in auto parallel.
  761. Description: mul-BitwiseOr net with strategy in semi auto parallel.
  762. Expectation: compile done without error.
  763. """
  764. class Net(nn.Cell):
  765. def __init__(self, strategy1, strategy2):
  766. super().__init__()
  767. self.bitwise_or = P.BitwiseOr().shard(strategy1)
  768. self.matmul = P.MatMul().shard(strategy2)
  769. def construct(self, x, y, z):
  770. out = self.bitwise_or(x, y)
  771. out = self.matmul(out, z)
  772. return out
  773. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  774. strategy1 = ((2, 1), (1, 4))
  775. strategy2 = ((1, 4), (4, 2))
  776. net = Net(strategy1, strategy2)
  777. x = Tensor(np.ones([64, 1]), dtype=ms.int32)
  778. y = Tensor(np.ones([1, 64]), dtype=ms.int32)
  779. z = Tensor(np.ones([64, 32]), dtype=ms.int32)
  780. compile_net(net, x, y, z)
  781. def test_matmul_bitwise_xor_broadcast():
  782. """
  783. Feature: distribute operator BitwiseXor in auto parallel.
  784. Description: mul-BitwiseXor net with strategy in semi auto parallel.
  785. Expectation: compile done without error.
  786. """
  787. class Net(nn.Cell):
  788. def __init__(self, strategy1, strategy2):
  789. super().__init__()
  790. self.bitwise_xor = P.BitwiseXor().shard(strategy1)
  791. self.matmul = P.MatMul().shard(strategy2)
  792. def construct(self, x, y, z):
  793. out = self.bitwise_xor(x, y)
  794. out = self.matmul(out, z)
  795. return out
  796. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  797. strategy1 = ((2, 1), (1, 4))
  798. strategy2 = ((1, 4), (4, 2))
  799. net = Net(strategy1, strategy2)
  800. x = Tensor(np.ones([64, 1]), dtype=ms.int32)
  801. y = Tensor(np.ones([1, 64]), dtype=ms.int32)
  802. z = Tensor(np.ones([64, 32]), dtype=ms.int32)
  803. compile_net(net, x, y, z)
  804. def test_matmul_mul_no_nan_broadcast():
  805. """
  806. Feature: distribute operator MulNoNan in auto parallel.
  807. Description: mul-MulNoNan net with strategy in semi auto parallel.
  808. Expectation: compile done without error.
  809. """
  810. class Net(nn.Cell):
  811. def __init__(self, strategy1, strategy2):
  812. super().__init__()
  813. self.matmul = P.MatMul().shard(strategy1)
  814. self.mul_no_nan = P.MulNoNan().shard(strategy2)
  815. def construct(self, x, y, b):
  816. out = self.matmul(x, y)
  817. out = self.mul_no_nan(out, b)
  818. return out
  819. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  820. strategy1 = ((2, 4), (4, 1))
  821. strategy2 = ((4, 1), (1, 2))
  822. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  823. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  824. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  825. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  826. compile_net(net, x, y, b)
  827. def test_matmul_truncate_div_broadcast():
  828. """
  829. Feature: distribute operator TruncateDiv in auto parallel.
  830. Description: mul-TruncateDiv net with strategy in semi auto parallel.
  831. Expectation: compile done without error.
  832. """
  833. class Net(nn.Cell):
  834. def __init__(self, strategy1, strategy2):
  835. super().__init__()
  836. self.matmul = P.MatMul().shard(strategy1)
  837. self.truncate_div = P.TruncateDiv().shard(strategy2)
  838. def construct(self, x, y, b):
  839. out = self.matmul(x, y)
  840. out = self.truncate_div(out, b)
  841. return out
  842. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  843. strategy1 = ((2, 4), (4, 1))
  844. strategy2 = ((4, 1), (1, 2))
  845. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  846. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  847. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  848. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  849. compile_net(net, x, y, b)
  850. def test_matmul_truncate_mod_broadcast():
  851. """
  852. Feature: distribute operator TruncateMod in auto parallel.
  853. Description: mul-TruncateMod net with strategy in semi auto parallel.
  854. Expectation: compile done without error.
  855. """
  856. class Net(nn.Cell):
  857. def __init__(self, strategy1, strategy2):
  858. super().__init__()
  859. self.matmul = P.MatMul().shard(strategy1)
  860. self.truncate_mod = P.TruncateMod().shard(strategy2)
  861. def construct(self, x, y, b):
  862. out = self.matmul(x, y)
  863. out = self.truncate_mod(out, b)
  864. return out
  865. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  866. strategy1 = ((2, 4), (4, 1))
  867. strategy2 = ((4, 1), (1, 2))
  868. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  869. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  870. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  871. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  872. compile_net(net, x, y, b)
  873. def test_matmul_xdivy_broadcast():
  874. """
  875. Feature: distribute operator Xdivy in auto parallel.
  876. Description: mul-Xdivy net with strategy in semi auto parallel.
  877. Expectation: compile done without error.
  878. """
  879. class Net(nn.Cell):
  880. def __init__(self, strategy1, strategy2):
  881. super().__init__()
  882. self.matmul = P.MatMul().shard(strategy1)
  883. self.xdivy = P.Xdivy().shard(strategy2)
  884. def construct(self, x, y, b):
  885. out = self.matmul(x, y)
  886. out = self.xdivy(out, b)
  887. return out
  888. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  889. strategy1 = ((2, 4), (4, 1))
  890. strategy2 = ((4, 1), (1, 2))
  891. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  892. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  893. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  894. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  895. compile_net(net, x, y, b)
  896. def test_matmul_xlogy_broadcast():
  897. """
  898. Feature: distribute operator Xlogy in auto parallel.
  899. Description: mul-Xlogy net with strategy in semi auto parallel.
  900. Expectation: compile done without error.
  901. """
  902. class Net(nn.Cell):
  903. def __init__(self, strategy1, strategy2):
  904. super().__init__()
  905. self.matmul = P.MatMul().shard(strategy1)
  906. self.xlogy = P.Xlogy().shard(strategy2)
  907. def construct(self, x, y, b):
  908. out = self.matmul(x, y)
  909. out = self.xlogy(out, b)
  910. return out
  911. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  912. strategy1 = ((2, 4), (4, 1))
  913. strategy2 = ((4, 1), (1, 2))
  914. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  915. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  916. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  917. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  918. compile_net(net, x, y, b)
  919. def test_matmul_squared_difference_broadcast():
  920. """
  921. Feature: distribute operator SquaredDifference in auto parallel.
  922. Description: mul-SquaredDifference net with strategy in semi auto parallel.
  923. Expectation: compile done without error.
  924. """
  925. class Net(nn.Cell):
  926. def __init__(self, strategy1, strategy2):
  927. super().__init__()
  928. self.matmul = P.MatMul().shard(strategy1)
  929. self.squared_difference = P.SquaredDifference().shard(strategy2)
  930. def construct(self, x, y, b):
  931. out = self.matmul(x, y)
  932. out = self.squared_difference(out, b)
  933. return out
  934. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  935. strategy1 = ((2, 4), (4, 1))
  936. strategy2 = ((4, 1), (1, 2))
  937. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  938. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  939. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  940. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  941. compile_net(net, x, y, b)
  942. def test_matmul_masked_fill_broadcast_with_value_float():
  943. """
  944. Feature: distribute operator MaskedFill in auto parallel.
  945. Description: mul-MaskedFill net with strategy in semi auto parallel.
  946. Expectation: compile done without error.
  947. """
  948. class Net(nn.Cell):
  949. def __init__(self, strategy1, strategy2):
  950. super().__init__()
  951. self.matmul = P.MatMul().shard(strategy1)
  952. self.masked_fill = P.MaskedFill().shard(strategy2)
  953. self.value = 1.0
  954. def construct(self, x, y, b):
  955. out = self.matmul(x, y)
  956. out = self.masked_fill(out, b, self.value)
  957. return out
  958. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  959. strategy1 = ((2, 4), (4, 1))
  960. strategy2 = ((4, 1), (1, 2))
  961. net = Net(strategy1, strategy2)
  962. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  963. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  964. b = Tensor(np.ones([1, 64]), dtype=ms.bool_)
  965. compile_net(net, x, y, b)
  966. def test_matmul_masked_fill_broadcast_with_value_tensor():
  967. """
  968. Feature: distribute operator MaskedFill in auto parallel.
  969. Description: mul-MaskedFill net with strategy in semi auto parallel.
  970. Expectation: compile done without error.
  971. """
  972. class Net(nn.Cell):
  973. def __init__(self, strategy1, strategy2):
  974. super().__init__()
  975. self.matmul = P.MatMul().shard(strategy1)
  976. self.masked_fill = P.MaskedFill().shard(strategy2)
  977. self.value = Tensor(1.0, ms.float32)
  978. def construct(self, x, y, b):
  979. out = self.matmul(x, y)
  980. out = self.masked_fill(out, b, self.value)
  981. return out
  982. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  983. strategy1 = ((2, 4), (4, 1))
  984. strategy2 = ((4, 1), (1, 2), ())
  985. net = Net(strategy1, strategy2)
  986. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  987. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  988. b = Tensor(np.ones([1, 64]), dtype=ms.bool_)
  989. compile_net(net, x, y, b)