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_batch_matmul.py 4.8 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. from mindspore import context, Tensor, Parameter
  17. from mindspore.common.api import _cell_graph_executor
  18. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  19. from mindspore.ops import operations as P
  20. class Net(Cell):
  21. def __init__(self, mul_weight, batch_matmul_weight, transpose_b=False, strategy1=None, strategy2=None):
  22. super().__init__()
  23. self.mul = P.Mul().shard(strategy1)
  24. self.batch_matmul = P.BatchMatMul(transpose_b=transpose_b).shard(strategy2)
  25. self.mul_weight = Parameter(mul_weight, "w1")
  26. self.batch_matmul_weight = Parameter(batch_matmul_weight, "w2")
  27. def construct(self, x, b):
  28. out = self.mul(x, self.mul_weight)
  29. out = self.batch_matmul(out, self.batch_matmul_weight)
  30. return out
  31. _x = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  32. _w1 = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  33. _w2 = Tensor(np.ones([128, 32, 32]), dtype=ms.float32)
  34. _b = Tensor(np.ones([128, 64, 16]), dtype=ms.float32)
  35. def compile_net(net):
  36. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  37. train_net = TrainOneStepCell(net, optimizer)
  38. train_net.set_auto_parallel()
  39. train_net.set_train()
  40. _cell_graph_executor.compile(train_net, _x, _b)
  41. context.reset_auto_parallel_context()
  42. def test_batch_matmul_data_parallel():
  43. """
  44. Feature: distribute operator batch_matmul in auto parallel.
  45. Description: mul-batch_matmul net with data parallel strategy in semi auto parallel.
  46. Expectation: compile done without error.
  47. """
  48. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  49. strategy1 = ((16, 1, 1), (16, 1, 1))
  50. strategy2 = ((16, 1, 1), (16, 1, 1))
  51. net = Net(_w1, _w2, False, strategy1, strategy2)
  52. compile_net(net)
  53. def test_batch_matmul_model_parallel():
  54. """
  55. Feature: distribute operator batch_matmul in auto parallel.
  56. Description: mul-batch_matmul net with model parallel strategy in semi auto parallel.
  57. Expectation: compile done without error.
  58. """
  59. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  60. strategy1 = ((1, 1, 1), (1, 1, 1))
  61. strategy2 = ((1, 1, 1), (1, 1, 16))
  62. net = Net(_w1, _w2, False, strategy1, strategy2)
  63. compile_net(net)
  64. def test_batch_matmul_hybrid_parallel():
  65. """
  66. Feature: distribute operator batch_matmul in auto parallel.
  67. Description: mul-batch_matmul net with mixed strategy in semi auto parallel.
  68. Expectation: compile done without error.
  69. """
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  71. strategy1 = ((2, 2, 2), (2, 2, 2))
  72. strategy2 = ((2, 2, 2), (2, 2, 2))
  73. net = Net(_w1, _w2, False, strategy1, strategy2)
  74. compile_net(net)
  75. def test_batch_matmul_auto_parallel():
  76. """
  77. Feature: distribute operator batch_matmul in auto parallel.
  78. Description: mul-batch_matmul net in auto parallel.
  79. Expectation: compile done without error.
  80. """
  81. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=16, global_rank=0)
  82. net = Net(_w1, _w2, False)
  83. compile_net(net)
  84. def test_batch_matmul_repeat_calc():
  85. """
  86. Feature: distribute operator batch_matmul in auto parallel.
  87. Description: mul-batch_matmul net with repeated strategy in semi auto parallel.
  88. Expectation: compile done without error.
  89. """
  90. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  91. strategy1 = ((2, 2, 4), (2, 2, 4))
  92. strategy2 = ((1, 2, 2), (1, 2, 2))
  93. net = Net(_w1, _w2, False, strategy1, strategy2)
  94. compile_net(net)
  95. def test_batch_matmul_transpose_b():
  96. """
  97. Feature: distribute operator batch_matmul in auto parallel.
  98. Description: mul-batch_matmul net with strategy in semi auto parallel, transpose_b.
  99. Expectation: compile done without error.
  100. """
  101. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  102. strategy1 = ((2, 2, 4), (2, 2, 4))
  103. strategy2 = ((1, 2, 2), (1, 2, 2))
  104. net = Net(_w1, _w2, True, strategy1, strategy2)
  105. compile_net(net)