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_prelu_op.py 7.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. # ============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. from mindspore.ops import composite as C
  22. from mindspore.common import dtype as mstype
  23. class PReLUOpNet(nn.Cell):
  24. def __init__(self):
  25. super(PReLUOpNet, self).__init__()
  26. self.prelu = P.PReLU()
  27. def construct(self, x, weight):
  28. return self.prelu(x, weight)
  29. class PReLUOpGradNet(nn.Cell):
  30. def __init__(self, net):
  31. super(PReLUOpGradNet, self).__init__()
  32. self.forward = net
  33. self.grad = C.GradOperation(get_all=True, sens_param=False)
  34. def construct(self, x, weight):
  35. return self.grad(self.forward)(x, weight)
  36. def judge_result_correct(result, expect):
  37. result = result.asnumpy()
  38. expect = expect.asnumpy()
  39. assert result.dtype == expect.dtype
  40. assert result.shape == expect.shape
  41. assert np.allclose(result, expect, rtol=1.e-2)
  42. def test_prelu(x, weight, expect_forward, expect_dx, expect_dw):
  43. prelu_forward = PReLUOpNet()
  44. prelu_backward = PReLUOpGradNet(prelu_forward)
  45. forward_output = prelu_forward(x, weight)
  46. judge_result_correct(forward_output, expect_forward)
  47. backward_output = prelu_backward(x, weight)
  48. assert len(backward_output) == 2
  49. judge_result_correct(backward_output[0], expect_dx)
  50. judge_result_correct(backward_output[1], expect_dw)
  51. context.set_context(device_target="GPU", mode=context.GRAPH_MODE)
  52. dtypes = [mstype.float16, mstype.float32]
  53. @pytest.mark.level0
  54. @pytest.mark.platform_x86_gpu_training
  55. @pytest.mark.env_onecard
  56. def test_prelu_single_weight():
  57. x = np.arange(-10, 26).reshape((2, 3, 2, 3)) * 0.7
  58. weight = np.array([0.6])
  59. expect_forward = np.where(x >= 0, x, weight * x)
  60. expect_dx = np.where(x > 0, 1, weight)
  61. expect_dw = np.sum(np.where(x >= 0, 0, x)).reshape((1,))
  62. for dtype in dtypes:
  63. x = Tensor(x, dtype)
  64. weight = Tensor(weight, dtype)
  65. expect_forward = Tensor(expect_forward, dtype)
  66. expect_dx = Tensor(expect_dx, dtype)
  67. expect_dw = Tensor(expect_dw, dtype)
  68. test_prelu(x, weight, expect_forward, expect_dx, expect_dw)
  69. @pytest.mark.level0
  70. @pytest.mark.platform_x86_gpu_training
  71. @pytest.mark.env_onecard
  72. def test_prelu_multiple_weight():
  73. x = np.arange(-10, 26).reshape((2, 3, 2, 3)) * 0.6
  74. weight = np.array([0.2, 0.3, 0.4])
  75. expect_forward = np.array([[[[-1.20, -1.08, -0.96],
  76. [-0.84, -0.72, -0.60]],
  77. [[-0.72, -0.54, -0.36],
  78. [-0.18, 0.00, 0.60]],
  79. [[1.20, 1.80, 2.40],
  80. [3.00, 3.60, 4.20]]],
  81. [[[4.80, 5.40, 6.00],
  82. [6.60, 7.20, 7.80]],
  83. [[8.40, 9.00, 9.60],
  84. [10.20, 10.80, 11.40]],
  85. [[12.00, 12.60, 13.20],
  86. [13.80, 14.40, 15.00]]]])
  87. expect_dx = np.array([[[[0.2, 0.2, 0.2],
  88. [0.2, 0.2, 0.2]],
  89. [[0.3, 0.3, 0.3],
  90. [0.3, 0.3, 1.0]],
  91. [[1.0, 1.0, 1.0],
  92. [1.0, 1.0, 1.0]]],
  93. [[[1.0, 1.0, 1.0],
  94. [1.0, 1.0, 1.0]],
  95. [[1.0, 1.0, 1.0],
  96. [1.0, 1.0, 1.0]],
  97. [[1.0, 1.0, 1.0],
  98. [1.0, 1.0, 1.0]]]])
  99. expect_dw = np.array([-27.0, -6.0, 0.0])
  100. for dtype in dtypes:
  101. x = Tensor(x, dtype)
  102. weight = Tensor(weight, dtype)
  103. expect_forward = Tensor(expect_forward, dtype)
  104. expect_dx = Tensor(expect_dx, dtype)
  105. expect_dw = Tensor(expect_dw, dtype)
  106. test_prelu(x, weight, expect_forward, expect_dx, expect_dw)
  107. @pytest.mark.level0
  108. @pytest.mark.platform_x86_gpu_training
  109. @pytest.mark.env_onecard
  110. def test_prelu_single_weight_0_D():
  111. x = np.array(-0.8)
  112. weight = np.array([0.6])
  113. expect_forward = np.array(-0.48)
  114. expect_dx = np.array(0.6)
  115. expect_dw = np.array([-0.8])
  116. for dtype in dtypes:
  117. x = Tensor(x, dtype)
  118. weight = Tensor(weight, dtype)
  119. expect_forward = Tensor(expect_forward, dtype)
  120. expect_dx = Tensor(expect_dx, dtype)
  121. expect_dw = Tensor(expect_dw, dtype)
  122. test_prelu(x, weight, expect_forward, expect_dx, expect_dw)
  123. @pytest.mark.level0
  124. @pytest.mark.platform_x86_gpu_training
  125. @pytest.mark.env_onecard
  126. def test_prelu_single_weight_1_D():
  127. x = np.arange(-10, 26).reshape((36,)) * 0.7
  128. weight = np.array([0.6])
  129. expect_forward = np.where(x >= 0, x, weight * x)
  130. expect_dx = np.where(x > 0, 1, weight)
  131. expect_dw = np.sum(np.where(x >= 0, 0, x)).reshape((1,))
  132. for dtype in dtypes:
  133. x = Tensor(x, dtype)
  134. weight = Tensor(weight, dtype)
  135. expect_forward = Tensor(expect_forward, dtype)
  136. expect_dx = Tensor(expect_dx, dtype)
  137. expect_dw = Tensor(expect_dw, dtype)
  138. test_prelu(x, weight, expect_forward, expect_dx, expect_dw)
  139. @pytest.mark.level0
  140. @pytest.mark.platform_x86_gpu_training
  141. @pytest.mark.env_onecard
  142. def test_prelu_single_weight_2_D():
  143. x = np.arange(-10, 26).reshape((4, 9)) * 0.7
  144. weight = np.array([0.6])
  145. expect_forward = np.where(x >= 0, x, weight * x)
  146. expect_dx = np.where(x > 0, 1, weight)
  147. expect_dw = np.sum(np.where(x >= 0, 0, x)).reshape((1,))
  148. for dtype in dtypes:
  149. x = Tensor(x, dtype)
  150. weight = Tensor(weight, dtype)
  151. expect_forward = Tensor(expect_forward, dtype)
  152. expect_dx = Tensor(expect_dx, dtype)
  153. expect_dw = Tensor(expect_dw, dtype)
  154. test_prelu(x, weight, expect_forward, expect_dx, expect_dw)
  155. @pytest.mark.level0
  156. @pytest.mark.platform_x86_gpu_training
  157. @pytest.mark.env_onecard
  158. def test_prelu_multiple_weight_2_D():
  159. x = np.arange(-6, 6).reshape((3, 4)) * 0.6
  160. weight = np.array([0.2, 0.4, 0.7, 0.9])
  161. expect_forward = np.array([[-0.72, -1.20, -1.68, -1.62],
  162. [-0.24, -0.24, 0.00, 0.60],
  163. [1.20, 1.80, 2.40, 3.00]])
  164. expect_dx = np.array([[0.2, 0.4, 0.7, 0.9],
  165. [0.2, 0.4, 0.7, 1.0],
  166. [1.0, 1.0, 1.0, 1.0]])
  167. expect_dw = np.array([-4.8, -3.6, -2.4, -1.8])
  168. for dtype in dtypes:
  169. x = Tensor(x, dtype)
  170. weight = Tensor(weight, dtype)
  171. expect_forward = Tensor(expect_forward, dtype)
  172. expect_dx = Tensor(expect_dx, dtype)
  173. expect_dw = Tensor(expect_dw, dtype)
  174. test_prelu(x, weight, expect_forward, expect_dx, expect_dw)