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