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 11 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.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. def test_reshape_matmul():
  40. class Net(nn.Cell):
  41. def __init__(self):
  42. super().__init__()
  43. self.reshape = P.Reshape()
  44. self.matmul = P.MatMul()
  45. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  46. def construct(self, x):
  47. out = self.reshape(x, (64, 28))
  48. out = self.matmul(out, self.matmul_weight)
  49. return out
  50. size = 8
  51. context.set_auto_parallel_context(device_num=size, global_rank=0)
  52. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  53. net = GradWrap(NetWithLoss(Net()))
  54. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  55. net.set_auto_parallel()
  56. net.set_train()
  57. _executor.compile(net, x)
  58. def test_reshape_reshape():
  59. class Net(nn.Cell):
  60. def __init__(self):
  61. super().__init__()
  62. self.reshape = P.Reshape()
  63. self.relu = P.ReLU()
  64. def construct(self, x):
  65. x = self.relu(x)
  66. out = self.reshape(x, (64, 28))
  67. out = self.reshape(out, (64, 28, 1))
  68. return out
  69. size = 8
  70. context.set_auto_parallel_context(device_num=size, global_rank=0)
  71. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  72. net = GradWrap(NetWithLoss(Net()))
  73. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  74. net.set_auto_parallel()
  75. net.set_train()
  76. _executor.compile(net, x)
  77. def test_reshape_auto_1():
  78. class Net(nn.Cell):
  79. def __init__(self):
  80. super().__init__()
  81. self.relu = P.ReLU()
  82. self.reshape = P.Reshape()
  83. self.matmul = P.MatMul()
  84. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  85. def construct(self, x):
  86. out = self.relu(x)
  87. out = self.reshape(out, (64, 28))
  88. out = self.matmul(out, self.matmul_weight)
  89. return out
  90. size = 8
  91. context.set_auto_parallel_context(device_num=size, global_rank=0)
  92. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  93. net = GradWrap(NetWithLoss(Net()))
  94. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  95. net.set_auto_parallel()
  96. net.set_train()
  97. _executor.compile(net, x)
  98. def test_reshape_auto_2():
  99. class Net(nn.Cell):
  100. def __init__(self):
  101. super().__init__()
  102. self.relu = P.ReLU()
  103. self.reshape = P.Reshape()
  104. self.matmul = P.MatMul()
  105. self.add_weight = Parameter(Tensor(np.ones([128, 32]), dtype=ms.float32), name="weight1")
  106. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  107. def construct(self, x):
  108. out = self.relu(x)
  109. out = self.reshape(out, (64, 28))
  110. out = self.matmul(out, self.matmul_weight)
  111. out = self.reshape(out, (128, 32))
  112. out = out + self.add_weight
  113. return out
  114. size = 8
  115. context.set_auto_parallel_context(device_num=size, global_rank=0)
  116. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  117. net = GradWrap(NetWithLoss(Net()))
  118. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  119. net.set_auto_parallel()
  120. net.set_train()
  121. _executor.compile(net, x)
  122. def test_reshape_auto_3():
  123. class Net(nn.Cell):
  124. def __init__(self):
  125. super().__init__()
  126. self.relu = P.ReLU()
  127. self.reshape = P.Reshape()
  128. self.matmul = P.MatMul()
  129. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  130. def construct(self, x):
  131. out = self.relu(x)
  132. out = self.matmul(out, self.matmul_weight)
  133. out = self.reshape(out, (8, 8, 8, 8))
  134. return out
  135. size = 8
  136. context.set_auto_parallel_context(device_num=size, global_rank=0)
  137. x = Tensor(np.ones([8 * size, 28]), dtype=ms.float32)
  138. net = GradWrap(NetWithLoss(Net()))
  139. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  140. net.set_auto_parallel()
  141. net.set_train()
  142. _executor.compile(net, x)
  143. def test_reshape_auto_4():
  144. class Net(nn.Cell):
  145. def __init__(self):
  146. super().__init__()
  147. self.relu = P.ReLU()
  148. self.reshape = P.Reshape()
  149. self.matmul = P.MatMul()
  150. self.matmul_weight = Parameter(Tensor(np.ones([28 * 64]), dtype=ms.float32), name="weight")
  151. def construct(self, x):
  152. out = self.relu(x)
  153. out = self.reshape(out, (64, 28))
  154. w = self.reshape(self.matmul_weight, (28, 64))
  155. out = self.matmul(out, w)
  156. return out
  157. size = 8
  158. context.set_auto_parallel_context(device_num=size, global_rank=0)
  159. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  160. net = GradWrap(NetWithLoss(Net()))
  161. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  162. net.set_auto_parallel()
  163. net.set_train()
  164. _executor.compile(net, x)
  165. def test_reshape_auto_5():
  166. class NetWithLoss5(nn.Cell):
  167. def __init__(self, network):
  168. super(NetWithLoss5, self).__init__()
  169. self.loss = VirtualLoss()
  170. self.network = network
  171. def construct(self, x, y):
  172. predict = self.network(x, y)
  173. return self.loss(predict)
  174. class GradWrap5(nn.Cell):
  175. def __init__(self, network):
  176. super(GradWrap5, self).__init__()
  177. self.network = network
  178. def construct(self, x, y):
  179. return grad_all(self.network)(x, y)
  180. class Net(nn.Cell):
  181. def __init__(self):
  182. super().__init__()
  183. self.relu = P.ReLU()
  184. self.mul = P.Mul()
  185. self.reshape = P.Reshape()
  186. self.reduce_sum = P.ReduceSum()
  187. self.wide_w = Parameter(Tensor(np.ones([4, 1024 * 8, 64]), dtype=ms.float32), name="weight")
  188. def construct(self, x, y):
  189. mask = self.reshape(y, (4, 1024 * 8, 1))
  190. w_id = self.relu(x)
  191. wx = self.mul(w_id, mask)
  192. wide_out = self.reshape(self.reduce_sum(wx, 1), (-1, 1))
  193. deep_id = x + self.wide_w
  194. vx = self.mul(deep_id, mask)
  195. deep_in = self.reshape(vx, (-1, 1024 * 8 * 64))
  196. out = wide_out + deep_in
  197. return out
  198. size = 8
  199. context.set_auto_parallel_context(device_num=size, global_rank=0)
  200. x = Tensor(np.ones([4, 1024 * size, 1]), dtype=ms.float32)
  201. y = Tensor(np.ones([4, 1024 * size,]), dtype=ms.float32)
  202. net = GradWrap5(NetWithLoss5(Net()))
  203. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  204. net.set_auto_parallel()
  205. net.set_train()
  206. _executor.compile(net, x, y)
  207. def test_reshape_auto_6():
  208. class NetWithLoss6(nn.Cell):
  209. def __init__(self, network):
  210. super(NetWithLoss6, self).__init__()
  211. self.loss = VirtualLoss()
  212. self.network = network
  213. def construct(self, x, y):
  214. predict = self.network(x, y)
  215. return self.loss(predict)
  216. class GradWrap6(nn.Cell):
  217. def __init__(self, network):
  218. super(GradWrap6, self).__init__()
  219. self.network = network
  220. def construct(self, x, y):
  221. return grad_all(self.network)(x, y)
  222. class Net(nn.Cell):
  223. def __init__(self):
  224. super().__init__()
  225. self.relu = P.ReLU()
  226. self.mul = P.Mul()
  227. self.reshape = P.Reshape()
  228. self.reduce_mean = P.ReduceMean()
  229. self.wide_w = Parameter(Tensor(np.ones([4, 1024, 1]), dtype=ms.float32), name="weight")
  230. def construct(self, x, y):
  231. out1 = x + self.wide_w
  232. w = self.reshape(self.wide_w, (4, 1024))
  233. out1 = self.reduce_mean(out1, 1)
  234. out1 = out1 - w
  235. out2 = self.mul(y, w)
  236. out = out1 + out2
  237. return out
  238. size = 8
  239. context.set_auto_parallel_context(device_num=size, global_rank=0)
  240. x = Tensor(np.ones([4, 1024, 1]), dtype=ms.float32)
  241. y = Tensor(np.ones([4, 1024,]), dtype=ms.float32)
  242. net = GradWrap6(NetWithLoss6(Net()))
  243. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  244. net.set_auto_parallel()
  245. net.set_train()
  246. _executor.compile(net, x, y)
  247. def test_reshape_auto_7():
  248. class Net(nn.Cell):
  249. def __init__(self):
  250. super().__init__()
  251. self.reshape = P.Reshape()
  252. self.mul = P.Mul().shard(((1, 2, 4), (2, 4)))
  253. self.mul_weight = Parameter(Tensor(np.ones([128, 96]), dtype=ms.float32), name="weight")
  254. def construct(self, x):
  255. weight = self.reshape(self.mul_weight, (1, 128, 96))
  256. out = self.mul(weight, self.mul_weight)
  257. return out
  258. size = 8
  259. context.set_auto_parallel_context(device_num=size, global_rank=0)
  260. x = Tensor(np.ones([128, 28]), dtype=ms.float32)
  261. net = GradWrap(NetWithLoss(Net()))
  262. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  263. net.set_auto_parallel()
  264. net.set_train()
  265. _executor.compile(net, x)