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

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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, BatchNorm2d, BatchNorm1d
  19. from mindspore.ops import operations as P
  20. class Net(Cell):
  21. def __init__(self, conv2d_weight, out_channel, kernel_size, pad_mode, stride,
  22. strategy1=None, strategy2=None):
  23. super().__init__()
  24. self.conv2d = P.Conv2D(out_channel=out_channel, kernel_size=kernel_size,
  25. pad_mode=pad_mode, stride=stride).shard(strategy1)
  26. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  27. self.bn = BatchNorm2d(8)
  28. self.bn.bn_train.shard(strategy2)
  29. self.print = P.Print()
  30. def construct(self, x, b):
  31. out = self.conv2d(x, self.conv2d_weight)
  32. self.print("output is", out)
  33. out = self.bn(out)
  34. return out
  35. _x = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  36. _w1 = Tensor(np.ones([8, 16, 2, 2]), dtype=ms.float32)
  37. _b = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  38. def compile_net(net):
  39. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  40. train_net = TrainOneStepCell(net, optimizer)
  41. train_net.set_auto_parallel()
  42. train_net.set_train()
  43. _cell_graph_executor.compile(train_net, _x, _b)
  44. context.reset_auto_parallel_context()
  45. def test_batchnorm_data_parallel():
  46. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  47. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  48. strategy2 = ((8, 1, 1, 1), (1,), (1,), (1,), (1,))
  49. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, strategy1=strategy1, strategy2=strategy2)
  50. compile_net(net)
  51. def test_batchnorm_model_parallel1():
  52. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  53. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  54. strategy2 = ((2, 1, 2, 2), (1,), (1,), (1,), (1,))
  55. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, strategy1=strategy1, strategy2=strategy2)
  56. compile_net(net)
  57. def test_batchnorm_model_parallel2():
  58. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  59. strategy1 = ((2, 2, 2, 2), (2, 2, 1, 1))
  60. strategy2 = ((1, 8, 1, 1), (8,), (8,), (8,), (8,))
  61. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=2, strategy1=strategy1, strategy2=strategy2)
  62. compile_net(net)
  63. class Net2(Cell):
  64. def __init__(self, strategy1=None, strategy2=None):
  65. super().__init__()
  66. self.bn = BatchNorm1d(8)
  67. self.bn.bn_train.shard(strategy1)
  68. self.relu = P.ReLU().shard(strategy2)
  69. def construct(self, x, b):
  70. out = self.bn(x)
  71. out = self.relu(out)
  72. return out
  73. _x1 = Tensor(np.ones([32, 8]), dtype=ms.float32)
  74. _b1 = Tensor(np.ones([32, 8]), dtype=ms.float32)
  75. def compile_net2(net):
  76. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  77. train_net = TrainOneStepCell(net, optimizer)
  78. train_net.set_auto_parallel()
  79. train_net.set_train()
  80. _cell_graph_executor.compile(train_net, _x1, _b1)
  81. context.reset_auto_parallel_context()
  82. def test_batchnorm1d_data_parallel():
  83. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  84. strategy1 = ((8, 1), (1,), (1,), (1,), (1,))
  85. strategy2 = ((8, 1),)
  86. net = Net2(strategy1=strategy1, strategy2=strategy2)
  87. compile_net2(net)
  88. def test_batchnorm1d_model_parallel1():
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  90. strategy1 = ((1, 8), (8,), (8,), (8,), (8,))
  91. strategy2 = ((1, 8),)
  92. net = Net2(strategy1=strategy1, strategy2=strategy2)
  93. compile_net2(net)
  94. def test_batchnorm1d_model_parallel2():
  95. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  96. strategy1 = ((2, 4), (4,), (4,), (4,), (4,))
  97. strategy2 = ((2, 4),)
  98. net = Net2(strategy1=strategy1, strategy2=strategy2)
  99. compile_net2(net)