• 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# ============================================================================
15import numpy as np
16import pytest
17import mindspore.context as context
18import mindspore.nn as nn
19from mindspore import Tensor
20from mindspore.ops import operations as P
21
22
23class Net(nn.Cell):
24    def __init__(self):
25        super(Net, self).__init__()
26        self.ops = P.LessEqual()
27
28    def construct(self, x, y):
29        return self.ops(x, y)
30
31
32@pytest.mark.level0
33@pytest.mark.platform_x86_cpu
34@pytest.mark.env_onecard
35def test_net_fp32():
36    x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
37    y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
38    x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
39    y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.float32)
40    x2_np = np.random.randint(1, 5, (2, 1, 1, 4)).astype(np.float32)
41    y2_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
42    x3_np = np.random.randint(1, 5, 1).astype(np.float32)
43    y3_np = np.random.randint(1, 5, 1).astype(np.float32)
44    x4_np = np.array(768).astype(np.float32)
45    y4_np = np.array(3072.5).astype(np.float32)
46
47    x0 = Tensor(x0_np)
48    y0 = Tensor(y0_np)
49    x1 = Tensor(x1_np)
50    y1 = Tensor(y1_np)
51    x2 = Tensor(x2_np)
52    y2 = Tensor(y2_np)
53    x3 = Tensor(x3_np)
54    y3 = Tensor(y3_np)
55    x4 = Tensor(x4_np)
56    y4 = Tensor(y4_np)
57
58    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
59    net = Net()
60    out = net(x0, y0).asnumpy()
61    expect = x0_np <= y0_np
62    assert np.all(out == expect)
63    assert out.shape == expect.shape
64
65    out = net(x1, y1).asnumpy()
66    expect = x1_np <= y1_np
67    assert np.all(out == expect)
68    assert out.shape == expect.shape
69
70    out = net(x2, y2).asnumpy()
71    expect = x2_np <= y2_np
72    assert np.all(out == expect)
73    assert out.shape == expect.shape
74
75    out = net(x3, y3).asnumpy()
76    expect = x3_np <= y3_np
77    assert np.all(out == expect)
78    assert out.shape == expect.shape
79
80    out = net(x4, y4).asnumpy()
81    expect = x4_np <= y4_np
82    assert np.all(out == expect)
83    assert out.shape == expect.shape
84
85
86@pytest.mark.level0
87@pytest.mark.platform_x86_cpu
88@pytest.mark.env_onecard
89def test_net_fp16():
90    x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16)
91    y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16)
92    x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16)
93    y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.float16)
94    x2_np = np.random.randint(1, 5, (2, 1, 1, 4)).astype(np.float16)
95    y2_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16)
96    x3_np = np.random.randint(1, 5, 1).astype(np.float16)
97    y3_np = np.random.randint(1, 5, 1).astype(np.float16)
98    x4_np = np.array(768).astype(np.float16)
99    y4_np = np.array(3072.5).astype(np.float16)
100
101    x0 = Tensor(x0_np)
102    y0 = Tensor(y0_np)
103    x1 = Tensor(x1_np)
104    y1 = Tensor(y1_np)
105    x2 = Tensor(x2_np)
106    y2 = Tensor(y2_np)
107    x3 = Tensor(x3_np)
108    y3 = Tensor(y3_np)
109    x4 = Tensor(x4_np)
110    y4 = Tensor(y4_np)
111
112    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
113    net = Net()
114    out = net(x0, y0).asnumpy()
115    expect = x0_np <= y0_np
116    assert np.all(out == expect)
117    assert out.shape == expect.shape
118
119    out = net(x1, y1).asnumpy()
120    expect = x1_np <= y1_np
121    assert np.all(out == expect)
122    assert out.shape == expect.shape
123
124    out = net(x2, y2).asnumpy()
125    expect = x2_np <= y2_np
126    assert np.all(out == expect)
127    assert out.shape == expect.shape
128
129    out = net(x3, y3).asnumpy()
130    expect = x3_np <= y3_np
131    assert np.all(out == expect)
132    assert out.shape == expect.shape
133
134    out = net(x4, y4).asnumpy()
135    expect = x4_np <= y4_np
136    assert np.all(out == expect)
137    assert out.shape == expect.shape
138
139
140@pytest.mark.level0
141@pytest.mark.platform_x86_cpu
142@pytest.mark.env_onecard
143def test_net_int32():
144    x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.int32)
145    y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.int32)
146    x1 = Tensor(x1_np)
147    y1 = Tensor(y1_np)
148
149
150    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
151    net = Net()
152    out = net(x1, y1).asnumpy()
153    expect = x1_np <= y1_np
154    assert np.all(out == expect)
155    assert out.shape == expect.shape
156
157
158@pytest.mark.level0
159@pytest.mark.platform_x86_cpu
160@pytest.mark.env_onecard
161def test_net_int64():
162    x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.int64)
163    y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.int64)
164    x1 = Tensor(x1_np)
165    y1 = Tensor(y1_np)
166
167
168    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
169    net = Net()
170    out = net(x1, y1).asnumpy()
171    expect = x1_np <= y1_np
172    assert np.all(out == expect)
173    assert out.shape == expect.shape
174
175
176@pytest.mark.level0
177@pytest.mark.platform_x86_cpu
178@pytest.mark.env_onecard
179def test_net_float64():
180    x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float64)
181    y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.float64)
182    x1 = Tensor(x1_np)
183    y1 = Tensor(y1_np)
184
185
186    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
187    net = Net()
188    out = net(x1, y1).asnumpy()
189    expect = x1_np <= y1_np
190    assert np.all(out == expect)
191    assert out.shape == expect.shape
192
193
194@pytest.mark.level0
195@pytest.mark.platform_x86_cpu
196@pytest.mark.env_onecard
197def test_net_int16():
198    x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.int16)
199    y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.int16)
200    x1 = Tensor(x1_np)
201    y1 = Tensor(y1_np)
202
203
204    context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
205    net = Net()
206    out = net(x1, y1).asnumpy()
207    expect = x1_np <= y1_np
208    assert np.all(out == expect)
209    assert out.shape == expect.shape
210