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_prod_op.py 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. class ReduceProd(nn.Cell):
  22. def __init__(self, keep_dims):
  23. super(ReduceProd, self).__init__()
  24. self.reduce_prod = P.ReduceProd(keep_dims=keep_dims)
  25. def construct(self, x, axis):
  26. return self.reduce_prod(x, axis)
  27. @pytest.mark.level0
  28. @pytest.mark.platform_x86_gpu_training
  29. @pytest.mark.env_onecard
  30. @pytest.mark.parametrize('decimal, dtype',
  31. [(1e-10, np.int8), (1e-3, np.float16), (1e-5, np.float32), (1e-8, 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_prod(decimal, dtype, shape, axis, keep_dims):
  37. """
  38. Feature: ALL To ALL
  39. Description: test cases for ReduceProd
  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_prod = ReduceProd(keep_dims)
  46. ms_axis = axis if axis is not None else ()
  47. output = reduce_prod(tensor_x, ms_axis)
  48. expect = np.prod(x, axis=axis, keepdims=keep_dims)
  49. diff = abs(output.asnumpy() - expect)
  50. error = np.ones(shape=expect.shape) * decimal
  51. assert np.all(diff < error)
  52. assert output.shape == expect.shape