• 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 ConstScalarAndTensorMinimum(Cell):
26    def __init__(self):
27        super(ConstScalarAndTensorMinimum, self).__init__()
28        self.min = P.Minimum()
29        self.x = 20
30
31    def construct(self, y):
32        return self.min(self.x, y)
33
34
35class TwoTensorsMinimum(Cell):
36    def __init__(self):
37        super(TwoTensorsMinimum, self).__init__()
38        self.min = P.Minimum()
39
40    def construct(self, x, y):
41        return self.min(x, y)
42
43
44@pytest.mark.level0
45@pytest.mark.platform_x86_cpu
46@pytest.mark.env_onecard
47def test_minimum_constScalar_tensor_int():
48    x = Tensor(np.array([[2, 3, 4], [100, 200, 300]]).astype(np.int32))
49    expect = [[2, 3, 4], [20, 20, 20]]
50
51    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
52    min_op = ConstScalarAndTensorMinimum()
53    output = min_op(x)
54    assert np.all(output.asnumpy() == expect)
55
56
57@pytest.mark.level0
58@pytest.mark.platform_x86_cpu
59@pytest.mark.env_onecard
60def test_minimum_two_tensors_Not_Broadcast_int():
61    prop = 100 if np.random.random() > 0.5 else -100
62    x = np.random.randn(3, 4, 5).astype(np.int32) * prop
63    y = np.random.randn(3, 4, 5).astype(np.int32) * prop
64    expect = np.minimum(x, y).astype(np.int32)
65
66    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
67    min_op = TwoTensorsMinimum()
68    output = min_op(Tensor(x), Tensor(y))
69    assert np.all(output.asnumpy() == expect)
70
71
72@pytest.mark.level0
73@pytest.mark.platform_x86_cpu
74@pytest.mark.env_onecard
75def test_minimum_two_tensors_Broadcast_int():
76    prop = 100 if np.random.random() > 0.5 else -100
77    x = np.random.randn(3, 4, 5).astype(np.int32) * prop
78    y = np.random.randn(3, 1, 1).astype(np.int32) * prop
79    expect = np.minimum(x, y).astype(np.int32)
80
81    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
82    min_op = TwoTensorsMinimum()
83    output = min_op(Tensor(x), Tensor(y))
84    assert np.all(output.asnumpy() == expect)
85
86
87@pytest.mark.level0
88@pytest.mark.platform_x86_cpu
89@pytest.mark.env_onecard
90def test_minimum_two_tensors_Broadcast_oneDimension_int():
91    prop = 100 if np.random.random() > 0.5 else -100
92    x = np.random.randn(3).astype(np.int32) * prop
93    y = np.random.randn(3).astype(np.int32) * prop
94    expect = np.minimum(x, y).astype(np.int32)
95
96    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
97    min_op = TwoTensorsMinimum()
98    output = min_op(Tensor(x), Tensor(y))
99    assert np.all(output.asnumpy() == expect)
100
101
102@pytest.mark.level0
103@pytest.mark.platform_x86_cpu
104@pytest.mark.env_onecard
105def test_minimum_two_tensors_notBroadcast_all_oneDimension_int():
106    x = Tensor(np.array([[2]]).astype(np.int32))
107    y = Tensor(np.array([[100]]).astype(np.int32))
108    expect = [[2]]
109
110    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
111    min_op = TwoTensorsMinimum()
112    output = min_op(x, y)
113    assert np.all(output.asnumpy() == expect)
114
115
116@pytest.mark.level0
117@pytest.mark.platform_x86_cpu
118@pytest.mark.env_onecard
119def test_minimum_two_tensors_notBroadcast_float32():
120    prop = 100 if np.random.random() > 0.5 else -100
121    x = np.random.randn(3, 4, 5).astype(np.float32) * prop
122    y = np.random.randn(3, 4, 5).astype(np.float32) * prop
123    expect = np.minimum(x, y).astype(np.float32)
124    error = np.ones(shape=expect.shape) * 1.0e-5
125
126    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
127    min_op = TwoTensorsMinimum()
128    output = min_op(Tensor(x), Tensor(y))
129    diff = output.asnumpy() - expect
130    assert np.all(np.abs(diff) < error)
131    assert output.shape == expect.shape
132
133
134@pytest.mark.level0
135@pytest.mark.platform_x86_cpu
136@pytest.mark.env_onecard
137def test_minimum_two_tensors_notBroadcast_float16():
138    prop = 100 if np.random.random() > 0.5 else -100
139    x = np.random.randn(3, 4, 5).astype(np.float16) * prop
140    y = np.random.randn(3, 4, 5).astype(np.float16) * prop
141    expect = np.minimum(x, y).astype(np.float16)
142    error = np.ones(shape=expect.shape) * 1.0e-5
143
144    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
145    min_op = TwoTensorsMinimum()
146    output = min_op(Tensor(x), Tensor(y))
147    diff = output.asnumpy() - expect
148    assert np.all(np.abs(diff) < error)
149    assert output.shape == expect.shape
150
151
152@pytest.mark.level0
153@pytest.mark.platform_x86_cpu
154@pytest.mark.env_onecard
155def test_minimum_two_tensors_Broadcast_float16():
156    prop = 100 if np.random.random() > 0.5 else -100
157    x = np.random.randn(3, 4, 5).astype(np.float16) * prop
158    y = np.random.randn(3, 4, 1).astype(np.float16) * prop
159    expect = np.minimum(x, y).astype(np.float16)
160    error = np.ones(shape=expect.shape) * 1.0e-5
161
162    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
163    min_op = TwoTensorsMinimum()
164    output = min_op(Tensor(x), Tensor(y))
165    diff = output.asnumpy() - expect
166    assert np.all(np.abs(diff) < error)
167    assert output.shape == expect.shape
168
169
170@pytest.mark.level0
171@pytest.mark.platform_x86_cpu
172@pytest.mark.env_onecard
173def test_minimum_two_tensors_notBroadcast_float64():
174    prop = 100 if np.random.random() > 0.5 else -100
175    x = np.random.randn(3, 4, 1).astype(np.float64) * prop
176    y = np.random.randn(3, 4, 5).astype(np.float64) * prop
177    expect = np.minimum(x, y).astype(np.float64)
178    error = np.ones(shape=expect.shape) * 1.0e-5
179
180    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
181    min_op = TwoTensorsMinimum()
182    output = min_op(Tensor(x), Tensor(y))
183    diff = output.asnumpy() - expect
184    assert np.all(np.abs(diff) < error)
185    assert output.shape == expect.shape
186