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_resizebilinear.py 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. '''ResizeBilinear and ResizeNearestNeigbor ut'''
  15. import numpy as np
  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
  20. from mindspore.ops import operations as P
  21. class Net(Cell):
  22. '''
  23. create the test Net
  24. '''
  25. def __init__(self, conv2d_weight, out_channel, kernel_size, pad_mode, stride,
  26. strategy1=None, strategy2=None):
  27. super(Net, self).__init__()
  28. self.conv2d = P.Conv2D(out_channel=out_channel, kernel_size=kernel_size,
  29. pad_mode=pad_mode, stride=stride).shard(strategy1)
  30. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  31. self.resize_bilinear = P.ResizeBilinear((16, 16)).shard(strategy2)
  32. def construct(self, x):
  33. out = self.conv2d(x, self.conv2d_weight)
  34. out = self.resize_bilinear(out)
  35. return out
  36. class Net2(Cell):
  37. '''
  38. create the test Net
  39. '''
  40. def __init__(self, conv2d_weight, out_channel, kernel_size, pad_mode, stride,
  41. strategy1=None, strategy2=None):
  42. super(Net2, self).__init__()
  43. self.conv2d = P.Conv2D(out_channel=out_channel, kernel_size=kernel_size,
  44. pad_mode=pad_mode, stride=stride).shard(strategy1)
  45. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  46. self.resize_neighbor = P.ResizeNearestNeighbor((16, 16)).shard(strategy2)
  47. def construct(self, x):
  48. out = self.conv2d(x, self.conv2d_weight)
  49. out = self.resize_neighbor(out)
  50. return out
  51. class Net3(Cell):
  52. '''
  53. create the test Net
  54. '''
  55. def __init__(self, conv2d_weight, out_channel, kernel_size, pad_mode, stride,
  56. strategy1=None):
  57. super(Net3, self).__init__()
  58. self.conv2d = P.Conv2D(out_channel=out_channel, kernel_size=kernel_size,
  59. pad_mode=pad_mode, stride=stride).shard(strategy1)
  60. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  61. self.resize_bilinear = P.ResizeBilinear((16, 16))
  62. def construct(self, x):
  63. out = self.conv2d(x, self.conv2d_weight)
  64. out = self.resize_bilinear(out)
  65. return out
  66. _x = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  67. _w1 = Tensor(np.ones([8, 16, 2, 2]), dtype=ms.float32)
  68. def compile_net(net, inputs=_x):
  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. _cell_graph_executor.compile(train_net, inputs)
  74. context.reset_auto_parallel_context()
  75. def test_bililear_data_parallel():
  76. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  77. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  78. strategy2 = ((8, 1, 1, 1),)
  79. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  80. strategy1=strategy1, strategy2=strategy2)
  81. compile_net(net)
  82. def test_bilinear_model_parallel1():
  83. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  84. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  85. strategy2 = ((4, 2, 1, 1),)
  86. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  87. strategy1=strategy1, strategy2=strategy2)
  88. compile_net(net)
  89. def test_bilinear_model_parallel2():
  90. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  91. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  92. strategy2 = ((2, 1, 1, 1),)
  93. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  94. strategy1=strategy1, strategy2=strategy2)
  95. compile_net(net)
  96. def test_bilinear_auto_parallel():
  97. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  98. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1)
  99. compile_net(net)
  100. def test_bilinear_no_strategy():
  101. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  102. net = Net3(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1)
  103. compile_net(net)
  104. def test_neighbor_data_parallel():
  105. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  106. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  107. strategy2 = ((8, 1, 1, 1),)
  108. net = Net2(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  109. strategy1=strategy1, strategy2=strategy2)
  110. compile_net(net)
  111. def test_neighbor_model_parallel1():
  112. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  113. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  114. strategy2 = ((4, 2, 1, 1),)
  115. net = Net2(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  116. strategy1=strategy1, strategy2=strategy2)
  117. compile_net(net)
  118. def test_neighbor_auto_parallel():
  119. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  120. net = Net2(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1)
  121. compile_net(net)