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_any_op.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.array([[True, True], [True, False], [False, False]])
  24. axis0 = 0
  25. keep_dims0 = True
  26. x1 = np.array([[True, True], [True, False], [False, False]])
  27. axis1 = 0
  28. keep_dims1 = False
  29. x2 = np.array([[True, True], [True, False], [False, False]])
  30. axis2 = 1
  31. keep_dims2 = True
  32. x3 = np.array([[True, True], [True, False], [False, False]])
  33. axis3 = 1
  34. keep_dims3 = False
  35. context.set_context(device_target='GPU')
  36. class ReduceAny(nn.Cell):
  37. def __init__(self):
  38. super(ReduceAny, self).__init__()
  39. self.x0 = Tensor(x0)
  40. self.axis0 = axis0
  41. self.keep_dims0 = keep_dims0
  42. self.x1 = Tensor(x1)
  43. self.axis1 = axis1
  44. self.keep_dims1 = keep_dims1
  45. self.x2 = Tensor(x2)
  46. self.axis2 = axis2
  47. self.keep_dims2 = keep_dims2
  48. self.x3 = Tensor(x3)
  49. self.axis3 = axis3
  50. self.keep_dims3 = keep_dims3
  51. @ms_function
  52. def construct(self):
  53. return (P.ReduceAny(self.keep_dims0)(self.x0, self.axis0),
  54. P.ReduceAny(self.keep_dims1)(self.x1, self.axis1),
  55. P.ReduceAny(self.keep_dims2)(self.x2, self.axis2),
  56. P.ReduceAny(self.keep_dims3)(self.x3, self.axis3))
  57. @pytest.mark.level0
  58. @pytest.mark.platform_x86_gpu_training
  59. @pytest.mark.env_onecard
  60. def test_ReduceAny():
  61. reduce_any = ReduceAny()
  62. output = reduce_any()
  63. expect0 = np.any(x0, axis=axis0, keepdims=keep_dims0)
  64. assert np.allclose(output[0].asnumpy(), expect0)
  65. assert output[0].shape == expect0.shape
  66. expect1 = np.any(x1, axis=axis1, keepdims=keep_dims1)
  67. assert np.allclose(output[1].asnumpy(), expect1)
  68. assert output[1].shape == expect1.shape
  69. expect2 = np.any(x2, axis=axis2, keepdims=keep_dims2)
  70. assert np.allclose(output[2].asnumpy(), expect2)
  71. assert output[2].shape == expect2.shape
  72. expect3 = np.any(x3, axis=axis3, keepdims=keep_dims3)
  73. assert np.allclose(output[3].asnumpy(), expect3)
  74. assert output[3].shape == expect3.shape
  75. class ReduceAnyDynamic(nn.Cell):
  76. def __init__(self):
  77. super(ReduceAnyDynamic, self).__init__()
  78. self.reduceany = P.ReduceAny(False)
  79. self.test_dynamic = inner.GpuConvertToDynamicShape()
  80. def construct(self, x, axis):
  81. x = self.test_dynamic(x)
  82. return self.reduceany(x, axis)
  83. @pytest.mark.level0
  84. @pytest.mark.platform_x86_gpu_training
  85. @pytest.mark.env_onecard
  86. def test_reduce_any_dynamic():
  87. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  88. net = ReduceAnyDynamic()
  89. x_1 = np.array([[True, True], [True, False], [False, False]])
  90. axis_1 = 0
  91. expect_1 = np.any(x_1, axis=axis_1, keepdims=False)
  92. x_2 = np.array([[True, True], [True, True], [True, False], [False, False]])
  93. axis_2 = 0
  94. expect_2 = np.any(x_2, axis=axis_2, keepdims=False)
  95. output_1 = net(Tensor(x_1), axis_1)
  96. output_2 = net(Tensor(x_2), axis_2)
  97. np.testing.assert_almost_equal(output_1.asnumpy(), expect_1)
  98. np.testing.assert_almost_equal(output_2.asnumpy(), expect_2)