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_broadcast_to.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. # ============================================================================
  15. import numpy as np
  16. import mindspore as ms
  17. import mindspore.context as context
  18. from mindspore import Tensor, Parameter
  19. import mindspore.nn as nn
  20. from mindspore.common.api import _executor
  21. from mindspore.nn import TrainOneStepCell, Momentum
  22. from mindspore.ops import operations as P
  23. class Net(nn.Cell):
  24. def __init__(self, weight1, strategy1=None, strategy2=None, is_parameter=True):
  25. super(Net, self).__init__()
  26. self.shape = (8, 48, 64)
  27. self.broadcast = P.BroadcastTo(self.shape).shard(strategy1)
  28. self.mul = P.Mul().shard(strategy2)
  29. if is_parameter:
  30. self.weight1 = Parameter(weight1, "w1")
  31. else:
  32. self.weight1 = weight1
  33. def construct(self, x):
  34. out = self.broadcast(self.weight1)
  35. out = self.mul(x, out)
  36. return out
  37. class MatMulNet(nn.Cell):
  38. def __init__(self, weight1, strategy1=None, strategy2=None, strategy3=None, is_parameter=True):
  39. super(MatMulNet, self).__init__()
  40. self.shape = (8, 64, 64)
  41. self.broadcast = P.BroadcastTo(self.shape).shard(strategy1)
  42. self.matmul = P.BatchMatMul().shard(strategy2)
  43. self.mul = P.Mul().shard(strategy3)
  44. if is_parameter:
  45. self.weight1 = Parameter(weight1, "w1")
  46. else:
  47. self.weight1 = weight1
  48. def construct(self, x1, x2):
  49. out = self.broadcast(x2)
  50. out = self.matmul(x1, out)
  51. out = self.mul(out, self.weight1)
  52. return out
  53. _w1 = Tensor(np.ones([1, 48, 64]), dtype=ms.float32)
  54. _x1 = Tensor(np.ones([8, 48, 64]), dtype=ms.float32)
  55. _x2 = Tensor(np.ones([64, 64]), dtype=ms.float32)
  56. def compile_net(net):
  57. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  58. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  59. train_net = TrainOneStepCell(net, optimizer)
  60. train_net.set_auto_parallel()
  61. train_net.set_train()
  62. _executor.compile(train_net, _x1)
  63. context.reset_auto_parallel_context()
  64. def compile_net2(net):
  65. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  66. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  67. train_net = TrainOneStepCell(net, optimizer)
  68. train_net.set_auto_parallel()
  69. train_net.set_train()
  70. _executor.compile(train_net, _x1, _x2)
  71. context.reset_auto_parallel_context()
  72. def test_BroadcastTo_parameter():
  73. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  74. strategy1 = ((1, 4, 2),)
  75. strategy2 = ((1, 4, 2), (1, 4, 2))
  76. net = Net(_w1, strategy1, strategy2)
  77. compile_net(net)
  78. def test_BroadcastTo_parameter_no_full():
  79. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  80. strategy1 = ((1, 2, 2),)
  81. strategy2 = ((1, 4, 2), (1, 4, 2))
  82. net = Net(_w1, strategy1, strategy2)
  83. compile_net(net)
  84. def test_BroadcastTo_auto_parallel():
  85. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  86. net = Net(_w1)
  87. compile_net(net)
  88. def test_BroadcastTo_matmul():
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  90. strategy1 = ((2, 4),)
  91. strategy2 = ((1, 1, 2), (1, 2, 4))
  92. strategy3 = ((1, 2, 4), (1, 2, 4))
  93. net = MatMulNet(_w1, strategy1, strategy2, strategy3)
  94. compile_net2(net)