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 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, mul_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. self.mul = P.Mul()
  48. self.mul_weight = Parameter(mul_weight, "w2")
  49. def construct(self, x):
  50. out = self.conv2d(x, self.conv2d_weight)
  51. out = self.resize_neighbor(out)
  52. out = self.mul(out, self.mul_weight)
  53. return out
  54. class Net3(Cell):
  55. '''
  56. create the test Net
  57. '''
  58. def __init__(self, conv2d_weight, out_channel, kernel_size, pad_mode, stride,
  59. strategy1=None):
  60. super(Net3, self).__init__()
  61. self.conv2d = P.Conv2D(out_channel=out_channel, kernel_size=kernel_size,
  62. pad_mode=pad_mode, stride=stride).shard(strategy1)
  63. self.conv2d_weight = Parameter(conv2d_weight, "w1")
  64. self.resize_bilinear = P.ResizeBilinear((16, 16))
  65. def construct(self, x):
  66. out = self.conv2d(x, self.conv2d_weight)
  67. out = self.resize_bilinear(out)
  68. return out
  69. _x = Tensor(np.ones([32, 16, 8, 8]), dtype=ms.float32)
  70. _w1 = Tensor(np.ones([8, 16, 2, 2]), dtype=ms.float32)
  71. _w2 = Tensor(np.ones([32, 8, 16, 16]), dtype=ms.float32)
  72. def compile_net(net, inputs=_x):
  73. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  74. train_net = TrainOneStepCell(net, optimizer)
  75. train_net.set_auto_parallel()
  76. train_net.set_train()
  77. _cell_graph_executor.compile(train_net, inputs)
  78. context.reset_auto_parallel_context()
  79. def test_bililear_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 = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  84. strategy1=strategy1, strategy2=strategy2)
  85. compile_net(net)
  86. def test_bilinear_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 = ((4, 2, 1, 1),)
  90. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  91. strategy1=strategy1, strategy2=strategy2)
  92. compile_net(net)
  93. def test_bilinear_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, 1, 1),)
  97. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  98. strategy1=strategy1, strategy2=strategy2)
  99. compile_net(net)
  100. def test_bilinear_auto_parallel():
  101. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  102. net = Net(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1)
  103. compile_net(net)
  104. def test_bilinear_no_strategy():
  105. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  106. net = Net3(_w1, out_channel=8, kernel_size=2, pad_mode="same", stride=1)
  107. compile_net(net)
  108. def test_neighbor_data_parallel():
  109. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=8, global_rank=0)
  110. strategy1 = ((8, 1, 1, 1), (1, 1, 1, 1))
  111. strategy2 = ((8, 1, 1, 1),)
  112. net = Net2(_w1, _w2, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  113. strategy1=strategy1, strategy2=strategy2)
  114. compile_net(net)
  115. def test_neighbor_model_parallel1():
  116. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel", device_num=16, global_rank=0)
  117. strategy1 = ((2, 2, 1, 1), (2, 2, 1, 1))
  118. strategy2 = ((2, 2, 2, 2),)
  119. net = Net2(_w1, _w2, out_channel=8, kernel_size=2, pad_mode="same", stride=1,
  120. strategy1=strategy1, strategy2=strategy2)
  121. compile_net(net)
  122. def test_neighbor_auto_parallel():
  123. context.set_auto_parallel_context(parallel_mode="auto_parallel", device_num=8, global_rank=0)
  124. net = Net2(_w1, _w2, out_channel=8, kernel_size=2, pad_mode="same", stride=1)
  125. compile_net(net)