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