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_auto_parallel_reshape.py 14 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 _cell_graph_executor
  20. from mindspore.common.parameter import Parameter
  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 NetWithLoss(nn.Cell):
  26. def __init__(self, network):
  27. super(NetWithLoss, self).__init__()
  28. self.loss = VirtualLoss()
  29. self.network = network
  30. def construct(self, x):
  31. predict = self.network(x)
  32. return self.loss(predict)
  33. class GradWrap(nn.Cell):
  34. def __init__(self, network):
  35. super(GradWrap, self).__init__()
  36. self.network = network
  37. def construct(self, x):
  38. return grad_all(self.network)(x)
  39. class NetWithLossTwoInput(nn.Cell):
  40. def __init__(self, network):
  41. super(NetWithLossTwoInput, self).__init__()
  42. self.loss = VirtualLoss()
  43. self.network = network
  44. def construct(self, x, y):
  45. predict = self.network(x, y)
  46. return self.loss(predict)
  47. class NetWithReduceLoss(nn.Cell):
  48. def __init__(self, network):
  49. super(NetWithReduceLoss, self).__init__()
  50. self.mean = P.ReduceMean(keep_dims=False)
  51. self.network = network
  52. def construct(self, x, y):
  53. predict = self.network(x, y)
  54. return self.mean(predict, ())
  55. class GradWrapTwoInput(nn.Cell):
  56. def __init__(self, network):
  57. super(GradWrapTwoInput, self).__init__()
  58. self.network = network
  59. def construct(self, x, y):
  60. return grad_all(self.network)(x, y)
  61. def compile_graph(net, parallel_mode, device_num, x):
  62. context.set_auto_parallel_context(device_num=device_num, global_rank=0, parallel_mode=parallel_mode)
  63. net.set_auto_parallel()
  64. net.set_train()
  65. _cell_graph_executor.compile(net, x)
  66. def compile_graph_two_input(net, parallel_mode, device_num, x, y):
  67. context.set_auto_parallel_context(device_num=device_num, global_rank=0, parallel_mode=parallel_mode)
  68. net.set_auto_parallel()
  69. net.set_train()
  70. _cell_graph_executor.compile(net, x, y)
  71. def test_reshape_matmul():
  72. """
  73. Feature: distribute operator reshape in auto parallel.
  74. Description: reshape - matmul net in auto parallel.
  75. Expectation: compile done without error.
  76. """
  77. class Net(nn.Cell):
  78. def __init__(self):
  79. super().__init__()
  80. self.reshape = P.Reshape()
  81. self.matmul = P.MatMul()
  82. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  83. def construct(self, x):
  84. out = self.reshape(x, (64, 28))
  85. out = self.matmul(out, self.matmul_weight)
  86. return out
  87. size = 8
  88. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  89. net = GradWrap(NetWithLoss(Net()))
  90. compile_graph(net, "auto_parallel", size, x)
  91. def test_reshape_reshape():
  92. """
  93. Feature: distribute operator reshape in auto parallel.
  94. Description: reshape - reshape net in auto parallel.
  95. Expectation: compile done without error.
  96. """
  97. class Net(nn.Cell):
  98. def __init__(self):
  99. super().__init__()
  100. self.reshape = P.Reshape()
  101. self.relu = P.ReLU()
  102. def construct(self, x):
  103. x = self.relu(x)
  104. out = self.reshape(x, (64, 28))
  105. out = self.reshape(out, (64, 28, 1))
  106. return out
  107. size = 8
  108. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  109. net = GradWrap(NetWithLoss(Net()))
  110. compile_graph(net, "auto_parallel", size, x)
  111. def test_reshape_auto_1():
  112. """
  113. Feature: distribute operator reshape in auto parallel.
  114. Description: relu - reshape - matmul net in auto parallel.
  115. Expectation: compile done without error.
  116. """
  117. class Net(nn.Cell):
  118. def __init__(self):
  119. super().__init__()
  120. self.relu = P.ReLU()
  121. self.reshape = P.Reshape()
  122. self.matmul = P.MatMul()
  123. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  124. def construct(self, x):
  125. out = self.relu(x)
  126. out = self.reshape(out, (64, 28))
  127. out = self.matmul(out, self.matmul_weight)
  128. return out
  129. size = 8
  130. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  131. net = GradWrap(NetWithLoss(Net()))
  132. compile_graph(net, "auto_parallel", size, x)
  133. def test_reshape_auto_2():
  134. """
  135. Feature: distribute operator reshape in auto parallel.
  136. Description: reshape - matmul -reshape net in auto parallel.
  137. Expectation: compile done without error.
  138. """
  139. class Net(nn.Cell):
  140. def __init__(self):
  141. super().__init__()
  142. self.relu = P.ReLU()
  143. self.reshape = P.Reshape()
  144. self.matmul = P.MatMul()
  145. self.add_weight = Parameter(Tensor(np.ones([128, 32]), dtype=ms.float32), name="weight1")
  146. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  147. def construct(self, x):
  148. out = self.relu(x)
  149. out = self.reshape(out, (64, 28))
  150. out = self.matmul(out, self.matmul_weight)
  151. out = self.reshape(out, (128, 32))
  152. out = out + self.add_weight
  153. return out
  154. size = 8
  155. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  156. net = GradWrap(NetWithLoss(Net()))
  157. compile_graph(net, "auto_parallel", size, x)
  158. def test_reshape_auto_3():
  159. """
  160. Feature: distribute operator reshape in auto parallel.
  161. Description: reshape as last node net in auto parallel.
  162. Expectation: compile done without error.
  163. """
  164. class Net(nn.Cell):
  165. def __init__(self):
  166. super().__init__()
  167. self.relu = P.ReLU()
  168. self.reshape = P.Reshape()
  169. self.matmul = P.MatMul()
  170. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  171. def construct(self, x):
  172. out = self.relu(x)
  173. out = self.matmul(out, self.matmul_weight)
  174. out = self.reshape(out, (8, 8, 8, 8))
  175. return out
  176. size = 8
  177. x = Tensor(np.ones([8 * size, 28]), dtype=ms.float32)
  178. net = GradWrap(NetWithLoss(Net()))
  179. compile_graph(net, "auto_parallel", size, x)
  180. def test_reshape_auto_4():
  181. """
  182. Feature: distribute operator reshape in auto parallel.
  183. Description: reshape - reshape net in auto parallel.
  184. Expectation: compile done without error.
  185. """
  186. class Net(nn.Cell):
  187. def __init__(self):
  188. super().__init__()
  189. self.relu = P.ReLU()
  190. self.reshape = P.Reshape()
  191. self.matmul = P.MatMul()
  192. self.matmul_weight = Parameter(Tensor(np.ones([28 * 64]), dtype=ms.float32), name="weight")
  193. def construct(self, x):
  194. out = self.relu(x)
  195. out = self.reshape(out, (64, 28))
  196. w = self.reshape(self.matmul_weight, (28, 64))
  197. out = self.matmul(out, w)
  198. return out
  199. size = 8
  200. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  201. net = GradWrap(NetWithLoss(Net()))
  202. compile_graph(net, "auto_parallel", size, x)
  203. def test_reshape_auto_5():
  204. """
  205. Feature: distribute operator reshape in auto parallel.
  206. Description: modify wide&deep small net in auto parallel.
  207. Expectation: compile done without error.
  208. """
  209. class Net(nn.Cell):
  210. def __init__(self):
  211. super().__init__()
  212. self.relu = P.ReLU()
  213. self.mul = P.Mul()
  214. self.reshape = P.Reshape()
  215. self.reduce_sum = P.ReduceSum()
  216. self.wide_w = Parameter(Tensor(np.ones([4, 1024 * 8, 64]), dtype=ms.float32), name="weight")
  217. def construct(self, x, y):
  218. mask = self.reshape(y, (4, 1024 * 8, 1))
  219. w_id = self.relu(x)
  220. wx = self.mul(w_id, mask)
  221. wide_out = self.reshape(self.reduce_sum(wx, 1), (-1, 1))
  222. deep_id = x + self.wide_w
  223. vx = self.mul(deep_id, mask)
  224. deep_in = self.reshape(vx, (-1, 1024 * 8 * 64))
  225. out = wide_out + deep_in
  226. return out
  227. size = 8
  228. context.set_auto_parallel_context(dataset_strategy="full_batch")
  229. x = Tensor(np.ones([4, 1024 * size, 1]), dtype=ms.float32)
  230. y = Tensor(np.ones([4, 1024 * size,]), dtype=ms.float32)
  231. net = GradWrapTwoInput(NetWithLossTwoInput(Net()))
  232. compile_graph_two_input(net, "auto_parallel", size, x, y)
  233. def test_reshape_auto_6():
  234. """
  235. Feature: distribute operator reshape in auto parallel.
  236. Description: modify wide&deep small net in auto parallel.
  237. Expectation: compile done without error.
  238. """
  239. class Net(nn.Cell):
  240. def __init__(self):
  241. super().__init__()
  242. self.relu = P.ReLU()
  243. self.mul = P.Mul()
  244. self.reshape = P.Reshape()
  245. self.reduce_mean = P.ReduceMean()
  246. self.wide_w = Parameter(Tensor(np.ones([4, 1024, 1]), dtype=ms.float32), name="weight")
  247. def construct(self, x, y):
  248. out1 = x + self.wide_w
  249. w = self.reshape(self.wide_w, (4, 1024))
  250. out1 = self.reduce_mean(out1, 1)
  251. out1 = out1 - w
  252. out2 = self.mul(y, w)
  253. out = out1 + out2
  254. return out
  255. size = 8
  256. context.set_auto_parallel_context(dataset_strategy="full_batch")
  257. x = Tensor(np.ones([4, 1024, 1]), dtype=ms.float32)
  258. y = Tensor(np.ones([4, 1024,]), dtype=ms.float32)
  259. net = GradWrapTwoInput(NetWithLossTwoInput(Net()))
  260. compile_graph_two_input(net, "auto_parallel", size, x, y)
  261. def test_reshape_auto_7():
  262. """
  263. Feature: distribute operator reshape in auto parallel.
  264. Description: reshape weight net in semi auto parallel.
  265. Expectation: compile done without error.
  266. """
  267. class Net(nn.Cell):
  268. def __init__(self):
  269. super().__init__()
  270. self.reshape = P.Reshape()
  271. self.mul = P.Mul().shard(((1, 2, 4), (2, 4)))
  272. self.mul_weight = Parameter(Tensor(np.ones([128, 96]), dtype=ms.float32), name="weight")
  273. def construct(self, x):
  274. weight = self.reshape(self.mul_weight, (1, 128, 96))
  275. out = self.mul(weight, self.mul_weight)
  276. return out
  277. size = 8
  278. x = Tensor(np.ones([128, 28]), dtype=ms.float32)
  279. net = GradWrap(NetWithLoss(Net()))
  280. compile_graph(net, "semi_auto_parallel", size, x)
  281. def test_reshape_depend_reshape():
  282. """
  283. Feature: distribute operator reshape in auto parallel.
  284. Description: reshape - depend -reshape net in semi auto parallel.
  285. Expectation: compile done without error.
  286. """
  287. class Net(nn.Cell):
  288. def __init__(self):
  289. super().__init__()
  290. self.reshape1 = P.Reshape()
  291. self.reshape2 = P.Reshape()
  292. self.relu = P.ReLU()
  293. self.depend = P.Depend()
  294. self.mul = P.Mul().shard(((2, 4), (2, 4)))
  295. self.mul_weight = Parameter(Tensor(np.ones([128, 96]), dtype=ms.float32), name="weight")
  296. self.add = P.Add().shard(((4, 2), (4, 2)))
  297. def construct(self, x, y):
  298. out1 = self.mul(x, self.mul_weight)
  299. y = self.relu(y)
  300. out2 = self.reshape1(y, (96, 32, 4))
  301. out3 = self.depend(out2, out1)
  302. out3 = self.reshape2(out3, (128, 96))
  303. out = out1 + out3
  304. return out
  305. size = 8
  306. x = Tensor(np.ones([128, 96]), dtype=ms.float32)
  307. y = Tensor(np.ones([256, 48]), dtype=ms.float32)
  308. net = GradWrapTwoInput(NetWithReduceLoss(Net()))
  309. compile_graph_two_input(net, "semi_auto_parallel", size, x, y)
  310. net_auto = GradWrapTwoInput(NetWithReduceLoss(Net()))
  311. compile_graph_two_input(net_auto, "auto_parallel", size, x, y)
  312. def test_appeq_reshape():
  313. """
  314. Feature: distribute operator reshape in auto parallel.
  315. Description: app_eq - reshape - cast - relu net in semi auto parallel / auto parallel.
  316. Expectation: compile done without error.
  317. """
  318. class Net(nn.Cell):
  319. def __init__(self):
  320. super().__init__()
  321. self.app_eq = P.ApproximateEqual(2.)
  322. self.reshape = P.Reshape()
  323. self.cast = P.Cast()
  324. self.relu = P.ReLU().shard(((1, 8),))
  325. def construct(self, x, y):
  326. out1 = self.app_eq(x, y)
  327. out2 = self.reshape(out1, (64, 192))
  328. out3 = self.cast(out2, ms.int32)
  329. out = self.relu(out3)
  330. return out
  331. size = 8
  332. x = Tensor(np.ones([128, 96]), dtype=ms.float32)
  333. y = Tensor(np.ones([128, 96]), dtype=ms.float32)
  334. net = GradWrapTwoInput(NetWithReduceLoss(Net()))
  335. compile_graph_two_input(net, "semi_auto_parallel", size, x, y)
  336. net_auto = GradWrapTwoInput(NetWithReduceLoss(Net()))
  337. context.set_auto_parallel_context(search_mode="recursive_programming")
  338. compile_graph_two_input(net_auto, "auto_parallel", size, x, y)