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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.ops import operations as P
  21. from mindspore.ops.operations import _inner_ops as inner
  22. class ReduceMin(nn.Cell):
  23. def __init__(self, keep_dims):
  24. super(ReduceMin, self).__init__()
  25. self.reduce_min = P.ReduceMin(keep_dims=keep_dims)
  26. def construct(self, x, axis):
  27. return self.reduce_min(x, axis)
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. @pytest.mark.parametrize('dtype', [np.float16, np.float32, np.float64])
  32. @pytest.mark.parametrize('shape, axis, keep_dims',
  33. [((2, 3, 4, 4), 3, True), ((2, 3, 4, 4), 3, False), ((2, 3, 1, 4), 2, True),
  34. ((2, 3, 1, 4), 2, False), ((2, 3, 4, 4), None, True), ((2, 3, 4, 4), None, False),
  35. ((2, 3, 4, 4), -2, False), ((2, 3, 4, 4), (-2, -1), False), ((1, 1, 1, 1), None, True)])
  36. def test_reduce_min(dtype, shape, axis, keep_dims):
  37. """
  38. Feature: ALL To ALL
  39. Description: test cases for ReduceMin
  40. Expectation: the result match to numpy
  41. """
  42. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  43. x = np.random.rand(*shape).astype(dtype)
  44. tensor_x = Tensor(x)
  45. reduce_min = ReduceMin(keep_dims)
  46. ms_axis = axis if axis is not None else ()
  47. output = reduce_min(tensor_x, ms_axis)
  48. expect = np.min(x, axis=axis, keepdims=keep_dims)
  49. diff = abs(output.asnumpy() - expect)
  50. error = np.ones(shape=expect.shape) * 1.0e-5
  51. assert np.all(diff < error)
  52. assert output.shape == expect.shape
  53. class ReduceMinDynamic(nn.Cell):
  54. def __init__(self, x, axis):
  55. super(ReduceMinDynamic, self).__init__()
  56. self.reduce_min = P.ReduceMin(False)
  57. self.test_dynamic = inner.GpuConvertToDynamicShape()
  58. self.x = x
  59. self.axis = axis
  60. def construct(self):
  61. dynamic_x = self.test_dynamic(self.x)
  62. return self.reduce_min(dynamic_x, self.axis)
  63. @pytest.mark.level0
  64. @pytest.mark.platform_x86_gpu_training
  65. @pytest.mark.env_onecard
  66. @pytest.mark.parametrize('dtype', [np.float32])
  67. @pytest.mark.parametrize('shape, axis, keep_dims',
  68. [((1, 1, 1, 1), 0, False), ((2, 3, 4, 4), 0, False)])
  69. def test_reduce_min_dynamic(dtype, shape, axis, keep_dims):
  70. """
  71. Feature: ALL To ALL
  72. Description: test cases for ReduceMin with dynamic shape
  73. Expectation: the result match to numpy
  74. """
  75. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  76. x = np.random.rand(*shape).astype(dtype)
  77. ms_axis = axis if axis is not None else ()
  78. net = ReduceMinDynamic(Tensor(x), ms_axis)
  79. expect = np.min(x, axis=axis, keepdims=keep_dims)
  80. output = net()
  81. np.testing.assert_almost_equal(output.asnumpy(), expect)