• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.common.api import ms_function
23from mindspore.ops import operations as P
24from mindspore.ops.operations import _inner_ops as inner
25
26x0 = np.array([[True, True], [True, False], [False, False]])
27axis0 = 0
28keep_dims0 = True
29
30x1 = np.array([[True, True], [True, False], [False, False]])
31axis1 = 0
32keep_dims1 = False
33
34x2 = np.array([[True, True], [True, False], [False, False]])
35axis2 = 1
36keep_dims2 = True
37
38x3 = np.array([[True, True], [True, False], [False, False]])
39axis3 = 1
40keep_dims3 = False
41
42
43class ReduceAny(nn.Cell):
44    def __init__(self):
45        super(ReduceAny, self).__init__()
46
47        self.x0 = Tensor(x0)
48        self.axis0 = axis0
49        self.keep_dims0 = keep_dims0
50
51        self.x1 = Tensor(x1)
52        self.axis1 = axis1
53        self.keep_dims1 = keep_dims1
54
55        self.x2 = Tensor(x2)
56        self.axis2 = axis2
57        self.keep_dims2 = keep_dims2
58
59        self.x3 = Tensor(x3)
60        self.axis3 = axis3
61        self.keep_dims3 = keep_dims3
62
63
64    @ms_function
65    def construct(self):
66        return (P.ReduceAny(self.keep_dims0)(self.x0, self.axis0),
67                P.ReduceAny(self.keep_dims1)(self.x1, self.axis1),
68                P.ReduceAny(self.keep_dims2)(self.x2, self.axis2),
69                P.ReduceAny(self.keep_dims3)(self.x3, self.axis3))
70
71
72@pytest.mark.level0
73@pytest.mark.platform_x86_gpu_training
74@pytest.mark.env_onecard
75def test_ReduceAny():
76    context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
77    reduce_any = ReduceAny()
78    output = reduce_any()
79
80    expect0 = np.any(x0, axis=axis0, keepdims=keep_dims0)
81    assert np.allclose(output[0].asnumpy(), expect0)
82    assert output[0].shape == expect0.shape
83
84    expect1 = np.any(x1, axis=axis1, keepdims=keep_dims1)
85    assert np.allclose(output[1].asnumpy(), expect1)
86    assert output[1].shape == expect1.shape
87
88    expect2 = np.any(x2, axis=axis2, keepdims=keep_dims2)
89    assert np.allclose(output[2].asnumpy(), expect2)
90    assert output[2].shape == expect2.shape
91
92    expect3 = np.any(x3, axis=axis3, keepdims=keep_dims3)
93    assert np.allclose(output[3].asnumpy(), expect3)
94    assert output[3].shape == expect3.shape
95
96
97x_1 = np.array([[True, True], [True, False], [False, False]])
98axis_1 = 0
99x_2 = np.array([[True, True], [True, True], [True, False], [False, False]])
100axis_2 = 0
101
102
103class ReduceAnyDynamic(nn.Cell):
104    def __init__(self, x, axis):
105        super(ReduceAnyDynamic, self).__init__()
106        self.reduceany = P.ReduceAny(False)
107        self.test_dynamic = inner.GpuConvertToDynamicShape()
108        self.x = x
109        self.axis = axis
110
111    def construct(self):
112        dynamic_x = self.test_dynamic(self.x)
113        return self.reduceany(dynamic_x, self.axis)
114
115
116@pytest.mark.level0
117@pytest.mark.platform_x86_gpu_training
118@pytest.mark.env_onecard
119def test_reduce_any_dynamic():
120    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
121    net1 = ReduceAnyDynamic(Tensor(x_1), axis_1)
122    net2 = ReduceAnyDynamic(Tensor(x_2), axis_2)
123
124    expect_1 = np.any(x_1, axis=axis_1, keepdims=False)
125    expect_2 = np.any(x_2, axis=axis_2, keepdims=False)
126
127    output1 = net1()
128    output2 = net2()
129
130    np.testing.assert_almost_equal(output1.asnumpy(), expect_1)
131    np.testing.assert_almost_equal(output2.asnumpy(), expect_2)
132