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_two_matmul.py 6.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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.ops import composite as C
  21. from mindspore.ops import operations as P
  22. from tests.ut.python.ops.test_math_ops import VirtualLoss
  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 compile_net(net, x, y, b):
  38. net.set_auto_parallel()
  39. _executor.compile(net, x, y, b)
  40. # model_parallel test
  41. def test_two_matmul():
  42. class Net(nn.Cell):
  43. def __init__(self, strategy1, strategy2):
  44. super().__init__()
  45. self.matmul1 = P.MatMul().set_strategy(strategy1)
  46. self.matmul2 = P.MatMul().set_strategy(strategy2)
  47. def construct(self, x, y, b):
  48. out = self.matmul1(x, y)
  49. out = self.matmul2(out, b)
  50. return out
  51. context.set_auto_parallel_context(device_num=8, global_rank=0, mirror_mean=True)
  52. strategy1 = ((4, 2), (2, 1))
  53. strategy2 = ((2, 4), (4, 1))
  54. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  55. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  56. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  57. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  58. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  59. compile_net(net, x, y, b)
  60. def test_two_matmul_repeated_calculation1():
  61. class Net(nn.Cell):
  62. def __init__(self, strategy1, strategy2):
  63. super().__init__()
  64. self.matmul1 = P.MatMul().set_strategy(strategy1)
  65. self.matmul2 = P.MatMul().set_strategy(strategy2)
  66. def construct(self, x, y, b):
  67. out = self.matmul1(x, y)
  68. out = self.matmul2(out, b)
  69. return out
  70. context.set_auto_parallel_context(device_num=64, global_rank=5, mirror_mean=True)
  71. strategy1 = ((2, 4), (4, 8))
  72. strategy2 = ((1, 1), (1, 1))
  73. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  74. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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. compile_net(net, x, y, b)
  79. def test_two_matmul_repeated_calculation2():
  80. class Net(nn.Cell):
  81. def __init__(self, strategy1, strategy2):
  82. super().__init__()
  83. self.matmul1 = P.MatMul().set_strategy(strategy1)
  84. self.matmul2 = P.MatMul().set_strategy(strategy2)
  85. def construct(self, x, y, b):
  86. out = self.matmul1(x, y)
  87. out = self.matmul2(out, b)
  88. return out
  89. context.set_auto_parallel_context(device_num=64, global_rank=15)
  90. strategy1 = ((2, 4), (4, 8))
  91. strategy2 = ((2, 2), (2, 1))
  92. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  93. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  94. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  95. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  96. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  97. compile_net(net, x, y, b)
  98. def test_matmul_forward_reduce_scatter():
  99. class Net(nn.Cell):
  100. def __init__(self, strategy1, strategy2):
  101. super().__init__()
  102. self.matmul = P.MatMul().set_strategy(strategy1)
  103. self.matmul.add_prim_attr("forward_reduce_scatter", True)
  104. self.mul = P.Mul().set_strategy(strategy2)
  105. def construct(self, x, y, b):
  106. out = self.matmul(x, y)
  107. out = self.mul(out, b)
  108. return out
  109. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  110. context.set_context(save_graphs=True)
  111. strategy1 = ((2, 2), (2, 2))
  112. strategy2 = ((4, 2), (4, 2))
  113. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  114. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  115. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  116. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  117. compile_net(net, x, y, b)
  118. def test_matmul_forward_reduce_scatter_transpose():
  119. class Net(nn.Cell):
  120. def __init__(self, strategy1, strategy2):
  121. super().__init__()
  122. self.matmul = P.MatMul(transpose_b=True).set_strategy(strategy1)
  123. self.matmul.add_prim_attr("forward_reduce_scatter", True)
  124. self.mul = P.Mul().set_strategy(strategy2)
  125. def construct(self, x, y, b):
  126. out = self.matmul(x, y)
  127. out = self.mul(out, b)
  128. return out
  129. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  130. context.set_context(save_graphs=True)
  131. strategy1 = ((2, 4), (2, 4))
  132. strategy2 = ((8, 2), (8, 2))
  133. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  134. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  135. y = Tensor(np.ones([64, 32]), dtype=ms.float32)
  136. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  137. compile_net(net, x, y, b)