You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_arithmetic.py 26 kB

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