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_maxpool_avgpool.py 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 _executor
  18. from mindspore.nn import Cell, TrainOneStepCell, Momentum
  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, pool_kernel_size, pool_strides,
  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.max_pool = P.MaxPool(kernel_size=pool_kernel_size, strides=pool_strides).shard(strategy2)
  28. def construct(self, x, b):
  29. out = self.conv2d(x, self.conv2d_weight)
  30. out = self.max_pool(out)
  31. return out
  32. class Net2(Cell):
  33. def __init__(self, conv2d_weight, out_channel, kernel_size, pad_mode, stride, pool_kernel_size, pool_strides,
  34. strategy1=None, strategy2=None):
  35. super().__init__()
  36. self.conv2d = P.Conv2D(out_channel=out_channel, kernel_size=kernel_size,
  37. pad_mode=pad_mode, stride=stride).shard(strategy1)
  38. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  39. self.avg_pool = P.AvgPool(kernel_size=pool_kernel_size, strides=pool_strides).shard(strategy2)
  40. def construct(self, x, b):
  41. out = self.conv2d(x, self.conv2d_weight)
  42. out = self.avg_pool(out)
  43. return out
  44. _x = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  45. _w1 = Tensor(np.ones([8, 16, 2, 2]), dtype=ms.float32)
  46. _b = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  47. def compile_net(net):
  48. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  49. train_net = TrainOneStepCell(net, optimizer)
  50. train_net.set_auto_parallel()
  51. train_net.set_train()
  52. _executor.compile(train_net, _x, _b)
  53. context.reset_auto_parallel_context()
  54. def test_maxpool_data_parallel():
  55. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  56. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  57. strategy2 = ((8, 1, 1, 1),)
  58. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=2,
  59. strategy1=strategy1, strategy2=strategy2)
  60. compile_net(net)
  61. def test_maxpool_model_parallel1():
  62. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  63. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  64. strategy2 = ((2, 1, 2, 2),)
  65. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=2,
  66. strategy1=strategy1, strategy2=strategy2)
  67. compile_net(net)
  68. def test_maxpool_model_parallel2():
  69. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  70. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  71. strategy2 = ((2, 1, 2, 2),)
  72. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=4,
  73. strategy1=strategy1, strategy2=strategy2)
  74. compile_net(net)
  75. def test_maxpool_auto_parallel():
  76. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  77. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=4)
  78. compile_net(net)
  79. def test_avgpool_data_parallel():
  80. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  81. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  82. strategy2 = ((8, 1, 1, 1),)
  83. net = Net2(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=2,
  84. strategy1=strategy1, strategy2=strategy2)
  85. compile_net(net)
  86. def test_avgpool_model_parallel1():
  87. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  88. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  89. strategy2 = ((2, 1, 2, 2),)
  90. net = Net2(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=2,
  91. strategy1=strategy1, strategy2=strategy2)
  92. compile_net(net)
  93. def test_avgpool_model_parallel2():
  94. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  95. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  96. strategy2 = ((2, 1, 2, 2),)
  97. net = Net2(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=4,
  98. strategy1=strategy1, strategy2=strategy2)
  99. compile_net(net)
  100. def test_avgpool_auto_parallel():
  101. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  102. net = Net2(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1, pool_kernel_size=2, pool_strides=4)
  103. compile_net(net)