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_pad.py 7.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Copyright 2020 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. # ============================================================================
  15. import pytest
  16. import numpy as np
  17. import mindspore
  18. import mindspore.nn as nn
  19. import mindspore.context as context
  20. from mindspore import Tensor
  21. from mindspore.ops.composite import GradOperation
  22. @pytest.mark.level0
  23. @pytest.mark.platform_x86_gpu_training
  24. @pytest.mark.env_onecard
  25. def test_pad_basic():
  26. # confirm array is being padded with 0's
  27. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  28. test_arr = np.array([[1, 2], [3, 4]]).astype(np.float32)
  29. test_arr_expected = np.array(
  30. [[0, 0, 0, 0], [0, 1, 2, 0], [0, 3, 4, 0], [0, 0, 0, 0]]).astype(np.float32)
  31. x_test = Tensor(test_arr, dtype=mindspore.float32)
  32. pad_op = nn.Pad(mode='CONSTANT', paddings=((1, 1), (1, 1)))
  33. y_test = pad_op(x_test).asnumpy()
  34. np.testing.assert_array_equal(y_test, test_arr_expected)
  35. @pytest.mark.level0
  36. @pytest.mark.platform_x86_gpu_training
  37. @pytest.mark.env_onecard
  38. def test_pad_row():
  39. # Confirm correct row padding
  40. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  41. test_arr_1 = np.random.rand(40, 40).astype(np.float32)
  42. test_paddings_1 = ((2, 3), (0, 0))
  43. test_arr_2 = np.random.randn(3, 10, 30, 30).astype(np.float32)
  44. test_paddings_2 = ((0, 0), (0, 0), (3, 0), (0, 0))
  45. pad_op_row_1 = nn.Pad(mode='CONSTANT', paddings=test_paddings_1)
  46. pad_op_row_2 = nn.Pad(mode='CONSTANT', paddings=test_paddings_2)
  47. x_test_1 = Tensor(np.array(test_arr_1), dtype=mindspore.float32)
  48. x_test_2 = Tensor(np.array(test_arr_2), dtype=mindspore.float32)
  49. y_test_1 = pad_op_row_1(x_test_1).asnumpy()
  50. y_test_2 = pad_op_row_2(x_test_2).asnumpy()
  51. # check size
  52. assert y_test_1.shape == (45, 40)
  53. assert y_test_2.shape == (3, 10, 33, 30)
  54. # check values - select correct sections
  55. np.testing.assert_equal(y_test_1[2:-3, :], test_arr_1)
  56. np.testing.assert_equal(y_test_2[:, :, 3:, :], test_arr_2)
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_gpu_training
  59. @pytest.mark.env_onecard
  60. def test_pad_column():
  61. # Confirm correct column padding
  62. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  63. test_arr_1 = np.random.randn(40, 40).astype(np.float32)
  64. test_paddings_1 = ((0, 0), (3, 3))
  65. test_arr_2 = np.random.randn(3, 10, 30, 30).astype(np.float32)
  66. test_paddings_2 = ((0, 0), (0, 0), (0, 0), (6, 1))
  67. pad_op_col_1 = nn.Pad(mode='CONSTANT', paddings=test_paddings_1)
  68. pad_op_col_2 = nn.Pad(mode='CONSTANT', paddings=test_paddings_2)
  69. x_test_1 = Tensor(np.array(test_arr_1), dtype=mindspore.float32)
  70. x_test_2 = Tensor(np.array(test_arr_2), dtype=mindspore.float32)
  71. y_test_1 = pad_op_col_1(x_test_1).asnumpy()
  72. y_test_2 = pad_op_col_2(x_test_2).asnumpy()
  73. # check size
  74. assert y_test_1.shape == (40, 46)
  75. assert y_test_2.shape == (3, 10, 30, 37)
  76. # check values - select correct sections - should match
  77. np.testing.assert_equal(y_test_1[:, 3:-3], test_arr_1)
  78. np.testing.assert_equal(y_test_2[:, :, :, 6:-1], test_arr_2)
  79. @pytest.mark.level0
  80. @pytest.mark.platform_x86_gpu_training
  81. @pytest.mark.env_onecard
  82. def test_pad_3d_pad():
  83. # Confirm correct 3d padding - row, column, channel
  84. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  85. test_arr = np.random.randn(5, 3, 30, 30).astype(np.float32)
  86. test_paddings = ((0, 0), (2, 1), (0, 1), (0, 2)) # padding 3 dims now
  87. pad_op_3d = nn.Pad(mode='CONSTANT', paddings=test_paddings)
  88. x_test = Tensor(np.array(test_arr), dtype=mindspore.float32)
  89. y_test = pad_op_3d(x_test).asnumpy()
  90. assert y_test.shape == (5, 6, 31, 32)
  91. np.testing.assert_equal(test_arr, y_test[:, 2:-1, :-1, :-2])
  92. # For testing backprop
  93. class Grad(nn.Cell):
  94. def __init__(self, network):
  95. super(Grad, self).__init__()
  96. self.grad = GradOperation(get_all=True, sens_param=True)
  97. self.network = network
  98. def construct(self, input_, output_grad):
  99. return self.grad(self.network)(input_, output_grad)
  100. class Net(nn.Cell):
  101. def __init__(self):
  102. super(Net, self).__init__()
  103. self.pad = nn.Pad(mode="CONSTANT", paddings=(
  104. (0, 0), (4, 3), (1, 1), (0, 2)))
  105. def construct(self, x):
  106. return self.pad(x)
  107. @pytest.mark.level0
  108. @pytest.mark.platform_x86_gpu_training
  109. @pytest.mark.env_onecard
  110. def test_pad_3d_backprop():
  111. # Confirm correct 3d padding backprop
  112. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  113. test_arr = np.random.randn(5, 3, 30, 30).astype(np.float32)
  114. x_test = Tensor(test_arr, dtype=mindspore.float32)
  115. padded_shape = (5, 10, 32, 32)
  116. dy = np.random.randn(*padded_shape).astype(np.float32)
  117. expected_dx = dy[:, 4:-3, 1:-1, :-2]
  118. net = Grad(Net())
  119. dx = net(x_test, Tensor(dy))
  120. dx = dx[0].asnumpy()
  121. np.testing.assert_array_equal(dx, expected_dx)
  122. @pytest.mark.level0
  123. @pytest.mark.platform_x86_gpu_training
  124. @pytest.mark.env_onecard
  125. def test_pad_error_cases():
  126. # Test against common errorneous inputs to catch correctly
  127. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  128. # TEST 1 - Neg padding values
  129. test_op = nn.Pad(paddings=((0, 0), (-1, -1)), mode="CONSTANT")
  130. test_arr = np.random.randn(3, 3)
  131. test_arr_ms = Tensor(test_arr, dtype=mindspore.float32)
  132. with pytest.raises(ValueError):
  133. test_op(test_arr_ms)
  134. # TEST 2 - Mismatched input size and paddings - 1D tensor
  135. test_op = nn.Pad(paddings=((0, 0), (1, 0)), mode="CONSTANT")
  136. test_arr = np.random.randn(3) # 1D Tensor
  137. test_arr_ms = Tensor(test_arr, dtype=mindspore.float32)
  138. with pytest.raises(ValueError):
  139. test_op(test_arr_ms)
  140. # TEST 3 - Mismatched input size and paddings - 2D tensor, 3D padding
  141. test_op = nn.Pad(paddings=((0, 0), (1, 0)), mode="CONSTANT") # 2D Padding
  142. test_arr = np.random.randn(1, 3, 3) # 3D Tensor
  143. test_arr_ms = Tensor(test_arr, dtype=mindspore.float32)
  144. with pytest.raises(ValueError):
  145. test_op(test_arr_ms)
  146. # TEST 4 - 1D Paddings should not work
  147. with pytest.raises(TypeError):
  148. test_op = nn.Pad(paddings=((0, 2)), mode="CONSTANT")
  149. # TEST 5 - Padding beyond 4d - (added check in nn file in PR)
  150. with pytest.raises(ValueError):
  151. _ = nn.Pad(paddings=((0, 0), (0, 0,), (0, 0), (0, 0),
  152. (1, 0)), mode="CONSTANT") # 2D Padding