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_hybird_parallel_activation.py 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. from mindspore import context
  17. import mindspore.nn as nn
  18. from mindspore.ops import operations as P
  19. from mindspore import Tensor
  20. from tests.ut.python.ops.test_math_ops import VirtualLoss
  21. from mindspore.common.api import _executor
  22. from mindspore.ops import composite as C
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = VirtualLoss()
  27. self.network = network
  28. def construct(self, x, y, b):
  29. predict = self.network(x, y, b)
  30. return self.loss(predict)
  31. class GradWrap(nn.Cell):
  32. def __init__(self, network):
  33. super(GradWrap, self).__init__()
  34. self.network = network
  35. def construct(self, x, y, b):
  36. return C.grad_all(self.network)(x, y, b)
  37. def test_matmul_tanh():
  38. class Net(nn.Cell):
  39. def __init__(self, strategy1, strategy2, strategy3):
  40. super().__init__()
  41. self.matmul1 = P.MatMul().set_strategy(strategy1)
  42. self.matmul2 = P.MatMul().set_strategy(strategy2)
  43. self.tanh = P.Tanh().set_strategy(strategy3)
  44. def construct(self, x, y, b):
  45. out = self.tanh(self.matmul1(x, y))
  46. out = self.matmul2(out, b)
  47. return out
  48. strategy1 = ((16, 1), (1, 1))
  49. strategy2 = ((1, 1), (1, 16))
  50. strategy3 = ((4, 4), )
  51. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  52. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  53. context.set_auto_parallel_context(device_num=16, global_rank=0)
  54. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  55. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  56. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  57. _executor.compile(net, x, y, b)
  58. def test_matmul_activation():
  59. class Net(nn.Cell):
  60. def __init__(self, strategy1, strategy2, strategy3):
  61. super().__init__()
  62. self.matmul1 = P.MatMul().set_strategy(strategy1)
  63. self.matmul2 = P.MatMul().set_strategy(strategy2)
  64. self.activation = P.ReLU().set_strategy(strategy3)
  65. def construct(self, x, y, b):
  66. out = self.activation(self.matmul1(x, y))
  67. out = self.matmul2(out, b)
  68. return out
  69. strategy1 = ((16, 1), (1, 1))
  70. strategy2 = ((1, 1), (1, 16))
  71. strategy3 = ((4, 4), )
  72. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  73. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  74. context.set_auto_parallel_context(device_num=16, global_rank=0)
  75. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  76. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  77. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  78. _executor.compile(net, x, y, b)
  79. def test_matmul_softmax():
  80. class Net(nn.Cell):
  81. def __init__(self, strategy1, strategy2, strategy3):
  82. super().__init__()
  83. self.matmul1 = P.MatMul().set_strategy(strategy1)
  84. self.matmul2 = P.MatMul().set_strategy(strategy2)
  85. self.softmax = P.Softmax().set_strategy(strategy3)
  86. def construct(self, x, y, b):
  87. out = self.softmax(self.matmul1(x, y))
  88. out = self.matmul2(out, b)
  89. return out
  90. strategy1 = ((16, 1), (1, 1))
  91. strategy2 = ((1, 1), (1, 16))
  92. strategy3 = ((16, 1), )
  93. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  94. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  95. context.set_auto_parallel_context(device_num=16, global_rank=0)
  96. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  97. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  98. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  99. _executor.compile(net, x, y, b)
  100. def test_matmul_logsoftmax():
  101. class Net(nn.Cell):
  102. def __init__(self, strategy1, strategy2, strategy3):
  103. super().__init__()
  104. self.matmul1 = P.MatMul().set_strategy(strategy1)
  105. self.matmul2 = P.MatMul().set_strategy(strategy2)
  106. self.logsoftmax = P.LogSoftmax().set_strategy(strategy3)
  107. def construct(self, x, y, b):
  108. out = self.logsoftmax(self.matmul1(x, y))
  109. out = self.matmul2(out, b)
  110. return out
  111. strategy1 = ((4, 2), (2, 2))
  112. strategy2 = ((2, 4), (4, 2))
  113. strategy3 = ((16, 1), )
  114. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  115. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  116. context.set_auto_parallel_context(device_num=16, global_rank=0)
  117. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  118. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  119. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  120. _executor.compile(net, x, y, b)
  121. def test_activations():
  122. class Net(nn.Cell):
  123. def __init__(self, strategy1, strategy2, strategy3):
  124. super().__init__()
  125. self.matmul1 = P.MatMul().set_strategy(strategy1)
  126. self.matmul2 = P.MatMul().set_strategy(strategy2)
  127. self.gelu = P.Gelu().set_strategy(strategy3)
  128. self.tanh = P.Tanh().set_strategy(strategy3)
  129. self.softmax = P.Softmax().set_strategy(strategy3)
  130. self.logsoftmax = P.LogSoftmax().set_strategy(strategy3)
  131. def construct(self, x, y, b):
  132. out = self.gelu(self.tanh(self.matmul1(x, y)))
  133. out = self.logsoftmax(self.softmax(self.matmul2(out, b)))
  134. return out
  135. strategy1 = ((1, 2), (2, 2))
  136. strategy2 = ((2, 2), (2, 1))
  137. strategy3 = ((4, 1), )
  138. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  139. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  140. context.set_auto_parallel_context(device_num=4, global_rank=0)
  141. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  142. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  143. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  144. _executor.compile(net, x, y, b)
  145. def test_activations_repeated_calculation():
  146. class Net(nn.Cell):
  147. def __init__(self, strategy1, strategy2, strategy3, strategy4, strategy5, strategy6):
  148. super().__init__()
  149. self.matmul1 = P.MatMul().set_strategy(strategy1)
  150. self.matmul2 = P.MatMul().set_strategy(strategy2)
  151. self.gelu = P.Gelu().set_strategy(strategy3)
  152. self.tanh = P.Tanh().set_strategy(strategy4)
  153. self.softmax = P.Softmax().set_strategy(strategy5)
  154. self.logsoftmax = P.LogSoftmax().set_strategy(strategy6)
  155. def construct(self, x, y, b):
  156. out = self.gelu(self.tanh(self.matmul1(x, y)))
  157. out = self.logsoftmax(self.softmax(self.matmul2(out, b)))
  158. return out
  159. strategy1 = ((2, 4), (4, 8))
  160. strategy2 = ((2, 2), (2, 1))
  161. strategy3 = ((2, 1), )
  162. strategy4 = ((2, 2), )
  163. strategy5 = ((4, 1), )
  164. strategy6 = ((8, 1), )
  165. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3, strategy4, strategy5, strategy6)))
  166. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  167. context.set_auto_parallel_context(device_num=64, global_rank=0)
  168. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  169. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  170. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  171. _executor.compile(net, x, y, b)
  172. def test_activations_axis_tuple():
  173. class Net(nn.Cell):
  174. def __init__(self, strategy1, strategy2, strategy3, strategy4, strategy5, strategy6):
  175. super().__init__()
  176. self.matmul1 = P.MatMul().set_strategy(strategy1)
  177. self.matmul2 = P.MatMul().set_strategy(strategy2)
  178. self.gelu = P.Gelu().set_strategy(strategy3)
  179. self.tanh = P.Tanh().set_strategy(strategy4)
  180. self.softmax = P.Softmax(axis=(0, 1)).set_strategy(strategy5)
  181. self.logsoftmax = P.LogSoftmax().set_strategy(strategy6)
  182. def construct(self, x, y, b):
  183. out = self.gelu(self.tanh(self.matmul1(x, y)))
  184. out = self.logsoftmax(self.softmax(self.matmul2(out, b)))
  185. return out
  186. strategy1 = ((2, 4), (4, 8))
  187. strategy2 = ((2, 2), (2, 1))
  188. strategy3 = ((2, 1), )
  189. strategy4 = ((2, 2), )
  190. strategy5 = ((1, 1), )
  191. strategy6 = ((8, 1), )
  192. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3, strategy4, strategy5, strategy6)))
  193. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  194. context.set_auto_parallel_context(device_num=64, global_rank=0)
  195. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  196. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  197. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  198. _executor.compile(net, x, y, b)