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_conv2d.py 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 _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, 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.neg = P.Neg().shard(strategy2)
  28. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  29. def construct(self, x, b):
  30. out = self.conv2d(x, self.conv2d_weight)
  31. out = self.neg(out)
  32. return out
  33. _x = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  34. _w1 = Tensor(np.ones([8, 16, 2, 2]), dtype=ms.float32)
  35. _w2 = Tensor(np.ones([8, 16, 3, 3]), dtype=ms.float32)
  36. _w3 = Tensor(np.ones([8, 16, 5, 5]), 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. _executor.compile(train_net, _x, _b)
  44. context.reset_auto_parallel_context()
  45. def test_conv2d_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),)
  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_conv2d_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 = ((8, 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_conv2d_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 = ((32, 1, 1, 1),)
  61. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=2, strategy1=strategy1, strategy2=strategy2)
  62. compile_net(net)
  63. def test_conv2d_model_parallel3():
  64. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  65. strategy1 = ((2, 1, 1, 4), (1, 1, 1, 1))
  66. strategy2 = ((2, 1, 1, 4),)
  67. net = Net(_w2, out_channel=8, kernel_size=3, pad_mode="same", stride=1, strategy1=strategy1, strategy2=strategy2)
  68. compile_net(net)
  69. def test_conv2d_model_parallel4():
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=32, global_rank=0)
  71. strategy1 = ((2, 2, 1, 4), (2, 2, 1, 1))
  72. strategy2 = ((2, 2, 1, 4),)
  73. net = Net(_w2, out_channel=8, kernel_size=3, pad_mode="same", stride=1, strategy1=strategy1, strategy2=strategy2)
  74. compile_net(net)
  75. def test_conv2d_left_and_right_no_need_to_send():
  76. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  77. strategy1 = ((2, 1, 1, 4), (1, 1, 1, 1))
  78. strategy2 = ((2, 1, 1, 4),)
  79. net = Net(_w2, out_channel=8, kernel_size=3, pad_mode="same", stride=2, strategy1=strategy1, strategy2=strategy2)
  80. with pytest.raises(RuntimeError):
  81. compile_net(net)
  82. def test_conv2d_output_can_not_divisible_by_strategy():
  83. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  84. strategy1 = ((1, 1, 1, 8), (1, 1, 1, 1))
  85. strategy2 = ((1, 1, 1, 8),)
  86. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=2, strategy1=strategy1, strategy2=strategy2)
  87. with pytest.raises(RuntimeError):
  88. compile_net(net)