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