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 39 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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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)