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