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_batchnorm.py 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # Copyright 2021 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 pytest
  16. import mindspore as ms
  17. from mindspore import context, Tensor, Parameter
  18. from mindspore.common.api import _cell_graph_executor
  19. from mindspore.nn import Cell, TrainOneStepCell, Momentum, BatchNorm2d, BatchNorm1d
  20. from mindspore.ops import operations as P
  21. class Net(Cell):
  22. def __init__(self, conv2d_weight, out_channel, kernel_size, pad_mode, stride,
  23. strategy1=None, strategy2=None):
  24. super().__init__()
  25. self.conv2d = P.Conv2D(out_channel=out_channel, kernel_size=kernel_size,
  26. pad_mode=pad_mode, stride=stride).shard(strategy1)
  27. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  28. self.bn = BatchNorm2d(8)
  29. self.bn.bn_train.shard(strategy2)
  30. def construct(self, x, b):
  31. out = self.conv2d(x, self.conv2d_weight)
  32. out = self.bn(out)
  33. return out
  34. _x = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  35. _w1 = Tensor(np.ones([8, 16, 2, 2]), dtype=ms.float32)
  36. _b = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  37. def compile_net(net):
  38. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  39. train_net = TrainOneStepCell(net, optimizer)
  40. train_net.set_auto_parallel()
  41. train_net.set_train()
  42. _cell_graph_executor.compile(train_net, _x, _b)
  43. context.reset_auto_parallel_context()
  44. def test_batchnorm_data_parallel():
  45. """
  46. Feature: test batchnorm2d
  47. Description: shard n
  48. Expectation: compile success
  49. """
  50. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  51. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  52. strategy2 = ((8, 1, 1, 1), (1,), (1,), (1,), (1,))
  53. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, strategy1=strategy1, strategy2=strategy2)
  54. compile_net(net)
  55. def test_batchnorm_model_parallel1():
  56. """
  57. Feature: test batchnorm2d
  58. Description: shard n/c
  59. Expectation: compile success
  60. """
  61. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  62. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  63. strategy2 = ((2, 1, 2, 2), (1,), (1,), (1,), (1,))
  64. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, strategy1=strategy1, strategy2=strategy2)
  65. compile_net(net)
  66. def test_batchnorm_model_parallel2():
  67. """
  68. Feature: test batchnorm2d
  69. Description: shard n/c/h/w
  70. Expectation: compile success
  71. """
  72. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  73. strategy1 = ((2, 2, 2, 2), (2, 2, 1, 1))
  74. strategy2 = ((1, 8, 1, 1), (8,), (8,), (8,), (8,))
  75. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=2, strategy1=strategy1, strategy2=strategy2)
  76. compile_net(net)
  77. class Net2(Cell):
  78. def __init__(self, strategy1=None, strategy2=None, group_size=0):
  79. super().__init__()
  80. self.bn = BatchNorm1d(8)
  81. self.bn.bn_train.shard(strategy1)
  82. self.relu = P.ReLU().shard(strategy2)
  83. if group_size > 0:
  84. self.bn.bn_train.add_prim_attr("group_size", group_size)
  85. def construct(self, x, b):
  86. out = self.bn(x)
  87. out = self.relu(out)
  88. return out
  89. _x1 = Tensor(np.ones([32, 8]), dtype=ms.float32)
  90. _b1 = Tensor(np.ones([32, 8]), dtype=ms.float32)
  91. def compile_net2(net):
  92. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  93. train_net = TrainOneStepCell(net, optimizer)
  94. train_net.set_auto_parallel()
  95. train_net.set_train()
  96. _cell_graph_executor.compile(train_net, _x1, _b1)
  97. context.reset_auto_parallel_context()
  98. def test_batchnorm1d_data_parallel():
  99. """
  100. Feature: test batchnorm1d
  101. Description: shard n
  102. Expectation: compile success
  103. """
  104. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  105. strategy1 = ((8, 1), (1,), (1,), (1,), (1,))
  106. strategy2 = ((8, 1),)
  107. net = Net2(strategy1=strategy1, strategy2=strategy2)
  108. compile_net2(net)
  109. def test_batchnorm1d_model_parallel1():
  110. """
  111. Feature: test batchnorm1d
  112. Description: shard c
  113. Expectation: compile success
  114. """
  115. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  116. strategy1 = ((1, 8), (8,), (8,), (8,), (8,))
  117. strategy2 = ((1, 8),)
  118. net = Net2(strategy1=strategy1, strategy2=strategy2)
  119. compile_net2(net)
  120. def test_batchnorm1d_model_parallel2():
  121. """
  122. Feature: test batchnorm1d
  123. Description: shard n/c
  124. Expectation: compile success
  125. """
  126. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  127. strategy1 = ((2, 4), (4,), (4,), (4,), (4,))
  128. strategy2 = ((2, 4),)
  129. net = Net2(strategy1=strategy1, strategy2=strategy2)
  130. compile_net2(net)
  131. def test_batchnorm_config_group_size():
  132. """
  133. Feature: test config group size
  134. Description: group is 8
  135. Expectation: compile success
  136. """
  137. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  138. strategy1 = ((32, 1), (1,), (1,), (1,), (1,))
  139. strategy2 = ((32, 1),)
  140. net = Net2(strategy1=strategy1, strategy2=strategy2, group_size=8)
  141. compile_net2(net)
  142. def test_batchnorm_config_group_size_no_allreduce():
  143. """
  144. Feature: test config group size
  145. Description: group is 1
  146. Expectation: compile success
  147. """
  148. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  149. strategy1 = ((32, 1), (1,), (1,), (1,), (1,))
  150. strategy2 = ((32, 1),)
  151. net = Net2(strategy1=strategy1, strategy2=strategy2, group_size=1)
  152. compile_net2(net)
  153. def test_batchnorm_config_group_size_is_not_power_of_2():
  154. """
  155. Feature: test config group size
  156. Description: group is not the power of 2
  157. Expectation: compile failed
  158. """
  159. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  160. strategy1 = ((32, 1), (1,), (1,), (1,), (1,))
  161. strategy2 = ((32, 1),)
  162. net = Net2(strategy1=strategy1, strategy2=strategy2, group_size=10)
  163. with pytest.raises(RuntimeError):
  164. compile_net2(net)
  165. def test_batchnorm_config_group_size_and_shard_n_c():
  166. """
  167. Feature: test config group size
  168. Description: shard n/c
  169. Expectation: compile failed
  170. """
  171. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  172. strategy1 = ((8, 4), (4,), (4,), (4,), (4,))
  173. strategy2 = ((8, 4),)
  174. net = Net2(strategy1=strategy1, strategy2=strategy2, group_size=4)
  175. with pytest.raises(RuntimeError):
  176. compile_net2(net)