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_reduce_min_op.py 5.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 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.common.api import ms_function
  21. from mindspore.ops import operations as P
  22. x0 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  23. axis0 = 3
  24. keep_dims0 = True
  25. x1 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  26. axis1 = 3
  27. keep_dims1 = False
  28. x2 = np.random.rand(2, 3, 1, 4).astype(np.float32)
  29. axis2 = 2
  30. keep_dims2 = True
  31. x3 = np.random.rand(2, 3, 1, 4).astype(np.float32)
  32. axis3 = 2
  33. keep_dims3 = False
  34. x4 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  35. axis4 = ()
  36. np_axis4 = None
  37. keep_dims4 = True
  38. x5 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  39. axis5 = ()
  40. np_axis5 = None
  41. keep_dims5 = False
  42. x6 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  43. axis6 = -2
  44. keep_dims6 = False
  45. x7 = np.random.rand(2, 3, 4, 4).astype(np.float32)
  46. axis7 = (-2, -1)
  47. keep_dims7 = True
  48. x8 = np.random.rand(1, 1, 1, 1).astype(np.float32)
  49. axis8 = ()
  50. np_axis8 = None
  51. keep_dims8 = True
  52. context.set_context(device_target='GPU')
  53. class ReduceMin(nn.Cell):
  54. def __init__(self):
  55. super(ReduceMin, self).__init__()
  56. self.x0 = Tensor(x0)
  57. self.axis0 = axis0
  58. self.keep_dims0 = keep_dims0
  59. self.x1 = Tensor(x1)
  60. self.axis1 = axis1
  61. self.keep_dims1 = keep_dims1
  62. self.x2 = Tensor(x2)
  63. self.axis2 = axis2
  64. self.keep_dims2 = keep_dims2
  65. self.x3 = Tensor(x3)
  66. self.axis3 = axis3
  67. self.keep_dims3 = keep_dims3
  68. self.x4 = Tensor(x4)
  69. self.axis4 = axis4
  70. self.keep_dims4 = keep_dims4
  71. self.x5 = Tensor(x5)
  72. self.axis5 = axis5
  73. self.keep_dims5 = keep_dims5
  74. self.x6 = Tensor(x6)
  75. self.axis6 = axis6
  76. self.keep_dims6 = keep_dims6
  77. self.x7 = Tensor(x7)
  78. self.axis7 = axis7
  79. self.keep_dims7 = keep_dims7
  80. self.x8 = Tensor(x8)
  81. self.axis8 = axis8
  82. self.keep_dims8 = keep_dims8
  83. @ms_function
  84. def construct(self):
  85. return (P.ReduceMin(self.keep_dims0)(self.x0, self.axis0),
  86. P.ReduceMin(self.keep_dims1)(self.x1, self.axis1),
  87. P.ReduceMin(self.keep_dims2)(self.x2, self.axis2),
  88. P.ReduceMin(self.keep_dims3)(self.x3, self.axis3),
  89. P.ReduceMin(self.keep_dims4)(self.x4, self.axis4),
  90. P.ReduceMin(self.keep_dims5)(self.x5, self.axis5),
  91. P.ReduceMin(self.keep_dims6)(self.x6, self.axis6),
  92. P.ReduceMin(self.keep_dims7)(self.x7, self.axis7),
  93. P.ReduceMin(self.keep_dims8)(self.x8, self.axis8))
  94. @pytest.mark.level0
  95. @pytest.mark.platform_x86_gpu_training
  96. @pytest.mark.env_onecard
  97. def test_ReduceMin():
  98. reduce_min = ReduceMin()
  99. output = reduce_min()
  100. expect0 = np.min(x0, axis=axis0, keepdims=keep_dims0)
  101. diff0 = abs(output[0].asnumpy() - expect0)
  102. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  103. assert np.all(diff0 < error0)
  104. assert output[0].shape == expect0.shape
  105. expect1 = np.min(x1, axis=axis1, keepdims=keep_dims1)
  106. diff1 = abs(output[1].asnumpy() - expect1)
  107. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  108. assert np.all(diff1 < error1)
  109. assert output[1].shape == expect1.shape
  110. expect2 = np.min(x2, axis=axis2, keepdims=keep_dims2)
  111. diff2 = abs(output[2].asnumpy() - expect2)
  112. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  113. assert np.all(diff2 < error2)
  114. assert output[2].shape == expect2.shape
  115. expect3 = np.min(x3, axis=axis3, keepdims=keep_dims3)
  116. diff3 = abs(output[3].asnumpy() - expect3)
  117. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  118. assert np.all(diff3 < error3)
  119. assert output[3].shape == expect3.shape
  120. expect4 = np.min(x4, axis=np_axis4, keepdims=keep_dims4)
  121. diff4 = abs(output[4].asnumpy() - expect4)
  122. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  123. assert np.all(diff4 < error4)
  124. assert output[4].shape == expect4.shape
  125. expect5 = np.min(x5, axis=np_axis5, keepdims=keep_dims5)
  126. diff5 = abs(output[5].asnumpy() - expect5)
  127. error5 = np.ones(shape=expect5.shape) * 1.0e-5
  128. assert np.all(diff5 < error5)
  129. assert output[5].shape == expect5.shape
  130. expect6 = np.min(x6, axis=axis6, keepdims=keep_dims6)
  131. diff6 = abs(output[6].asnumpy() - expect6)
  132. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  133. assert np.all(diff6 < error6)
  134. assert output[6].shape == expect6.shape
  135. expect7 = np.min(x7, axis=axis7, keepdims=keep_dims7)
  136. diff7 = abs(output[7].asnumpy() - expect7)
  137. error7 = np.ones(shape=expect7.shape) * 1.0e-5
  138. assert np.all(diff7 < error7)
  139. expect8 = np.min(x8, axis=np_axis8, keepdims=keep_dims8)
  140. diff8 = abs(output[8].asnumpy() - expect8)
  141. error8 = np.ones(shape=expect8.shape) * 1.0e-5
  142. assert np.all(diff8 < error8)