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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. _executor.compile(train_net, _x1)
  62. context.reset_auto_parallel_context()
  63. def compile_net2(net):
  64. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  65. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  66. train_net = TrainOneStepCell(net, optimizer)
  67. train_net.set_auto_parallel()
  68. _executor.compile(train_net, _x1, _x2)
  69. context.reset_auto_parallel_context()
  70. def test_BroadcastTo_parameter():
  71. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  72. strategy1 = ((1, 4, 2),)
  73. strategy2 = ((1, 4, 2), (1, 4, 2))
  74. net = Net(_w1, strategy1, strategy2)
  75. compile_net(net)
  76. def test_BroadcastTo_parameter_no_full():
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  78. strategy1 = ((1, 2, 2),)
  79. strategy2 = ((1, 4, 2), (1, 4, 2))
  80. net = Net(_w1, strategy1, strategy2)
  81. compile_net(net)
  82. def test_BroadcastTo_auto_parallel():
  83. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  84. net = Net(_w1)
  85. compile_net(net)
  86. def test_BroadcastTo_matmul():
  87. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  88. strategy1 = ((2, 4),)
  89. strategy2 = ((1, 1, 2), (1, 2, 4))
  90. strategy3 = ((1, 2, 4), (1, 2, 4))
  91. net = MatMulNet(_w1, strategy1, strategy2, strategy3)
  92. compile_net2(net)