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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. class ReduceAny(nn.Cell):
  36. def __init__(self):
  37. super(ReduceAny, self).__init__()
  38. self.x0 = Tensor(x0)
  39. self.axis0 = axis0
  40. self.keep_dims0 = keep_dims0
  41. self.x1 = Tensor(x1)
  42. self.axis1 = axis1
  43. self.keep_dims1 = keep_dims1
  44. self.x2 = Tensor(x2)
  45. self.axis2 = axis2
  46. self.keep_dims2 = keep_dims2
  47. self.x3 = Tensor(x3)
  48. self.axis3 = axis3
  49. self.keep_dims3 = keep_dims3
  50. @ms_function
  51. def construct(self):
  52. return (P.ReduceAny(self.keep_dims0)(self.x0, self.axis0),
  53. P.ReduceAny(self.keep_dims1)(self.x1, self.axis1),
  54. P.ReduceAny(self.keep_dims2)(self.x2, self.axis2),
  55. P.ReduceAny(self.keep_dims3)(self.x3, self.axis3))
  56. @pytest.mark.level0
  57. @pytest.mark.platform_x86_gpu_training
  58. @pytest.mark.env_onecard
  59. def test_ReduceAny():
  60. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  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. x_1 = np.array([[True, True], [True, False], [False, False]])
  76. axis_1 = 0
  77. x_2 = np.array([[True, True], [True, True], [True, False], [False, False]])
  78. axis_2 = 0
  79. class ReduceAnyDynamic(nn.Cell):
  80. def __init__(self, x, axis):
  81. super(ReduceAnyDynamic, self).__init__()
  82. self.reduceany = P.ReduceAny(False)
  83. self.test_dynamic = inner.GpuConvertToDynamicShape()
  84. self.x = x
  85. self.axis = axis
  86. def construct(self):
  87. dynamic_x = self.test_dynamic(self.x)
  88. return self.reduceany(dynamic_x, self.axis)
  89. @pytest.mark.level0
  90. @pytest.mark.platform_x86_gpu_training
  91. @pytest.mark.env_onecard
  92. def test_reduce_any_dynamic():
  93. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  94. net1 = ReduceAnyDynamic(Tensor(x_1), axis_1)
  95. net2 = ReduceAnyDynamic(Tensor(x_2), axis_2)
  96. expect_1 = np.any(x_1, axis=axis_1, keepdims=False)
  97. expect_2 = np.any(x_2, axis=axis_2, keepdims=False)
  98. output1 = net1()
  99. output2 = net2()
  100. np.testing.assert_almost_equal(output1.asnumpy(), expect_1)
  101. np.testing.assert_almost_equal(output2.asnumpy(), expect_2)