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_adasum.py 5.5 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright 2022 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, Tensor, Parameter
  17. from mindspore.common.api import _cell_graph_executor
  18. from mindspore.nn import Cell, TrainOneStepCell, Momentum, AdaSumByDeltaWeightWrapCell, AdaSumByGradWrapCell
  19. from mindspore.ops import operations as P
  20. class Net(Cell):
  21. def __init__(self, strategy1=None, strategy2=None, strategy3=None):
  22. super().__init__()
  23. self.mul = P.Mul().shard(strategy1)
  24. self.matmul = P.MatMul().shard(strategy2)
  25. self.gather = P.Gather().shard(strategy3)
  26. self.reduce_sum = P.ReduceSum()
  27. self.mul_weight = Parameter(Tensor(np.ones([64, 32]), dtype=ms.float32), "w1")
  28. self.matmul_weight = Parameter(Tensor(np.ones([32, 32]), dtype=ms.float32), "w2")
  29. self.embedding_table = Parameter(Tensor(np.ones([64, 32]), dtype=ms.float32), "embedding_table")
  30. def construct(self, x, b):
  31. out = self.gather(self.embedding_table, x, 0)
  32. out = self.matmul(out, self.matmul_weight)
  33. out = self.mul(out, self.mul_weight)
  34. out = out + b
  35. return self.reduce_sum(out)
  36. _x = Tensor(np.ones([64]), dtype=ms.int32)
  37. _b = Tensor(np.ones([64, 32]), dtype=ms.float32)
  38. def compile_net(net, by_grad=True):
  39. if by_grad:
  40. optimizer = AdaSumByGradWrapCell(Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9))
  41. else:
  42. optimizer = AdaSumByDeltaWeightWrapCell(Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9))
  43. train_net = TrainOneStepCell(net, optimizer)
  44. train_net.set_auto_parallel()
  45. train_net.set_train()
  46. _cell_graph_executor.compile(train_net, _x, _b)
  47. context.reset_auto_parallel_context()
  48. def test_auto_parallel_adasum1():
  49. """
  50. Feature: adasum in auto parallel.
  51. Description: verify adasum by mul/matmul/gather, rank0, dp, mp, not_full_dp
  52. Expectation: compile done without error.
  53. """
  54. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  55. mul_strategy1 = ((8, 4), (8, 4))
  56. matmul_strategy2 = ((8, 1), (1, 1))
  57. gather_strategy3 = ((1, 1), (32,))
  58. net = Net(mul_strategy1, matmul_strategy2, gather_strategy3)
  59. compile_net(net)
  60. def test_auto_parallel_adasum2():
  61. """
  62. Feature: adasum in auto parallel.
  63. Description: verify adasum by mul/matmul/gather, rank0, dp, mp, not_full_dp
  64. Expectation: compile done without error.
  65. """
  66. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  67. mul_strategy1 = ((8, 4), (8, 4))
  68. matmul_strategy2 = ((8, 1), (1, 1))
  69. gather_strategy3 = ((1, 1), (32,))
  70. net = Net(mul_strategy1, matmul_strategy2, gather_strategy3)
  71. compile_net(net, by_grad=False)
  72. def test_auto_parallel_adasum3():
  73. """
  74. Feature: adasum in auto parallel.
  75. Description: verify adasum by mul/matmul/gather, rank0, mix_dp_mp, mp
  76. Expectation: compile done without error.
  77. """
  78. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  79. mul_strategy1 = ((8, 4), (8, 4))
  80. matmul_strategy2 = ((8, 4), (4, 1))
  81. gather_strategy3 = ((32, 1), (1,))
  82. net = Net(mul_strategy1, matmul_strategy2, gather_strategy3)
  83. compile_net(net)
  84. def test_auto_parallel_adasum4():
  85. """
  86. Feature: adasum in auto parallel.
  87. Description: verify adasum by mul/matmul/gather, rank0, mix_dp_mp, mp
  88. Expectation: compile done without error.
  89. """
  90. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  91. mul_strategy1 = ((8, 4), (8, 4))
  92. matmul_strategy2 = ((8, 4), (4, 1))
  93. gather_strategy3 = ((32, 1), (1,))
  94. net = Net(mul_strategy1, matmul_strategy2, gather_strategy3)
  95. compile_net(net, by_grad=False)
  96. def test_auto_parallel_adasum5():
  97. """
  98. Feature: adasum in auto parallel.
  99. Description: verify adasum by mul/matmul/gather, rank16, dp, mp, not_full_dp
  100. Expectation: compile done without error.
  101. """
  102. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=16)
  103. mul_strategy1 = ((8, 4), (8, 4))
  104. matmul_strategy2 = ((8, 1), (1, 1))
  105. gather_strategy3 = ((1, 1), (32,))
  106. net = Net(mul_strategy1, matmul_strategy2, gather_strategy3)
  107. compile_net(net)
  108. def test_auto_parallel_adasum6():
  109. """
  110. Feature: adasum in auto parallel.
  111. Description: verify adasum by mul/matmul/gather, rank16, dp, mp, not_full_dp
  112. Expectation: compile done without error.
  113. """
  114. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=16)
  115. mul_strategy1 = ((8, 4), (8, 4))
  116. matmul_strategy2 = ((8, 1), (1, 1))
  117. gather_strategy3 = ((1, 1), (32,))
  118. net = Net(mul_strategy1, matmul_strategy2, gather_strategy3)
  119. compile_net(net, by_grad=False)