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

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