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 18 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. class NetWithLoss(nn.Cell):
  23. def __init__(self, network):
  24. super(NetWithLoss, self).__init__()
  25. self.loss = VirtualLoss()
  26. self.network = network
  27. def construct(self, x, y, b):
  28. predict = self.network(x, y, b)
  29. return self.loss(predict)
  30. class GradWrap(nn.Cell):
  31. def __init__(self, network):
  32. super(GradWrap, self).__init__()
  33. self.network = network
  34. def construct(self, x, y, b):
  35. return C.grad_all(self.network)(x, y, b)
  36. def compile_net(net, x, y, b):
  37. net.set_auto_parallel()
  38. _executor.compile(net, x, y, b)
  39. def test_matmul_sub():
  40. class Net(nn.Cell):
  41. def __init__(self, strategy1, strategy2):
  42. super().__init__()
  43. self.matmul = P.MatMul().set_strategy(strategy1)
  44. self.sub = P.Sub().set_strategy(strategy2)
  45. def construct(self, x, y, b):
  46. out = self.matmul(x, y)
  47. out = self.sub(out, b)
  48. return out
  49. context.set_auto_parallel_context(device_num=8, global_rank=0)
  50. strategy1 = ((2, 2), (2, 2))
  51. strategy2 = ((4, 2), (4, 2))
  52. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  53. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  54. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  55. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  56. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  57. compile_net(net, x, y, b)
  58. def test_matmul_add():
  59. class Net(nn.Cell):
  60. def __init__(self, strategy1, strategy2):
  61. super().__init__()
  62. self.matmul = P.MatMul().set_strategy(strategy1)
  63. self.add = P.TensorAdd().set_strategy(strategy2)
  64. def construct(self, x, y, b):
  65. out = self.matmul(x, y)
  66. out = self.add(out, b)
  67. return out
  68. context.set_auto_parallel_context(device_num=8, global_rank=0)
  69. strategy1 = ((2, 2), (2, 2))
  70. strategy2 = ((4, 2), (4, 2))
  71. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  72. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  73. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  74. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  75. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  76. compile_net(net, x, y, b)
  77. def test_matmul_mul():
  78. class Net(nn.Cell):
  79. def __init__(self, strategy1, strategy2):
  80. super().__init__()
  81. self.matmul = P.MatMul().set_strategy(strategy1)
  82. self.mul = P.Mul().set_strategy(strategy2)
  83. def construct(self, x, y, b):
  84. out = self.matmul(x, y)
  85. out = self.mul(out, b)
  86. return out
  87. context.set_auto_parallel_context(device_num=8, global_rank=0)
  88. strategy1 = ((2, 2), (2, 2))
  89. strategy2 = ((4, 2), (4, 2))
  90. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  91. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  92. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  93. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  94. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  95. compile_net(net, x, y, b)
  96. def test_matmul_div():
  97. class Net(nn.Cell):
  98. def __init__(self, strategy1, strategy2):
  99. super().__init__()
  100. self.matmul = P.MatMul().set_strategy(strategy1)
  101. self.div = P.Div().set_strategy(strategy2)
  102. def construct(self, x, y, b):
  103. out = self.matmul(x, y)
  104. out = self.div(out, b)
  105. return out
  106. context.set_auto_parallel_context(device_num=8, global_rank=0)
  107. strategy1 = ((2, 2), (2, 2))
  108. strategy2 = ((4, 2), (4, 2))
  109. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  110. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  111. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  112. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  113. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  114. compile_net(net, x, y, b)
  115. def test_matmul_greater():
  116. class Net(nn.Cell):
  117. def __init__(self, strategy1, strategy2):
  118. super().__init__()
  119. self.matmul = P.MatMul().set_strategy(strategy1)
  120. self.greater = P.Greater().set_strategy(strategy2)
  121. def construct(self, x, y, b):
  122. out = self.matmul(x, y)
  123. out = self.greater(out, b)
  124. return out
  125. context.set_auto_parallel_context(device_num=8, global_rank=0)
  126. strategy1 = ((2, 2), (2, 2))
  127. strategy2 = ((4, 2), (4, 2))
  128. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  129. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  130. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  131. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  132. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  133. compile_net(net, x, y, b)
  134. def test_matmul_add_broadcast():
  135. class Net(nn.Cell):
  136. def __init__(self, strategy1, strategy2):
  137. super().__init__()
  138. self.matmul = P.MatMul().set_strategy(strategy1)
  139. self.add = P.TensorAdd().set_strategy(strategy2)
  140. def construct(self, x, y, b):
  141. out = self.matmul(x, y)
  142. out = self.add(out, b)
  143. return out
  144. context.set_auto_parallel_context(device_num=8, global_rank=0)
  145. strategy1 = ((2, 2), (2, 2))
  146. strategy2 = ((4, 2), (2,))
  147. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  148. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  149. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  150. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  151. b = Tensor(np.ones([64]), dtype=ms.float32)
  152. compile_net(net, x, y, b)
  153. def test_matmul_add_broadcast2():
  154. class Net(nn.Cell):
  155. def __init__(self, strategy1, strategy2):
  156. super().__init__()
  157. self.matmul = P.MatMul().set_strategy(strategy1)
  158. self.add = P.TensorAdd().set_strategy(strategy2)
  159. def construct(self, x, y, b):
  160. out = self.matmul(x, y)
  161. out = self.add(out, b)
  162. return out
  163. context.set_auto_parallel_context(device_num=8, global_rank=0)
  164. strategy1 = ((2, 4), (4, 1))
  165. strategy2 = ((4, 1), (1, 2))
  166. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  167. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  168. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  169. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  170. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  171. compile_net(net, x, y, b)
  172. def test_matmul_sub_broadcast():
  173. class Net(nn.Cell):
  174. def __init__(self, strategy1, strategy2):
  175. super().__init__()
  176. self.matmul = P.MatMul().set_strategy(strategy1)
  177. self.sub = P.Sub().set_strategy(strategy2)
  178. def construct(self, x, y, b):
  179. out = self.matmul(x, y)
  180. out = self.sub(out, b)
  181. return out
  182. context.set_auto_parallel_context(device_num=8, global_rank=0)
  183. strategy1 = ((2, 2), (2, 2))
  184. strategy2 = ((4, 2), (2,))
  185. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  186. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  187. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  188. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  189. b = Tensor(np.ones([64]), dtype=ms.float32)
  190. compile_net(net, x, y, b)
  191. def test_matmul_sub_broadcast2():
  192. class Net(nn.Cell):
  193. def __init__(self, strategy1, strategy2):
  194. super().__init__()
  195. self.matmul = P.MatMul().set_strategy(strategy1)
  196. self.sub = P.Sub().set_strategy(strategy2)
  197. def construct(self, x, y, b):
  198. out = self.matmul(x, y)
  199. out = self.sub(out, b)
  200. return out
  201. context.set_auto_parallel_context(device_num=8, global_rank=0)
  202. strategy1 = ((2, 4), (4, 1))
  203. strategy2 = ((4, 1), (1, 2))
  204. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  205. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  206. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  207. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  208. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  209. compile_net(net, x, y, b)
  210. def test_matmul_mul_broadcast():
  211. class Net(nn.Cell):
  212. def __init__(self, strategy1, strategy2):
  213. super().__init__()
  214. self.matmul = P.MatMul().set_strategy(strategy1)
  215. self.mul = P.Mul().set_strategy(strategy2)
  216. def construct(self, x, y, b):
  217. out = self.matmul(x, y)
  218. out = self.mul(out, b)
  219. return out
  220. context.set_auto_parallel_context(device_num=8, global_rank=0)
  221. strategy1 = ((2, 2), (2, 2))
  222. strategy2 = ((4, 2), (2,))
  223. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  224. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  225. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  226. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  227. b = Tensor(np.ones([64]), dtype=ms.float32)
  228. compile_net(net, x, y, b)
  229. def test_matmul_mul_broadcast2():
  230. class Net(nn.Cell):
  231. def __init__(self, strategy1, strategy2):
  232. super().__init__()
  233. self.matmul = P.MatMul().set_strategy(strategy1)
  234. self.mul = P.Mul().set_strategy(strategy2)
  235. def construct(self, x, y, b):
  236. out = self.matmul(x, y)
  237. out = self.mul(out, b)
  238. return out
  239. context.set_auto_parallel_context(device_num=8, global_rank=0)
  240. strategy1 = ((2, 4), (4, 1))
  241. strategy2 = ((4, 1), (1, 2))
  242. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  243. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  244. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  245. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  246. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  247. compile_net(net, x, y, b)
  248. def test_matmul_div_broadcast():
  249. class Net(nn.Cell):
  250. def __init__(self, strategy1, strategy2):
  251. super().__init__()
  252. self.matmul = P.MatMul().set_strategy(strategy1)
  253. self.div = P.Div().set_strategy(strategy2)
  254. def construct(self, x, y, b):
  255. out = self.matmul(x, y)
  256. out = self.div(out, b)
  257. return out
  258. context.set_auto_parallel_context(device_num=8, global_rank=0)
  259. strategy1 = ((2, 2), (2, 2))
  260. strategy2 = ((4, 2), (2,))
  261. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  262. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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]), dtype=ms.float32)
  266. compile_net(net, x, y, b)
  267. def test_matmul_div_broadcast2():
  268. class Net(nn.Cell):
  269. def __init__(self, strategy1, strategy2):
  270. super().__init__()
  271. self.matmul = P.MatMul().set_strategy(strategy1)
  272. self.div = P.Div().set_strategy(strategy2)
  273. def construct(self, x, y, b):
  274. out = self.matmul(x, y)
  275. out = self.div(out, b)
  276. return out
  277. context.set_auto_parallel_context(device_num=8, global_rank=0)
  278. strategy1 = ((2, 4), (4, 1))
  279. strategy2 = ((4, 1), (1, 2))
  280. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  281. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  282. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  283. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  284. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  285. compile_net(net, x, y, b)
  286. def test_matmul_greater_broadcast():
  287. class Net(nn.Cell):
  288. def __init__(self, strategy1, strategy2):
  289. super().__init__()
  290. self.matmul = P.MatMul().set_strategy(strategy1)
  291. self.greater = P.Greater().set_strategy(strategy2)
  292. def construct(self, x, y, b):
  293. out = self.matmul(x, y)
  294. out = self.greater(out, b)
  295. return out
  296. context.set_auto_parallel_context(device_num=8, global_rank=0)
  297. strategy1 = ((2, 2), (2, 2))
  298. strategy2 = ((4, 2), (2,))
  299. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  300. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  301. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  302. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  303. b = Tensor(np.ones([64]), dtype=ms.float32)
  304. compile_net(net, x, y, b)
  305. def test_matmul_greater_broadcast2():
  306. class Net(nn.Cell):
  307. def __init__(self, strategy1, strategy2):
  308. super().__init__()
  309. self.matmul = P.MatMul().set_strategy(strategy1)
  310. self.greater = P.Greater().set_strategy(strategy2)
  311. def construct(self, x, y, b):
  312. out = self.matmul(x, y)
  313. out = self.greater(out, b)
  314. return out
  315. context.set_auto_parallel_context(device_num=8, global_rank=0)
  316. strategy1 = ((2, 4), (4, 1))
  317. strategy2 = ((4, 1), (1, 2))
  318. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  319. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  320. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  321. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  322. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  323. compile_net(net, x, y, b)
  324. def test_matmul_floordiv():
  325. class Net(nn.Cell):
  326. def __init__(self, strategy1, strategy2):
  327. super().__init__()
  328. self.matmul = P.MatMul().set_strategy(strategy1)
  329. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  330. def construct(self, x, y, b):
  331. out = self.matmul(x, y)
  332. out = self.floordiv(out, b)
  333. return out
  334. context.set_auto_parallel_context(device_num=8, global_rank=0)
  335. strategy1 = ((2, 2), (2, 2))
  336. strategy2 = ((4, 2), (4, 2))
  337. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  338. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  339. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  340. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  341. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  342. compile_net(net, x, y, b)
  343. def test_matmul_floordiv_broadcast():
  344. class Net(nn.Cell):
  345. def __init__(self, strategy1, strategy2):
  346. super().__init__()
  347. self.matmul = P.MatMul().set_strategy(strategy1)
  348. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  349. def construct(self, x, y, b):
  350. out = self.matmul(x, y)
  351. out = self.floordiv(out, b)
  352. return out
  353. context.set_auto_parallel_context(device_num=8, global_rank=0)
  354. strategy1 = ((2, 2), (2, 2))
  355. strategy2 = ((4, 2), (2,))
  356. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  357. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  358. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  359. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  360. b = Tensor(np.ones([64]), dtype=ms.float32)
  361. compile_net(net, x, y, b)
  362. def test_matmul_floordiv_broadcast2():
  363. class Net(nn.Cell):
  364. def __init__(self, strategy1, strategy2):
  365. super().__init__()
  366. self.matmul = P.MatMul().set_strategy(strategy1)
  367. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  368. def construct(self, x, y, b):
  369. out = self.matmul(x, y)
  370. out = self.floordiv(out, b)
  371. return out
  372. context.set_auto_parallel_context(device_num=8, global_rank=0)
  373. strategy1 = ((2, 4), (4, 1))
  374. strategy2 = ((4, 1), (1, 2))
  375. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  376. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  377. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  378. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  379. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  380. compile_net(net, x, y, b)
  381. def test_assign_sub():
  382. class Net(nn.Cell):
  383. def __init__(self):
  384. super().__init__()
  385. self.assign_sub = P.AssignSub()
  386. self.mul = P.Mul()
  387. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  388. 0.5, dtype=np.float32)),
  389. name="mul_weight")
  390. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  391. 1.1, dtype=np.float32)),
  392. name="assignsub_weight")
  393. def construct(self, x):
  394. out = self.mul(x, self.mul_weight)
  395. out = self.assign_sub(self.assignsub_weight, out)
  396. return out
  397. class SubNetWithLoss(nn.Cell):
  398. def __init__(self, network):
  399. super(SubNetWithLoss, self).__init__()
  400. self.loss = VirtualLoss()
  401. self.network = network
  402. def construct(self, x):
  403. predict = self.network(x,)
  404. return self.loss(predict)
  405. class SubGradWrap(nn.Cell):
  406. def __init__(self, network):
  407. super(SubGradWrap, self).__init__()
  408. self.network = network
  409. def construct(self, x):
  410. return C.grad_all(self.network)(x)
  411. def compile_sub_net(net, x):
  412. net.set_auto_parallel()
  413. _executor.compile(net, x)
  414. context.set_auto_parallel_context(device_num=64, global_rank=15)
  415. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  416. net = SubGradWrap(SubNetWithLoss(Net()))
  417. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  418. compile_sub_net(net, x)