• 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
20from mindspore.common.tensor import Tensor
21from mindspore.nn import Cell
22from mindspore.ops import operations as P
23
24
25class ConstScalarAndTensorMaximum(Cell):
26    def __init__(self):
27        super(ConstScalarAndTensorMaximum, self).__init__()
28        self.max = P.Maximum()
29        self.x = 20
30
31    def construct(self, y):
32        return self.max(self.x, y)
33
34
35class TwoTensorsMaximum(Cell):
36    def __init__(self):
37        super(TwoTensorsMaximum, self).__init__()
38        self.max = P.Maximum()
39
40    def construct(self, x, y):
41        return self.max(x, y)
42
43
44@pytest.mark.level0
45@pytest.mark.platform_x86_cpu
46@pytest.mark.env_onecard
47def test_maximum_constScalar_tensor_int():
48    x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
49    expect = [[20, 20, 20], [100, 200, 300]]
50    error = np.ones(shape=[2, 3]) * 1.0e-5
51
52    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
53    max_op = ConstScalarAndTensorMaximum()
54    output = max_op(x)
55    diff = output.asnumpy() - expect
56    assert np.all(diff < error)
57    assert np.all(-diff < error)
58
59
60@pytest.mark.level0
61@pytest.mark.platform_x86_cpu
62@pytest.mark.env_onecard
63def test_maximum_two_tensors_Not_Broadcast_int():
64    x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
65    y = Tensor(np.array([[1, 2, 3], [100, 100, 200]]).astype(np.int32))
66    expect = [[2, 3, 4], [100, 200, 300]]
67    error = np.ones(shape=[2, 3]) * 1.0e-5
68
69    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
70    max_op = TwoTensorsMaximum()
71    output = max_op(x, y)
72    diff = output.asnumpy() - expect
73    assert np.all(diff < error)
74    assert np.all(-diff < error)
75
76
77@pytest.mark.level0
78@pytest.mark.platform_x86_cpu
79@pytest.mark.env_onecard
80def test_maximum_two_tensors_Broadcast_int():
81    x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
82    y = Tensor(np.array([[100, 100, 200]]).astype(np.int32))
83    expect = [[100, 100, 200], [100, 200, 300]]
84    error = np.ones(shape=[2, 3]) * 1.0e-5
85
86    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
87    max_op = TwoTensorsMaximum()
88    output = max_op(x, y)
89    diff = output.asnumpy() - expect
90    assert np.all(diff < error)
91    assert np.all(-diff < error)
92
93
94@pytest.mark.level0
95@pytest.mark.platform_x86_cpu
96@pytest.mark.env_onecard
97def test_maximum_two_tensors_Broadcast_oneDimension_int():
98    x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
99    y = Tensor(np.array([[100]]).astype(np.int32))
100    expect = [[100, 100, 100], [100, 200, 300]]
101    error = np.ones(shape=[2, 3]) * 1.0e-5
102
103    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
104    max_op = TwoTensorsMaximum()
105    output = max_op(x, y)
106    diff = output.asnumpy() - expect
107    assert np.all(diff < error)
108    assert np.all(-diff < error)
109
110
111@pytest.mark.level0
112@pytest.mark.platform_x86_cpu
113@pytest.mark.env_onecard
114def test_maximum_two_tensors_notBroadcast_all_oneDimension_int():
115    x = Tensor(np.array([[2]]).astype(np.int32))
116    y = Tensor(np.array([[100]]).astype(np.int32))
117    expect = [[100]]
118    error = np.ones(shape=[1, 1]) * 1.0e-5
119
120    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
121    max_op = TwoTensorsMaximum()
122    output = max_op(x, y)
123    diff = output.asnumpy() - expect
124    assert np.all(diff < error)
125    assert np.all(-diff < error)
126
127
128@pytest.mark.level0
129@pytest.mark.platform_x86_cpu
130@pytest.mark.env_onecard
131def test_maximum_two_tensors_notBroadcast_float32():
132    x = Tensor(np.array([[2.0, 2.0], [-1, 100]]).astype(np.float32))
133    y = Tensor(np.array([[1.0, 2.1], [-0.8, 100.5]]).astype(np.float32))
134    expect = [[2.0, 2.1], [-0.8, 100.5]]
135    error = np.ones(shape=[2, 2]) * 1.0e-5
136
137    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
138    max_op = TwoTensorsMaximum()
139    output = max_op(x, y)
140    diff = output.asnumpy() - expect
141    assert np.all(diff < error)
142    assert np.all(-diff < error)
143
144
145@pytest.mark.level0
146@pytest.mark.platform_x86_cpu
147@pytest.mark.env_onecard
148def test_maximum_two_tensors_notBroadcast_float64():
149    x = Tensor(np.array([[2.0, 2.0], [-1, 100]]).astype(np.float64))
150    y = Tensor(np.array([[1.0, 2.1], [-0.8, 100.5]]).astype(np.float64))
151    expect = [[2.0, 2.1], [-0.8, 100.5]]
152    error = np.ones(shape=[2, 2]) * 1.0e-5
153
154    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
155    max_op = TwoTensorsMaximum()
156    output = max_op(x, y)
157    diff = output.asnumpy() - expect
158    assert np.all(diff < error)
159    assert np.all(-diff < error)
160