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_tile.py 5.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. from mindspore import context, Tensor, Parameter
  18. from mindspore.common.api import _executor
  19. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  20. from mindspore.ops import operations as P
  21. class Net(Cell):
  22. def __init__(self, weight, weight2, strategy1=None, strategy2=None, is_parameter=True):
  23. super().__init__()
  24. self.mul = P.Mul().shard(strategy1)
  25. self.tile = P.Tile().shard(strategy2)
  26. if is_parameter:
  27. self.weight = Parameter(weight, "w1")
  28. else:
  29. self.weight = weight
  30. self.mul2 = P.Mul()
  31. self.weight2 = Parameter(weight2, "w2")
  32. def construct(self, x, b):
  33. out = self.tile(self.weight, (8, 4, 2))
  34. out = self.mul(x, out)
  35. out = self.mul2(out, self.weight2)
  36. return out
  37. class Net2(Cell):
  38. def __init__(self, weight2, strategy1=None, strategy2=None):
  39. super().__init__()
  40. self.mul = P.Mul().shard(strategy1)
  41. self.tile = P.Tile().shard(strategy2)
  42. self.weight2 = Parameter(weight2, "w2")
  43. def construct(self, x, b):
  44. out = self.mul(x, self.weight2)
  45. out = self.tile(out, (8, 8, 4, 2))
  46. return out
  47. class Net3(Cell):
  48. def __init__(self, weight, strategy1=None, strategy2=None, is_parameter=True):
  49. super().__init__()
  50. self.mul = P.Mul().shard(strategy1)
  51. self.tile = P.Tile().shard(strategy2)
  52. if is_parameter:
  53. self.weight = Parameter(weight, "w1")
  54. else:
  55. self.weight = weight
  56. self.mul2 = P.Mul()
  57. def construct(self, x, b):
  58. out = self.tile(self.weight, (8, 1, 1))
  59. out = self.mul(x, out)
  60. return out
  61. _x = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  62. _x1 = Tensor(np.ones([128, 16, 16]), dtype=ms.float32)
  63. _w1 = Tensor(np.ones([16, 16, 16]), dtype=ms.float32)
  64. _w2 = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  65. _w3 = Tensor(np.ones([128, 16, 16]), dtype=ms.float32)
  66. _b = Tensor(np.ones([128, 64, 32]), dtype=ms.float32)
  67. def compile_net(net, x=_b, b=_b):
  68. context.set_context(save_graphs=True)
  69. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  70. train_net = TrainOneStepCell(net, optimizer)
  71. train_net.set_auto_parallel()
  72. train_net.set_train()
  73. _executor.compile(train_net, x, b)
  74. context.reset_auto_parallel_context()
  75. def test_tile_parameter():
  76. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  77. strategy1 = ((2, 2, 2), (2, 2, 2))
  78. strategy2 = ((2, 2, 2),)
  79. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  80. compile_net(net)
  81. def test_tile_parameter_no_full_split():
  82. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  83. strategy1 = ((2, 2, 2), (2, 2, 2))
  84. strategy2 = ((2, 2, 1),)
  85. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=True)
  86. compile_net(net)
  87. def test_tile_tensor():
  88. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  89. strategy1 = ((2, 2, 2), (2, 2, 2))
  90. strategy2 = ((2, 2, 2),)
  91. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=False)
  92. compile_net(net)
  93. def test_tile_tensor_no_full_split():
  94. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  95. strategy1 = ((2, 2, 2), (2, 2, 2))
  96. strategy2 = ((2, 2, 1),)
  97. net = Net(_w1, _w2, strategy1, strategy2, is_parameter=False)
  98. compile_net(net)
  99. def test_tile_tensor_no_full_split2():
  100. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  101. strategy1 = ((2, 2, 1), (2, 2, 1))
  102. strategy2 = ((2, 2, 1),)
  103. net = Net3(_w1, strategy1, strategy2)
  104. compile_net(net, _x1, _b)
  105. def test_tile_output():
  106. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  107. strategy1 = ((2, 2, 2), (2, 2, 2))
  108. strategy2 = ((1, 2, 2, 2),)
  109. net = Net2(_w2, strategy1, strategy2)
  110. compile_net(net)
  111. def test_tile_output_no_full_split():
  112. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  113. strategy1 = ((2, 2, 2), (2, 2, 2))
  114. strategy2 = ((1, 2, 1, 2),)
  115. net = Net2(_w2, strategy1, strategy2)
  116. compile_net(net)
  117. def test_tile_no_strategy():
  118. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  119. strategy1 = ((2, 2, 2), (2, 2, 2))
  120. strategy2 = None
  121. net = Net2(_w2, strategy1, strategy2)
  122. compile_net(net)
  123. def test_tile_auto_parallel():
  124. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  125. net = Net2(_w2)
  126. compile_net(net)
  127. def test_tile_auto_parallel_2():
  128. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  129. net = Net3(_w1)
  130. compile_net(net, _x1, _b)