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_max_op.py 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # Copyright 2019 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. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. from mindspore.common.api import ms_function
  20. import numpy as np
  21. import mindspore.context as context
  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. context.set_context(device_target='GPU')
  49. class ReduceMax(nn.Cell):
  50. def __init__(self):
  51. super(ReduceMax, self).__init__()
  52. self.x0 = Tensor(x0)
  53. self.axis0 = axis0
  54. self.keep_dims0 = keep_dims0
  55. self.x1 = Tensor(x1)
  56. self.axis1 = axis1
  57. self.keep_dims1 = keep_dims1
  58. self.x2 = Tensor(x2)
  59. self.axis2 = axis2
  60. self.keep_dims2 = keep_dims2
  61. self.x3 = Tensor(x3)
  62. self.axis3 = axis3
  63. self.keep_dims3 = keep_dims3
  64. self.x4 = Tensor(x4)
  65. self.axis4 = axis4
  66. self.keep_dims4 = keep_dims4
  67. self.x5 = Tensor(x5)
  68. self.axis5 = axis5
  69. self.keep_dims5 = keep_dims5
  70. self.x6 = Tensor(x6)
  71. self.axis6 = axis6
  72. self.keep_dims6 = keep_dims6
  73. self.x7 = Tensor(x7)
  74. self.axis7 = axis7
  75. self.keep_dims7 = keep_dims7
  76. @ms_function
  77. def construct(self):
  78. return (P.ReduceMax(self.keep_dims0)(self.x0, self.axis0),
  79. P.ReduceMax(self.keep_dims1)(self.x1, self.axis1),
  80. P.ReduceMax(self.keep_dims2)(self.x2, self.axis2),
  81. P.ReduceMax(self.keep_dims3)(self.x3, self.axis3),
  82. P.ReduceMax(self.keep_dims4)(self.x4, self.axis4),
  83. P.ReduceMax(self.keep_dims5)(self.x5, self.axis5),
  84. P.ReduceMax(self.keep_dims6)(self.x6, self.axis6),
  85. P.ReduceMax(self.keep_dims7)(self.x7, self.axis7))
  86. @pytest.mark.level0
  87. @pytest.mark.platform_x86_gpu_training
  88. @pytest.mark.env_onecard
  89. def test_ReduceMax():
  90. reduce_max = ReduceMax()
  91. output = reduce_max()
  92. expect0 = np.max(x0, axis=axis0, keepdims=keep_dims0)
  93. diff0 = output[0].asnumpy() - expect0
  94. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  95. assert np.all(diff0 < error0)
  96. assert (output[0].shape() == expect0.shape)
  97. expect1 = np.max(x1, axis=axis1, keepdims=keep_dims1)
  98. diff1 = output[1].asnumpy() - expect1
  99. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  100. assert np.all(diff1 < error1)
  101. assert (output[1].shape() == expect1.shape)
  102. expect2 = np.max(x2, axis=axis2, keepdims=keep_dims2)
  103. diff2 = output[2].asnumpy() - expect2
  104. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  105. assert np.all(diff2 < error2)
  106. assert (output[2].shape() == expect2.shape)
  107. expect3 = np.max(x3, axis=axis3, keepdims=keep_dims3)
  108. diff3 = output[3].asnumpy() - expect3
  109. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  110. assert np.all(diff3 < error3)
  111. assert (output[3].shape() == expect3.shape)
  112. expect4 = np.max(x4, axis=np_axis4, keepdims=keep_dims4)
  113. diff4 = output[4].asnumpy() - expect4
  114. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  115. assert np.all(diff4 < error4)
  116. assert (output[4].shape() == expect4.shape)
  117. expect5 = np.max(x5, axis=np_axis5, keepdims=keep_dims5)
  118. diff5 = output[5].asnumpy() - expect5
  119. error5 = np.ones(shape=expect5.shape) * 1.0e-5
  120. assert np.all(diff5 < error5)
  121. assert (output[5].shape() == expect5.shape)
  122. expect6 = np.max(x6, axis=axis6, keepdims=keep_dims6)
  123. diff6 = output[6].asnumpy() - expect6
  124. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  125. assert np.all(diff6 < error6)
  126. assert (output[6].shape() == expect6.shape)
  127. expect7 = np.max(x7, axis=axis7, keepdims=keep_dims7)
  128. diff7 = output[7].asnumpy() - expect7
  129. error7 = np.ones(shape=expect7.shape) * 1.0e-5
  130. assert np.all(diff7 < error7)