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_reduce_method_info.py 23 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 pytest
  16. import mindspore as ms
  17. import mindspore.nn as nn
  18. from mindspore import Tensor
  19. from mindspore import context
  20. from mindspore.common.api import _cell_graph_executor
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. from tests.ut.python.ops.test_math_ops import VirtualLoss
  24. grad_all = C.GradOperation(get_all=True)
  25. class NetWithLossNoBias(nn.Cell):
  26. def __init__(self, network):
  27. super(NetWithLossNoBias, self).__init__()
  28. self.loss = VirtualLoss()
  29. self.network = network
  30. def construct(self, x, y):
  31. predict = self.network(x, y)
  32. return self.loss(predict)
  33. class NetWithLoss(nn.Cell):
  34. def __init__(self, network):
  35. super(NetWithLoss, self).__init__()
  36. self.loss = VirtualLoss()
  37. self.network = network
  38. def construct(self, x, y, b):
  39. predict = self.network(x, y, b)
  40. return self.loss(predict)
  41. class GradWrapNoBias(nn.Cell):
  42. def __init__(self, network):
  43. super(GradWrapNoBias, self).__init__()
  44. self.network = network
  45. def construct(self, x, y):
  46. return grad_all(self.network)(x, y)
  47. class GradWrap(nn.Cell):
  48. def __init__(self, network):
  49. super(GradWrap, self).__init__()
  50. self.network = network
  51. def construct(self, x, y, b):
  52. return grad_all(self.network)(x, y, b)
  53. def compile_net_no_bias(net, x, y):
  54. net.set_auto_parallel()
  55. net.set_train()
  56. _cell_graph_executor.compile(net, x, y)
  57. def compile_net(net, x, y, b):
  58. net.set_auto_parallel()
  59. net.set_train()
  60. _cell_graph_executor.compile(net, x, y, b)
  61. # model_parallel test
  62. def test_sum_mul():
  63. class Net(nn.Cell):
  64. def __init__(self, strategy1, strategy2, strategy3):
  65. super().__init__()
  66. self.mul1 = P.Mul().shard(strategy1)
  67. self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy2)
  68. self.mul2 = P.Mul().shard(strategy3)
  69. def construct(self, x, y, b):
  70. out = self.mul1(x, y)
  71. out = self.reduce_sum(out, (1,))
  72. out = self.mul2(out, b)
  73. return out
  74. context.set_auto_parallel_context(device_num=8, global_rank=0)
  75. strategy1 = ((1, 1, 8), (1, 1, 8))
  76. strategy2 = ((4, 1, 2),)
  77. strategy3 = ((2, 4), (2, 4))
  78. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  79. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  80. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  81. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  82. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  83. compile_net(net, x, y, b)
  84. def test_sum_mul2():
  85. class Net(nn.Cell):
  86. def __init__(self, strategy1, strategy2, strategy3):
  87. super().__init__()
  88. self.mul1 = P.Mul().shard(strategy1)
  89. self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy2)
  90. self.mul2 = P.Mul().shard(strategy3)
  91. def construct(self, x, y, b):
  92. out = self.mul1(x, y)
  93. out = self.reduce_sum(out, (0, 1))
  94. out = self.mul2(out, b)
  95. return out
  96. context.set_auto_parallel_context(device_num=8, global_rank=0)
  97. strategy1 = ((1, 1, 4, 2), (1, 1, 4, 2))
  98. strategy2 = ((2, 4, 1, 1),)
  99. strategy3 = ((2, 4), (2, 4))
  100. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  101. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  102. x = Tensor(np.ones([128, 128, 64, 64]), dtype=ms.float32)
  103. y = Tensor(np.ones([128, 128, 64, 64]), dtype=ms.float32)
  104. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  105. compile_net(net, x, y, b)
  106. def test_sum_mul3():
  107. class Net(nn.Cell):
  108. def __init__(self, strategy1, strategy2, strategy3):
  109. super().__init__()
  110. self.mul1 = P.Mul().shard(strategy1)
  111. self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy2)
  112. self.mul2 = P.Mul().shard(strategy3)
  113. def construct(self, x, y, b):
  114. out = self.mul1(x, y)
  115. out = self.reduce_sum(out, -1)
  116. out = self.mul2(out, b)
  117. return out
  118. context.set_auto_parallel_context(device_num=8, global_rank=0)
  119. strategy1 = ((1, 4, 2), (1, 4, 2))
  120. strategy2 = ((4, 2, 1),)
  121. strategy3 = ((2, 4), (2, 4))
  122. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  123. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  124. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  125. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  126. b = Tensor(np.ones([128, 32]), dtype=ms.float32)
  127. compile_net(net, x, y, b)
  128. def test_sum_mul4():
  129. class Net(nn.Cell):
  130. def __init__(self, strategy1, strategy2, strategy3):
  131. super().__init__()
  132. self.mul1 = P.Mul().shard(strategy1)
  133. self.reduce_sum = P.ReduceSum(keep_dims=True).shard(strategy2)
  134. self.mul2 = P.Mul().shard(strategy3)
  135. def construct(self, x, y, b):
  136. out = self.mul1(x, y)
  137. out = self.reduce_sum(out, -1)
  138. out = self.mul2(out, b)
  139. return out
  140. context.set_auto_parallel_context(device_num=8, global_rank=0)
  141. strategy1 = ((1, 4, 2), (1, 4, 2))
  142. strategy2 = ((2, 2, 2),)
  143. strategy3 = ((4, 2, 1), (4, 2, 1))
  144. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  145. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  146. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  147. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  148. b = Tensor(np.ones([128, 32, 1]), dtype=ms.float32)
  149. compile_net(net, x, y, b)
  150. def test_sum_mul5():
  151. class Net(nn.Cell):
  152. def __init__(self, strategy1, strategy2):
  153. super().__init__()
  154. self.mul1 = P.Mul().shard(strategy1)
  155. self.reduce_sum = P.ReduceSum(keep_dims=True).shard(strategy2)
  156. def construct(self, x, y):
  157. out = self.mul1(x, y)
  158. out = self.reduce_sum(out, 0)
  159. return out
  160. context.set_auto_parallel_context(device_num=64, global_rank=0)
  161. strategy1 = ((1, 8, 8), (1, 8, 8))
  162. strategy2 = ((2, 4, 1),)
  163. net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
  164. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  165. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  166. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  167. compile_net_no_bias(net, x, y)
  168. def test_sum_mul6():
  169. class Net(nn.Cell):
  170. def __init__(self, strategy1, strategy2):
  171. super().__init__()
  172. self.mul1 = P.Mul().shard(strategy1)
  173. self.reduce_sum = P.ReduceSum(keep_dims=True).shard(strategy2)
  174. def construct(self, x, y):
  175. out = self.mul1(x, y)
  176. out = self.reduce_sum(out, 1)
  177. return out
  178. context.set_auto_parallel_context(device_num=64, global_rank=0)
  179. strategy1 = ((1, 8, 8), (1, 8, 8))
  180. strategy2 = ((2, 1, 4),)
  181. net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
  182. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  183. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  184. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  185. compile_net_no_bias(net, x, y)
  186. def test_sum_mul7():
  187. class Net(nn.Cell):
  188. def __init__(self, strategy1, strategy2):
  189. super().__init__()
  190. self.mul1 = P.Mul().shard(strategy1)
  191. self.reduce_sum = P.ReduceSum(keep_dims=True).shard(strategy2)
  192. def construct(self, x, y):
  193. out = self.mul1(x, y)
  194. out = self.reduce_sum(out, (0, 1))
  195. return out
  196. context.set_auto_parallel_context(device_num=64, global_rank=0)
  197. strategy1 = ((1, 8, 8), (1, 8, 8))
  198. strategy2 = ((2, 4, 1),)
  199. net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
  200. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  201. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  202. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  203. compile_net_no_bias(net, x, y)
  204. def test_max_mul():
  205. class Net(nn.Cell):
  206. def __init__(self, strategy1, strategy2, strategy3):
  207. super().__init__()
  208. self.mul1 = P.Mul().shard(strategy1)
  209. self.reduce_max = P.ReduceMax(keep_dims=False).shard(strategy2)
  210. self.mul2 = P.Mul().shard(strategy3)
  211. def construct(self, x, y, b):
  212. out = self.mul1(x, y)
  213. out = self.reduce_max(out, -1)
  214. out = self.mul2(out, b)
  215. return out
  216. context.set_auto_parallel_context(device_num=8, global_rank=0)
  217. strategy1 = ((1, 4, 2), (1, 4, 2))
  218. strategy2 = ((4, 1, 2),)
  219. strategy3 = ((2, 4), (2, 4))
  220. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  221. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  222. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  223. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  224. b = Tensor(np.ones([128, 32]), dtype=ms.float32)
  225. compile_net(net, x, y, b)
  226. def test_min_mul():
  227. class Net(nn.Cell):
  228. def __init__(self, strategy1, strategy2, strategy3):
  229. super().__init__()
  230. self.mul1 = P.Mul().shard(strategy1)
  231. self.reduce_min = P.ReduceMin(keep_dims=False).shard(strategy2)
  232. self.mul2 = P.Mul().shard(strategy3)
  233. def construct(self, x, y, b):
  234. out = self.mul1(x, y)
  235. out = self.reduce_min(out, 0)
  236. out = self.mul2(out, b)
  237. return out
  238. context.set_auto_parallel_context(device_num=8, global_rank=0)
  239. strategy1 = ((1, 4, 2), (1, 4, 2))
  240. strategy2 = ((4, 1, 2),)
  241. strategy3 = ((2, 4), (2, 4))
  242. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  243. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  244. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  245. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  246. b = Tensor(np.ones([32, 64]), dtype=ms.float32)
  247. compile_net(net, x, y, b)
  248. def test_reduce_mean_mul_float32():
  249. class Net(nn.Cell):
  250. def __init__(self, strategy1, strategy2, strategy3):
  251. super().__init__()
  252. self.mul1 = P.Mul().shard(strategy1)
  253. self.reduce_mean = P.ReduceMean(keep_dims=False).shard(strategy2)
  254. self.mul2 = P.Mul().shard(strategy3)
  255. def construct(self, x, y, b):
  256. out = self.mul1(x, y)
  257. out = self.reduce_mean(out, 0)
  258. out = self.mul2(out, b)
  259. return out
  260. context.set_auto_parallel_context(device_num=8, global_rank=0)
  261. strategy1 = ((1, 4, 2), (1, 4, 2))
  262. strategy2 = ((4, 1, 2),)
  263. strategy3 = ((2, 4), (2, 4))
  264. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  265. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  266. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  267. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  268. b = Tensor(np.ones([32, 64]), dtype=ms.float32)
  269. compile_net(net, x, y, b)
  270. class ArgMaxWithValueNet(nn.Cell):
  271. def __init__(self, strategy1, strategy2, strategy3):
  272. super().__init__()
  273. self.mul1 = P.Mul().shard(strategy1)
  274. self.arg_max_with_value = P.ArgMaxWithValue(keep_dims=False, axis=-1).shard(strategy2)
  275. self.mul2 = P.Mul().shard(strategy3)
  276. def construct(self, x, y, b):
  277. out = self.mul1(x, y)
  278. _, out = self.arg_max_with_value(out)
  279. out = self.mul2(out, b)
  280. return out
  281. class ArgMinWithValueNet(nn.Cell):
  282. def __init__(self, strategy1, strategy2, strategy3):
  283. super().__init__()
  284. self.mul1 = P.Mul().shard(strategy1)
  285. self.arg_min_with_value = P.ArgMinWithValue(keep_dims=False, axis=-1).shard(strategy2)
  286. self.mul2 = P.Mul().shard(strategy3)
  287. def construct(self, x, y, b):
  288. out = self.mul1(x, y)
  289. _, out = self.arg_min_with_value(out)
  290. out = self.mul2(out, b)
  291. return out
  292. def gen_inputs_and_compile_net(net):
  293. x = Tensor(np.ones([128, 64, 64]), dtype=ms.float32)
  294. y = Tensor(np.ones([128, 64, 64]), dtype=ms.float32)
  295. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  296. compile_net(net, x, y, b)
  297. def gen_inputs_and_compile_net_no_bias(net):
  298. x = Tensor(np.ones([128, 64, 64]), dtype=ms.float32)
  299. y = Tensor(np.ones([128, 64, 64]), dtype=ms.float32)
  300. compile_net_no_bias(net, x, y)
  301. def tobefixed_test_arg_max_with_value_mul_semi_axis_parallel():
  302. context.set_auto_parallel_context(device_num=8, global_rank=0)
  303. strategy1 = ((1, 4, 2), (1, 4, 2))
  304. strategy2 = ((4, 1, 2),)
  305. strategy3 = ((2, 4), (2, 4))
  306. net = GradWrap(NetWithLoss(ArgMaxWithValueNet(strategy1, strategy2, strategy3)))
  307. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  308. gen_inputs_and_compile_net(net)
  309. def test_arg_max_with_value_mul_semi():
  310. context.set_auto_parallel_context(device_num=8, global_rank=0)
  311. strategy1 = ((1, 4, 2), (1, 4, 2))
  312. strategy2 = ((4, 1, 1),)
  313. strategy3 = ((2, 4), (2, 4))
  314. net = GradWrap(NetWithLoss(ArgMaxWithValueNet(strategy1, strategy2, strategy3)))
  315. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  316. gen_inputs_and_compile_net(net)
  317. def test_arg_max_with_value_mul_auto():
  318. context.set_auto_parallel_context(device_num=8, global_rank=0)
  319. strategy1 = None
  320. strategy2 = None
  321. strategy3 = None
  322. net = GradWrap(NetWithLoss(ArgMaxWithValueNet(strategy1, strategy2, strategy3)))
  323. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  324. gen_inputs_and_compile_net(net)
  325. def test_arg_min_with_value_mul_semi_axis_parallel():
  326. context.set_auto_parallel_context(device_num=8, global_rank=0)
  327. strategy1 = ((1, 4, 2), (1, 4, 2))
  328. strategy2 = ((4, 1, 2),)
  329. strategy3 = ((2, 4), (2, 4))
  330. net = GradWrap(NetWithLoss(ArgMinWithValueNet(strategy1, strategy2, strategy3)))
  331. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  332. gen_inputs_and_compile_net(net)
  333. def test_arg_min_with_value_mul_semi():
  334. context.set_auto_parallel_context(device_num=8, global_rank=0)
  335. strategy1 = ((1, 4, 2), (1, 4, 2))
  336. strategy2 = ((4, 1, 1),)
  337. strategy3 = ((2, 4), (2, 4))
  338. net = GradWrap(NetWithLoss(ArgMinWithValueNet(strategy1, strategy2, strategy3)))
  339. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  340. gen_inputs_and_compile_net(net)
  341. def test_arg_min_with_value_mul_auto():
  342. context.set_auto_parallel_context(device_num=8, global_rank=0)
  343. strategy1 = None
  344. strategy2 = None
  345. strategy3 = None
  346. net = GradWrap(NetWithLoss(ArgMinWithValueNet(strategy1, strategy2, strategy3)))
  347. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  348. gen_inputs_and_compile_net(net)
  349. class ArgMinWithValueNet2(nn.Cell):
  350. def __init__(self, strategy1, strategy2, strategy3):
  351. super().__init__()
  352. self.mul1 = P.Mul().shard(strategy1)
  353. self.arg_min_with_value = P.ArgMinWithValue(keep_dims=True, axis=-1).shard(strategy2)
  354. self.relu = P.ReLU().shard(strategy3)
  355. def construct(self, x, y):
  356. out = self.mul1(x, y)
  357. _, out = self.arg_min_with_value(out)
  358. out = self.relu(out)
  359. return out
  360. def tobefixed_test_arg_min_with_value_mul_semi_axis_parallel2():
  361. context.set_auto_parallel_context(device_num=8, global_rank=0)
  362. strategy1 = ((1, 4, 2), (1, 4, 2))
  363. strategy2 = ((4, 1, 2),)
  364. strategy3 = ((2, 4, 1),)
  365. net = GradWrapNoBias(NetWithLossNoBias(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
  366. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  367. gen_inputs_and_compile_net_no_bias(net)
  368. def test_arg_min_with_value_mul_semi2():
  369. context.set_auto_parallel_context(device_num=8, global_rank=0)
  370. strategy1 = ((1, 4, 2), (1, 4, 2))
  371. strategy2 = ((4, 1, 1),)
  372. strategy3 = ((2, 4, 1),)
  373. net = GradWrapNoBias(NetWithLossNoBias(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
  374. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  375. gen_inputs_and_compile_net_no_bias(net)
  376. def test_arg_min_with_value_mul_auto2():
  377. context.set_auto_parallel_context(device_num=8, global_rank=0)
  378. strategy1 = None
  379. strategy2 = None
  380. strategy3 = None
  381. net = GradWrapNoBias(NetWithLossNoBias(ArgMinWithValueNet2(strategy1, strategy2, strategy3)))
  382. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  383. gen_inputs_and_compile_net_no_bias(net)
  384. def test_cross_batch():
  385. class Net(nn.Cell):
  386. def __init__(self, strategy1, strategy2, strategy3):
  387. super().__init__()
  388. self.mul1 = P.Mul().shard(strategy1)
  389. self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy2)
  390. self.reduce_mean = P.ReduceMean(keep_dims=False).shard(strategy3).add_prim_attr("cross_batch", True)
  391. def construct(self, x, y):
  392. out = self.mul1(x, y)
  393. out = self.reduce_sum(out, -1)
  394. out = self.reduce_mean(out, 0)
  395. return out
  396. context.set_auto_parallel_context(device_num=8, global_rank=0)
  397. strategy1 = ((4, 2), (4, 2))
  398. strategy2 = ((2, 1),)
  399. strategy3 = ((8,),)
  400. net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2, strategy3)))
  401. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  402. x = Tensor(np.ones([32, 64]), dtype=ms.float32)
  403. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  404. compile_net_no_bias(net, x, y)
  405. def test_cross_batch2():
  406. class Net(nn.Cell):
  407. def __init__(self, strategy1, strategy2, strategy3):
  408. super().__init__()
  409. self.mul1 = P.Mul().shard(strategy1)
  410. self.reduce_mean = P.ReduceMean(keep_dims=False).shard(strategy2)
  411. self.reduce_sum = P.ReduceSum(keep_dims=False).shard(strategy3).add_prim_attr("cross_batch", True)
  412. def construct(self, x, y):
  413. out = self.mul1(x, y)
  414. out = self.reduce_mean(out, -1)
  415. out = self.reduce_sum(out, 0)
  416. return out
  417. context.set_auto_parallel_context(device_num=8, global_rank=0)
  418. strategy1 = ((4, 2), (4, 2))
  419. strategy2 = ((2, 1),)
  420. strategy3 = ((8,),)
  421. net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2, strategy3)))
  422. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  423. x = Tensor(np.ones([32, 64]), dtype=ms.float32)
  424. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  425. compile_net_no_bias(net, x, y)
  426. def test_cross_batch_auto():
  427. class Net(nn.Cell):
  428. def __init__(self):
  429. super().__init__()
  430. self.mul1 = P.Mul()
  431. self.reduce_mean = P.ReduceMean(keep_dims=False)
  432. self.reduce_sum = P.ReduceSum(keep_dims=False).add_prim_attr("cross_batch", True)
  433. def construct(self, x, y):
  434. out = self.mul1(x, y)
  435. out = self.reduce_mean(out, -1)
  436. out = self.reduce_sum(out, 0)
  437. return out
  438. context.set_auto_parallel_context(device_num=8, global_rank=0)
  439. net = GradWrapNoBias(NetWithLossNoBias(Net()))
  440. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  441. x = Tensor(np.ones([32, 64]), dtype=ms.float32)
  442. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  443. compile_net_no_bias(net, x, y)
  444. def test_max_empty_tuple():
  445. class Net(nn.Cell):
  446. def __init__(self, strategy1, strategy2, strategy3):
  447. super().__init__()
  448. self.mul = P.Mul().shard(strategy1)
  449. self.reduce_max = P.ReduceMax(keep_dims=False).shard(strategy2)
  450. self.add = P.Add().shard(strategy3)
  451. def construct(self, x, y, b):
  452. out = self.mul(x, y)
  453. out = self.reduce_max(out)
  454. out = self.add(out, b)
  455. return out
  456. context.set_auto_parallel_context(device_num=8, global_rank=0)
  457. strategy1 = ((1, 4, 2), (1, 4, 2))
  458. strategy2 = ((4, 1, 2),)
  459. strategy3 = ((), (1, 1))
  460. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  461. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  462. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  463. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  464. b = Tensor(np.ones([128, 32]), dtype=ms.float32)
  465. compile_net(net, x, y, b)
  466. def test_any_mul():
  467. class Net(nn.Cell):
  468. def __init__(self, strategy1, strategy2):
  469. super().__init__()
  470. self.mul1 = P.Mul().shard(strategy1)
  471. self.reduce_any = P.ReduceAny(keep_dims=False).shard(strategy2)
  472. self.cast = P.Cast()
  473. def construct(self, x, y):
  474. out = self.mul1(x, y)
  475. out = self.cast(out, ms.bool_)
  476. out = self.reduce_any(out, 1)
  477. return out
  478. context.set_auto_parallel_context(device_num=64, global_rank=0)
  479. strategy1 = ((1, 8, 1), (1, 8, 1))
  480. strategy2 = ((1, 8, 1),)
  481. net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
  482. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  483. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  484. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  485. with pytest.raises(RuntimeError):
  486. compile_net_no_bias(net, x, y)
  487. def test_any_mul2():
  488. class Net(nn.Cell):
  489. def __init__(self, strategy1, strategy2):
  490. super().__init__()
  491. self.mul1 = P.Mul().shard(strategy1)
  492. self.reduce_any = P.ReduceAny(keep_dims=False).shard(strategy2)
  493. self.cast = P.Cast()
  494. def construct(self, x, y):
  495. out = self.mul1(x, y)
  496. out = self.cast(out, ms.bool_)
  497. out = self.reduce_any(out, -1)
  498. return out
  499. context.set_auto_parallel_context(device_num=64, global_rank=0)
  500. strategy1 = ((8, 1, 1), (8, 1, 1))
  501. strategy2 = ((8, 1, 1),)
  502. net = GradWrapNoBias(NetWithLossNoBias(Net(strategy1, strategy2)))
  503. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  504. x = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  505. y = Tensor(np.ones([128, 32, 64]), dtype=ms.float32)
  506. compile_net_no_bias(net, x, y)