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 9.0 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. class NetWithLoss(nn.Cell):
  25. def __init__(self, network):
  26. super(NetWithLoss, self).__init__()
  27. self.loss = VirtualLoss()
  28. self.network = network
  29. def construct(self, x):
  30. predict = self.network(x)
  31. return self.loss(predict)
  32. class GradWrap(nn.Cell):
  33. def __init__(self, network):
  34. super(GradWrap, self).__init__()
  35. self.network = network
  36. def construct(self, x):
  37. return C.grad_all(self.network)(x)
  38. # core dump, step_auto_parallel should SetInputs for transpose axis
  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. _executor.compile(net, x)
  57. def test_reshape_auto_1():
  58. class Net(nn.Cell):
  59. def __init__(self):
  60. super().__init__()
  61. self.relu = P.ReLU()
  62. self.reshape = P.Reshape()
  63. self.matmul = P.MatMul()
  64. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  65. def construct(self, x):
  66. out = self.relu(x)
  67. out = self.reshape(out, (64, 28))
  68. out = self.matmul(out, self.matmul_weight)
  69. return out
  70. size = 8
  71. context.set_auto_parallel_context(device_num=size, global_rank=0)
  72. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  73. net = GradWrap(NetWithLoss(Net()))
  74. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  75. net.set_auto_parallel()
  76. _executor.compile(net, x)
  77. def test_reshape_auto_2():
  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.add_weight = Parameter(Tensor(np.ones([128, 32]), dtype=ms.float32), name="weight1")
  85. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  86. def construct(self, x):
  87. out = self.relu(x)
  88. out = self.reshape(out, (64, 28))
  89. out = self.matmul(out, self.matmul_weight)
  90. out = self.reshape(out, (128, 32))
  91. out = out + self.add_weight
  92. return out
  93. size = 8
  94. context.set_auto_parallel_context(device_num=size, global_rank=0)
  95. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  96. net = GradWrap(NetWithLoss(Net()))
  97. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  98. net.set_auto_parallel()
  99. _executor.compile(net, x)
  100. def test_reshape_auto_3():
  101. class Net(nn.Cell):
  102. def __init__(self):
  103. super().__init__()
  104. self.relu = P.ReLU()
  105. self.reshape = P.Reshape()
  106. self.matmul = P.MatMul()
  107. self.matmul_weight = Parameter(Tensor(np.ones([28, 64]), dtype=ms.float32), name="weight")
  108. def construct(self, x):
  109. out = self.relu(x)
  110. out = self.matmul(out, self.matmul_weight)
  111. out = self.reshape(out, (8, 8, 8, 8))
  112. return out
  113. size = 8
  114. context.set_auto_parallel_context(device_num=size, global_rank=0)
  115. x = Tensor(np.ones([8 * size, 28]), dtype=ms.float32)
  116. net = GradWrap(NetWithLoss(Net()))
  117. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  118. net.set_auto_parallel()
  119. _executor.compile(net, x)
  120. def test_reshape_auto_4():
  121. class Net(nn.Cell):
  122. def __init__(self):
  123. super().__init__()
  124. self.relu = P.ReLU()
  125. self.reshape = P.Reshape()
  126. self.matmul = P.MatMul()
  127. self.matmul_weight = Parameter(Tensor(np.ones([28 * 64]), dtype=ms.float32), name="weight")
  128. def construct(self, x):
  129. out = self.relu(x)
  130. out = self.reshape(out, (64, 28))
  131. w = self.reshape(self.matmul_weight, (28, 64))
  132. out = self.matmul(out, w)
  133. return out
  134. size = 8
  135. context.set_auto_parallel_context(device_num=size, global_rank=0)
  136. x = Tensor(np.ones([8 * size, 28, 1, 1]), dtype=ms.float32)
  137. net = GradWrap(NetWithLoss(Net()))
  138. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  139. net.set_auto_parallel()
  140. _executor.compile(net, x)
  141. def test_reshape_auto_5():
  142. class NetWithLoss(nn.Cell):
  143. def __init__(self, network):
  144. super(NetWithLoss, self).__init__()
  145. self.loss = VirtualLoss()
  146. self.network = network
  147. def construct(self, x, y):
  148. predict = self.network(x, y)
  149. return self.loss(predict)
  150. class GradWrap(nn.Cell):
  151. def __init__(self, network):
  152. super(GradWrap, self).__init__()
  153. self.network = network
  154. def construct(self, x, y):
  155. return C.grad_all(self.network)(x, y)
  156. class Net(nn.Cell):
  157. def __init__(self):
  158. super().__init__()
  159. self.relu = P.ReLU()
  160. self.mul = P.Mul()
  161. self.reshape = P.Reshape()
  162. self.reduce_sum = P.ReduceSum()
  163. self.wide_w = Parameter(Tensor(np.ones([4, 1024 * 8, 64]), dtype=ms.float32), name="weight")
  164. def construct(self, x, y):
  165. mask = self.reshape(y, (4, 1024 * 8, 1))
  166. w_id = self.relu(x)
  167. wx = self.mul(w_id, mask)
  168. wide_out = self.reshape(self.reduce_sum(wx, 1), (-1, 1))
  169. deep_id = x + self.wide_w
  170. vx = self.mul(deep_id, mask)
  171. deep_in = self.reshape(vx, (-1, 1024 * 8 * 64))
  172. out = wide_out + deep_in
  173. return out
  174. size = 8
  175. context.set_auto_parallel_context(device_num=size, global_rank=0)
  176. x = Tensor(np.ones([4, 1024 * size, 1]), dtype=ms.float32)
  177. y = Tensor(np.ones([4, 1024 * size,]), dtype=ms.float32)
  178. net = GradWrap(NetWithLoss(Net()))
  179. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  180. net.set_auto_parallel()
  181. _executor.compile(net, x, y)
  182. def test_reshape_auto_6():
  183. class NetWithLoss(nn.Cell):
  184. def __init__(self, network):
  185. super(NetWithLoss, self).__init__()
  186. self.loss = VirtualLoss()
  187. self.network = network
  188. def construct(self, x, y):
  189. predict = self.network(x, y)
  190. return self.loss(predict)
  191. class GradWrap(nn.Cell):
  192. def __init__(self, network):
  193. super(GradWrap, self).__init__()
  194. self.network = network
  195. def construct(self, x, y):
  196. return C.grad_all(self.network)(x, y)
  197. class Net(nn.Cell):
  198. def __init__(self):
  199. super().__init__()
  200. self.relu = P.ReLU()
  201. self.mul = P.Mul()
  202. self.reshape = P.Reshape()
  203. self.reduce_mean = P.ReduceMean()
  204. self.wide_w = Parameter(Tensor(np.ones([4, 1024, 1]), dtype=ms.float32), name="weight")
  205. def construct(self, x, y):
  206. out1 = x + self.wide_w
  207. w = self.reshape(self.wide_w, (4, 1024))
  208. out1 = self.reduce_mean(out1, 1)
  209. out1 = out1 - w
  210. out2 = self.mul(y, w)
  211. out = out1 + out2
  212. return out
  213. size = 8
  214. context.set_auto_parallel_context(device_num=size, global_rank=0)
  215. x = Tensor(np.ones([4, 1024, 1]), dtype=ms.float32)
  216. y = Tensor(np.ones([4, 1024,]), dtype=ms.float32)
  217. net = GradWrap(NetWithLoss(Net()))
  218. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  219. net.set_auto_parallel()
  220. _executor.compile(net, x, y)