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