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_reshape_unexpand.py 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # Copyright 2020 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. def compile_net(net, input_data, dev_num=8, parallel_mode="semi_auto_parallel"):
  40. context.set_auto_parallel_context(device_num=dev_num, global_rank=0)
  41. context.set_auto_parallel_context(parallel_mode=parallel_mode)
  42. net.set_auto_parallel()
  43. net.set_train()
  44. _cell_graph_executor.compile(net, input_data)
  45. def test_reshape_unexpand():
  46. """
  47. Feature: distribute operator reshape which cannot do normal redistribution in semi auto parallel.
  48. Description: rreshape-weight net in auto parallel.
  49. Expectation: compile done without error.
  50. """
  51. class Net(nn.Cell):
  52. def __init__(self):
  53. super().__init__()
  54. self.reshape = P.Reshape()
  55. self.mul = P.Mul().shard(((1, 8), (1, 1, 8)))
  56. self.mul_weight = Parameter(Tensor(np.ones([96, 128]), dtype=ms.float32), name="weight")
  57. def construct(self, x):
  58. weight = self.reshape(self.mul_weight, (1, 128, 96))
  59. out = self.mul(x, weight)
  60. return out
  61. x = Tensor(np.ones([128, 96]), dtype=ms.float32)
  62. net = GradWrap(NetWithLoss(Net()))
  63. compile_net(net, x)
  64. def test_reshape_unexpand_1():
  65. """
  66. Feature: distribute operator reshape which cannot do normal redistribution in semi auto parallel.
  67. Description: weight-reshape net in auto parallel.
  68. Expectation: compile done without error.
  69. """
  70. class Net(nn.Cell):
  71. def __init__(self):
  72. super().__init__()
  73. self.reshape = P.Reshape()
  74. self.mul = P.Mul().shard(((1, 1, 8), (1, 8)))
  75. self.mul_weight = Parameter(Tensor(np.ones([128, 96]), dtype=ms.float32), name="weight")
  76. def construct(self, data):
  77. x = self.reshape(self.mul_weight, (1, 128, 96))
  78. out = self.mul(x, self.mul_weight)
  79. return out
  80. x = Tensor(np.ones([128, 96]), dtype=ms.float32)
  81. net = GradWrap(NetWithLoss(Net()))
  82. compile_net(net, x)
  83. def test_reshape_unexpand_2():
  84. """
  85. Feature: distribute operator reshape which cannot do normal redistribution in semi auto parallel.
  86. Description: weight-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.mul = P.Mul().shard(((1, 4, 2), (4, 2)))
  94. self.mul_weight = Parameter(Tensor(np.ones([128, 96]), dtype=ms.float32), name="weight")
  95. def construct(self, data):
  96. x = self.reshape(self.mul_weight, (1, 128, 96))
  97. out = self.mul(x, self.mul_weight)
  98. return out
  99. x = Tensor(np.ones([128, 96]), dtype=ms.float32)
  100. net = GradWrap(NetWithLoss(Net()))
  101. compile_net(net, x)
  102. def test_reshape_unexpand_3():
  103. """
  104. Feature: distribute operator reshape which cannot do normal redistribution in semi auto parallel.
  105. Description: relu-reshape-relu net in auto parallel.
  106. Expectation: compile done without error.
  107. """
  108. class Net(nn.Cell):
  109. def __init__(self):
  110. super().__init__()
  111. self.reshape = P.Reshape()
  112. self.relu1 = P.ReLU().shard(((4, 1),))
  113. self.relu2 = P.ReLU().shard(((1, 4),))
  114. def construct(self, data):
  115. x = self.relu1(data)
  116. x = self.reshape(x, (3, 4))
  117. x = self.relu2(x)
  118. return x
  119. x = Tensor(np.ones([4, 3]), dtype=ms.float32)
  120. net = GradWrap(NetWithLoss(Net()))
  121. compile_net(net, x, dev_num=4)
  122. def test_reshape_unexpand_4():
  123. """
  124. Feature: distribute operator reshape which cannot do normal redistribution in semi auto parallel.
  125. Description: relu-reshape-relu net in auto parallel.
  126. Expectation: compile done without error.
  127. """
  128. class Net(nn.Cell):
  129. def __init__(self):
  130. super().__init__()
  131. self.reshape = P.Reshape()
  132. self.relu1 = P.ReLU().shard(((4, 1),))
  133. self.relu2 = P.ReLU().shard(((1, 2, 2),))
  134. def construct(self, data):
  135. x = self.relu1(data)
  136. x = self.reshape(x, (3, 2, 2))
  137. x = self.relu2(x)
  138. return x
  139. x = Tensor(np.ones([4, 3]), dtype=ms.float32)
  140. net = GradWrap(NetWithLoss(Net()))
  141. compile_net(net, x, dev_num=4)
  142. def test_reshape_unexpand_5():
  143. """
  144. Feature: distribute operator reshape which cannot do normal redistribution in semi auto parallel.
  145. Description: relu-reshape-relu net in auto parallel.
  146. Expectation: compile done without error.
  147. """
  148. class Net(nn.Cell):
  149. def __init__(self):
  150. super().__init__()
  151. self.reshape = P.Reshape()
  152. self.relu1 = P.ReLU().shard(((2, 2, 1),))
  153. self.relu2 = P.ReLU().shard(((1, 4),))
  154. def construct(self, data):
  155. x = self.relu1(data)
  156. x = self.reshape(x, (3, 4))
  157. x = self.relu2(x)
  158. return x
  159. x = Tensor(np.ones([2, 2, 3]), dtype=ms.float32)
  160. net = GradWrap(NetWithLoss(Net()))
  161. compile_net(net, x, dev_num=4)
  162. def test_reshape_unexpand_6():
  163. """
  164. Feature: distribute operator reshape which cannot do normal redistribution in semi auto parallel.
  165. Description: weight-reshape net in auto parallel.
  166. Expectation: compile done without error.
  167. """
  168. class Net(nn.Cell):
  169. def __init__(self):
  170. super().__init__()
  171. self.reshape = P.Reshape()
  172. self.relu1 = P.ReLU().shard(((2, 1),))
  173. self.relu2 = P.ReLU().shard(((1, 1, 4),))
  174. def construct(self, data):
  175. x = self.relu1(data)
  176. x = self.reshape(x, (1, 3, 4))
  177. x = self.relu2(x)
  178. return x
  179. x = Tensor(np.ones([4, 3]), dtype=ms.float32)
  180. net = GradWrap(NetWithLoss(Net()))
  181. compile_net(net, x, dev_num=4)
  182. def test_reshape_unexpand_7():
  183. """
  184. Feature: distribute operator reshape which cannot do normal redistribution in auto parallel.
  185. Description: reshape as net output in auto parallel.
  186. Expectation: compile done without error.
  187. """
  188. class Net(nn.Cell):
  189. def __init__(self, in_channel=3, out_channel=8, axis=1, input_shape=(32, 4, 110, -1),
  190. mul_size=(32, 1, 220, 220)):
  191. super().__init__()
  192. mul_np = np.full(mul_size, 0.5, dtype=np.float32)
  193. self.mul_weight = Parameter(Tensor(mul_np), name="mul_weight")
  194. self.mul = P.Mul()
  195. self.conv = nn.Conv2d(in_channels=in_channel, out_channels=out_channel,
  196. kernel_size=5, has_bias=True, weight_init='ones',
  197. bias_init='ones', pad_mode='valid')
  198. self.conv.conv2d.shard(((8, 1, 1, 1), (1, 1, 1, 1)))
  199. self.softmax = nn.Softmax(axis=axis)
  200. self.relu = nn.ReLU()
  201. self.reshape = P.Reshape()
  202. self.input_shape = input_shape
  203. def construct(self, inputs):
  204. x = self.conv(inputs)
  205. x = self.softmax(x)
  206. x = self.relu(x)
  207. x = self.mul(x, self.mul_weight)
  208. x = self.reshape(x, self.input_shape)
  209. return x
  210. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  211. x = Tensor(np.ones([32, 3, 224, 224]), dtype=ms.float32)
  212. net = GradWrap(NetWithLoss(Net()))
  213. compile_net(net, x, parallel_mode="auto_parallel")
  214. def test_reshape_unexpand_8():
  215. """
  216. Feature: distribute operator reshape which cannot do normal redistribution in auto parallel.
  217. Description: weight-reshape net in auto parallel.
  218. Expectation: compile done without error.
  219. """
  220. class Net(nn.Cell):
  221. def __init__(self):
  222. super().__init__()
  223. self.reshape = P.Reshape()
  224. self.mul = P.Mul().shard(((1, 4, 2), (4, 2)))
  225. self.mul_weight = Parameter(Tensor(np.ones([128, 96]), dtype=ms.float32), name="weight")
  226. def construct(self, data):
  227. x = self.reshape(self.mul_weight, (1, 128, 96))
  228. out = self.mul(x, self.mul_weight)
  229. return out
  230. x = Tensor(np.ones([128, 96]), dtype=ms.float32)
  231. net = GradWrap(NetWithLoss(Net()))
  232. compile_net(net, x, parallel_mode="auto_parallel")