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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 mindspore.parallel._utils import _set_has_initializer
  22. from tests.ut.python.ops.test_math_ops import VirtualLoss
  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 C.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. _set_has_initializer(False)
  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().set_strategy(strategy1)
  65. self.add = P.TensorAdd().set_strategy(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().set_strategy(strategy1)
  84. self.mul = P.Mul().set_strategy(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_div():
  99. class Net(nn.Cell):
  100. def __init__(self, strategy1, strategy2):
  101. super().__init__()
  102. self.matmul = P.MatMul().set_strategy(strategy1)
  103. self.div = P.Div().set_strategy(strategy2)
  104. def construct(self, x, y, b):
  105. out = self.matmul(x, y)
  106. out = self.div(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_greater():
  118. class Net(nn.Cell):
  119. def __init__(self, strategy1, strategy2):
  120. super().__init__()
  121. self.matmul = P.MatMul().set_strategy(strategy1)
  122. self.greater = P.Greater().set_strategy(strategy2)
  123. def construct(self, x, y, b):
  124. out = self.matmul(x, y)
  125. out = self.greater(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_add_broadcast():
  137. class Net(nn.Cell):
  138. def __init__(self, strategy1, strategy2):
  139. super().__init__()
  140. self.matmul = P.MatMul().set_strategy(strategy1)
  141. self.add = P.TensorAdd().set_strategy(strategy2)
  142. def construct(self, x, y, b):
  143. out = self.matmul(x, y)
  144. out = self.add(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), (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]), dtype=ms.float32)
  154. compile_net(net, x, y, b)
  155. def test_matmul_add_broadcast2():
  156. class Net(nn.Cell):
  157. def __init__(self, strategy1, strategy2):
  158. super().__init__()
  159. self.matmul = P.MatMul().set_strategy(strategy1)
  160. self.add = P.TensorAdd().set_strategy(strategy2)
  161. def construct(self, x, y, b):
  162. out = self.matmul(x, y)
  163. out = self.add(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, 4), (4, 1))
  168. strategy2 = ((4, 1), (1, 2))
  169. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  170. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  171. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  172. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  173. compile_net(net, x, y, b)
  174. def test_matmul_sub_broadcast():
  175. class Net(nn.Cell):
  176. def __init__(self, strategy1, strategy2):
  177. super().__init__()
  178. self.matmul = P.MatMul().set_strategy(strategy1)
  179. self.sub = P.Sub().set_strategy(strategy2)
  180. def construct(self, x, y, b):
  181. out = self.matmul(x, y)
  182. out = self.sub(out, b)
  183. return out
  184. context.set_auto_parallel_context(device_num=8, global_rank=0)
  185. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  186. strategy1 = ((2, 2), (2, 2))
  187. strategy2 = ((4, 2), (2,))
  188. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  189. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  190. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  191. b = Tensor(np.ones([64]), dtype=ms.float32)
  192. compile_net(net, x, y, b)
  193. def test_matmul_sub_broadcast2():
  194. class Net(nn.Cell):
  195. def __init__(self, strategy1, strategy2):
  196. super().__init__()
  197. self.matmul = P.MatMul().set_strategy(strategy1)
  198. self.sub = P.Sub().set_strategy(strategy2)
  199. def construct(self, x, y, b):
  200. out = self.matmul(x, y)
  201. out = self.sub(out, b)
  202. return out
  203. context.set_auto_parallel_context(device_num=8, global_rank=0)
  204. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  205. strategy1 = ((2, 4), (4, 1))
  206. strategy2 = ((4, 1), (1, 2))
  207. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  208. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  209. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  210. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  211. compile_net(net, x, y, b)
  212. def test_matmul_mul_broadcast():
  213. class Net(nn.Cell):
  214. def __init__(self, strategy1, strategy2):
  215. super().__init__()
  216. self.matmul = P.MatMul().set_strategy(strategy1)
  217. self.mul = P.Mul().set_strategy(strategy2)
  218. def construct(self, x, y, b):
  219. out = self.matmul(x, y)
  220. out = self.mul(out, b)
  221. return out
  222. context.set_auto_parallel_context(device_num=8, global_rank=0)
  223. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  224. strategy1 = ((2, 2), (2, 2))
  225. strategy2 = ((4, 2), (2,))
  226. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  227. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  228. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  229. b = Tensor(np.ones([64]), dtype=ms.float32)
  230. compile_net(net, x, y, b)
  231. def test_matmul_mul_broadcast2():
  232. class Net(nn.Cell):
  233. def __init__(self, strategy1, strategy2):
  234. super().__init__()
  235. self.matmul = P.MatMul().set_strategy(strategy1)
  236. self.mul = P.Mul().set_strategy(strategy2)
  237. def construct(self, x, y, b):
  238. out = self.matmul(x, y)
  239. out = self.mul(out, b)
  240. return out
  241. context.set_auto_parallel_context(device_num=8, global_rank=0)
  242. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  243. strategy1 = ((2, 4), (4, 1))
  244. strategy2 = ((4, 1), (1, 2))
  245. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  246. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  247. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  248. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  249. compile_net(net, x, y, b)
  250. def test_matmul_div_broadcast():
  251. class Net(nn.Cell):
  252. def __init__(self, strategy1, strategy2):
  253. super().__init__()
  254. self.matmul = P.MatMul().set_strategy(strategy1)
  255. self.div = P.Div().set_strategy(strategy2)
  256. def construct(self, x, y, b):
  257. out = self.matmul(x, y)
  258. out = self.div(out, b)
  259. return out
  260. context.set_auto_parallel_context(device_num=8, global_rank=0)
  261. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  262. strategy1 = ((2, 2), (2, 2))
  263. strategy2 = ((4, 2), (2,))
  264. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  265. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  266. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  267. b = Tensor(np.ones([64]), dtype=ms.float32)
  268. compile_net(net, x, y, b)
  269. def test_matmul_div_broadcast2():
  270. class Net(nn.Cell):
  271. def __init__(self, strategy1, strategy2):
  272. super().__init__()
  273. self.matmul = P.MatMul().set_strategy(strategy1)
  274. self.div = P.Div().set_strategy(strategy2)
  275. def construct(self, x, y, b):
  276. out = self.matmul(x, y)
  277. out = self.div(out, b)
  278. return out
  279. context.set_auto_parallel_context(device_num=8, global_rank=0)
  280. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  281. strategy1 = ((2, 4), (4, 1))
  282. strategy2 = ((4, 1), (1, 2))
  283. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  284. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  285. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  286. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  287. compile_net(net, x, y, b)
  288. def test_matmul_greater_broadcast():
  289. class Net(nn.Cell):
  290. def __init__(self, strategy1, strategy2):
  291. super().__init__()
  292. self.matmul = P.MatMul().set_strategy(strategy1)
  293. self.greater = P.Greater().set_strategy(strategy2)
  294. def construct(self, x, y, b):
  295. out = self.matmul(x, y)
  296. out = self.greater(out, b)
  297. return out
  298. context.set_auto_parallel_context(device_num=8, global_rank=0)
  299. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  300. strategy1 = ((2, 2), (2, 2))
  301. strategy2 = ((4, 2), (2,))
  302. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  303. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  304. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  305. b = Tensor(np.ones([64]), dtype=ms.float32)
  306. compile_net(net, x, y, b)
  307. def test_matmul_greater_broadcast2():
  308. class Net(nn.Cell):
  309. def __init__(self, strategy1, strategy2):
  310. super().__init__()
  311. self.matmul = P.MatMul().set_strategy(strategy1)
  312. self.greater = P.Greater().set_strategy(strategy2)
  313. def construct(self, x, y, b):
  314. out = self.matmul(x, y)
  315. out = self.greater(out, b)
  316. return out
  317. context.set_auto_parallel_context(device_num=8, global_rank=0)
  318. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  319. strategy1 = ((2, 4), (4, 1))
  320. strategy2 = ((4, 1), (1, 2))
  321. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  322. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  323. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  324. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  325. compile_net(net, x, y, b)
  326. def test_matmul_floordiv():
  327. class Net(nn.Cell):
  328. def __init__(self, strategy1, strategy2):
  329. super().__init__()
  330. self.matmul = P.MatMul().set_strategy(strategy1)
  331. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  332. def construct(self, x, y, b):
  333. out = self.matmul(x, y)
  334. out = self.floordiv(out, b)
  335. return out
  336. context.set_auto_parallel_context(device_num=8, global_rank=0)
  337. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  338. strategy1 = ((2, 2), (2, 2))
  339. strategy2 = ((4, 2), (4, 2))
  340. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  341. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  342. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  343. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  344. compile_net(net, x, y, b)
  345. def test_matmul_floordiv_broadcast():
  346. class Net(nn.Cell):
  347. def __init__(self, strategy1, strategy2):
  348. super().__init__()
  349. self.matmul = P.MatMul().set_strategy(strategy1)
  350. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  351. def construct(self, x, y, b):
  352. out = self.matmul(x, y)
  353. out = self.floordiv(out, b)
  354. return out
  355. context.set_auto_parallel_context(device_num=8, global_rank=0)
  356. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  357. strategy1 = ((2, 2), (2, 2))
  358. strategy2 = ((4, 2), (2,))
  359. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  360. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  361. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  362. b = Tensor(np.ones([64]), dtype=ms.float32)
  363. compile_net(net, x, y, b)
  364. def test_matmul_floordiv_broadcast2():
  365. class Net(nn.Cell):
  366. def __init__(self, strategy1, strategy2):
  367. super().__init__()
  368. self.matmul = P.MatMul().set_strategy(strategy1)
  369. self.floordiv = P.FloorDiv().set_strategy(strategy2)
  370. def construct(self, x, y, b):
  371. out = self.matmul(x, y)
  372. out = self.floordiv(out, b)
  373. return out
  374. context.set_auto_parallel_context(device_num=8, global_rank=0)
  375. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  376. strategy1 = ((2, 4), (4, 1))
  377. strategy2 = ((4, 1), (1, 2))
  378. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  379. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  380. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  381. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  382. compile_net(net, x, y, b)
  383. def test_assign_sub():
  384. class Net(nn.Cell):
  385. def __init__(self):
  386. super().__init__()
  387. self.assign_sub = P.AssignSub()
  388. self.mul = P.Mul()
  389. self.mul_weight = Parameter(Tensor(np.full([128, 32],
  390. 0.5, dtype=np.float32)),
  391. name="mul_weight")
  392. self.assignsub_weight = Parameter(Tensor(np.full([128, 32],
  393. 1.1, dtype=np.float32)),
  394. name="assignsub_weight")
  395. def construct(self, x):
  396. out = self.mul(x, self.mul_weight)
  397. out = self.assign_sub(self.assignsub_weight, out)
  398. return out
  399. class SubNetWithLoss(nn.Cell):
  400. def __init__(self, network):
  401. super(SubNetWithLoss, self).__init__()
  402. self.loss = VirtualLoss()
  403. self.network = network
  404. def construct(self, x):
  405. predict = self.network(x,)
  406. return self.loss(predict)
  407. class SubGradWrap(nn.Cell):
  408. def __init__(self, network):
  409. super(SubGradWrap, self).__init__()
  410. self.network = network
  411. def construct(self, x):
  412. return C.grad_all(self.network)(x)
  413. def compile_sub_net(net, x):
  414. net.set_auto_parallel()
  415. _executor.compile(net, x)
  416. context.set_auto_parallel_context(device_num=64, global_rank=15)
  417. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  418. net = SubGradWrap(SubNetWithLoss(Net()))
  419. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  420. compile_sub_net(net, x)