• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2021 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"""unit tests for numpy logical operations"""
16
17import pytest
18import numpy as onp
19
20import mindspore.numpy as mnp
21from mindspore import context
22
23from .utils import rand_int, rand_bool, run_binop_test, run_logical_test, match_res, \
24    match_all_arrays, to_tensor
25
26context.set_context(mode=context.PYNATIVE_MODE)
27
28
29class Cases():
30    def __init__(self):
31        self.arrs = [
32            rand_int(2),
33            rand_int(2, 3),
34            rand_int(2, 3, 4),
35            rand_int(2, 3, 4, 5),
36        ]
37
38        # scalars expanded across the 0th dimension
39        self.scalars = [
40            rand_int(),
41            rand_int(1),
42            rand_int(1, 1),
43            rand_int(1, 1, 1, 1),
44        ]
45
46        # arrays of the same size expanded across the 0th dimension
47        self.expanded_arrs = [
48            rand_int(2, 3),
49            rand_int(1, 2, 3),
50            rand_int(1, 1, 2, 3),
51            rand_int(1, 1, 1, 2, 3),
52        ]
53
54        # arrays which can be broadcast
55        self.broadcastables = [
56            rand_int(5),
57            rand_int(6, 1),
58            rand_int(7, 1, 5),
59            rand_int(8, 1, 6, 1)
60        ]
61
62        # Boolean arrays
63        self.boolean_arrs = [
64            rand_bool(),
65            rand_bool(5),
66            rand_bool(6, 1),
67            rand_bool(7, 1, 5),
68            rand_bool(8, 1, 6, 1)
69        ]
70
71        # array which contains infs and nans
72        self.infs = onp.array([[1.0, onp.nan], [onp.inf, onp.NINF], [2.3, -4.5], [onp.nan, 0.0]])
73
74
75test_case = Cases()
76
77
78def mnp_not_equal(a, b):
79    return mnp.not_equal(a, b)
80
81
82def onp_not_equal(a, b):
83    return onp.not_equal(a, b)
84
85
86@pytest.mark.level1
87@pytest.mark.platform_arm_ascend_training
88@pytest.mark.platform_x86_ascend_training
89@pytest.mark.platform_x86_gpu_training
90@pytest.mark.platform_x86_cpu
91@pytest.mark.env_onecard
92def test_not_equal():
93    run_binop_test(mnp_not_equal, onp_not_equal, test_case)
94
95
96def mnp_less_equal(a, b):
97    return mnp.less_equal(a, b)
98
99
100def onp_less_equal(a, b):
101    return onp.less_equal(a, b)
102
103
104@pytest.mark.level1
105@pytest.mark.platform_arm_ascend_training
106@pytest.mark.platform_x86_ascend_training
107@pytest.mark.platform_x86_gpu_training
108@pytest.mark.platform_x86_cpu
109@pytest.mark.env_onecard
110def test_less_equal():
111    run_binop_test(mnp_less_equal, onp_less_equal, test_case)
112
113
114def mnp_less(a, b):
115    return mnp.less(a, b)
116
117
118def onp_less(a, b):
119    return onp.less(a, b)
120
121
122@pytest.mark.level1
123@pytest.mark.platform_arm_ascend_training
124@pytest.mark.platform_x86_ascend_training
125@pytest.mark.platform_x86_gpu_training
126@pytest.mark.platform_x86_cpu
127@pytest.mark.env_onecard
128def test_less():
129    run_binop_test(mnp_less, onp_less, test_case)
130
131
132def mnp_greater_equal(a, b):
133    return mnp.greater_equal(a, b)
134
135
136def onp_greater_equal(a, b):
137    return onp.greater_equal(a, b)
138
139
140@pytest.mark.level1
141@pytest.mark.platform_arm_ascend_training
142@pytest.mark.platform_x86_ascend_training
143@pytest.mark.platform_x86_gpu_training
144@pytest.mark.platform_x86_cpu
145@pytest.mark.env_onecard
146def test_greater_equal():
147    run_binop_test(mnp_greater_equal, onp_greater_equal, test_case)
148
149
150def mnp_greater(a, b):
151    return mnp.greater(a, b)
152
153
154def onp_greater(a, b):
155    return onp.greater(a, b)
156
157
158@pytest.mark.level1
159@pytest.mark.platform_arm_ascend_training
160@pytest.mark.platform_x86_ascend_training
161@pytest.mark.platform_x86_gpu_training
162@pytest.mark.platform_x86_cpu
163@pytest.mark.env_onecard
164def test_greater():
165    run_binop_test(mnp_greater, onp_greater, test_case)
166
167
168def mnp_equal(a, b):
169    return mnp.equal(a, b)
170
171
172def onp_equal(a, b):
173    return onp.equal(a, b)
174
175
176def test_equal():
177    run_binop_test(mnp_equal, onp_equal, test_case)
178
179
180def mnp_isfinite(x):
181    return mnp.isfinite(x)
182
183
184def onp_isfinite(x):
185    return onp.isfinite(x)
186
187
188@pytest.mark.level0
189@pytest.mark.platform_arm_ascend_training
190@pytest.mark.platform_x86_ascend_training
191@pytest.mark.platform_x86_gpu_training
192@pytest.mark.platform_x86_cpu
193@pytest.mark.env_onecard
194def test_isfinite():
195    match_res(mnp_isfinite, onp_isfinite, test_case.infs)
196
197
198def mnp_isnan(x):
199    return mnp.isnan(x)
200
201
202def onp_isnan(x):
203    return onp.isnan(x)
204
205
206@pytest.mark.level0
207@pytest.mark.platform_x86_gpu_training
208@pytest.mark.platform_x86_cpu
209@pytest.mark.env_onecard
210def test_isnan():
211    match_res(mnp_isnan, onp_isnan, test_case.infs)
212
213
214def mnp_isinf(x):
215    return mnp.isinf(x)
216
217
218def onp_isinf(x):
219    return onp.isinf(x)
220
221
222@pytest.mark.level0
223@pytest.mark.platform_x86_gpu_training
224@pytest.mark.platform_x86_cpu
225@pytest.mark.env_onecard
226def test_isinf():
227    match_res(mnp_isinf, onp_isinf, test_case.infs)
228
229
230def mnp_isposinf(x):
231    return mnp.isposinf(x)
232
233
234def onp_isposinf(x):
235    return onp.isposinf(x)
236
237
238@pytest.mark.level1
239@pytest.mark.platform_x86_gpu_training
240@pytest.mark.platform_x86_cpu
241@pytest.mark.env_onecard
242def test_isposinf():
243    match_res(mnp_isposinf, onp_isposinf, test_case.infs)
244
245
246def mnp_isneginf(x):
247    return mnp.isneginf(x)
248
249
250def onp_isneginf(x):
251    return onp.isneginf(x)
252
253
254@pytest.mark.level1
255@pytest.mark.platform_x86_gpu_training
256@pytest.mark.platform_x86_cpu
257@pytest.mark.env_onecard
258def test_isneginf():
259    match_res(mnp_isneginf, onp_isneginf, test_case.infs)
260
261
262@pytest.mark.level1
263@pytest.mark.platform_arm_ascend_training
264@pytest.mark.platform_x86_ascend_training
265@pytest.mark.platform_x86_gpu_training
266@pytest.mark.platform_x86_cpu
267@pytest.mark.env_onecard
268def test_isscalar():
269    assert mnp.isscalar(1) == onp.isscalar(1)
270    assert mnp.isscalar(2.3) == onp.isscalar(2.3)
271    assert mnp.isscalar([4.5]) == onp.isscalar([4.5])
272    assert mnp.isscalar(False) == onp.isscalar(False)
273    assert mnp.isscalar(to_tensor(True)) == onp.isscalar(onp.array(True))
274    assert mnp.isscalar('numpy') == onp.isscalar('numpy')
275
276
277@pytest.mark.level0
278@pytest.mark.platform_x86_gpu_training
279@pytest.mark.platform_x86_cpu
280@pytest.mark.env_onecard
281def test_isclose():
282    a = [0, 1, 2, float('inf'), float('inf'), float('nan')]
283    b = [0, 1, -2, float('-inf'), float('inf'), float('nan')]
284    match_all_arrays(mnp.isclose(a, b), onp.isclose(a, b))
285    match_all_arrays(mnp.isclose(a, b, equal_nan=True), onp.isclose(a, b, equal_nan=True))
286
287    a = rand_int(2, 3, 4, 5)
288    diff = (onp.random.random((2, 3, 4, 5)).astype("float32") - 0.5) / 1000
289    b = a + diff
290    match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b), atol=1e-3), onp.isclose(a, b, atol=1e-3))
291    match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b), atol=1e-3, rtol=1e-4),
292                     onp.isclose(a, b, atol=1e-3, rtol=1e-4))
293    match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b), atol=1e-2, rtol=1e-6),
294                     onp.isclose(a, b, atol=1e-2, rtol=1e-6))
295
296    a = rand_int(2, 3, 4, 5)
297    b = rand_int(4, 5)
298    match_all_arrays(mnp.isclose(to_tensor(a), to_tensor(b)), onp.isclose(a, b))
299
300
301@pytest.mark.level1
302@pytest.mark.platform_arm_ascend_training
303@pytest.mark.platform_x86_ascend_training
304@pytest.mark.platform_x86_gpu_training
305@pytest.mark.platform_x86_cpu
306@pytest.mark.env_onecard
307def test_in1d():
308    xi = [rand_int(), rand_int(1), rand_int(10)]
309    yi = [rand_int(), rand_int(1), rand_int(10)]
310    for x in xi:
311        for y in yi:
312            match_res(mnp.in1d, onp.in1d, x, y)
313            match_res(mnp.in1d, onp.in1d, x, y, invert=True)
314
315
316@pytest.mark.level1
317@pytest.mark.platform_arm_ascend_training
318@pytest.mark.platform_x86_ascend_training
319@pytest.mark.platform_x86_gpu_training
320@pytest.mark.platform_x86_cpu
321@pytest.mark.env_onecard
322def test_isin():
323    xi = [rand_int(), rand_int(1), rand_int(10), rand_int(2, 3)]
324    yi = [rand_int(), rand_int(1), rand_int(10), rand_int(2, 3)]
325    for x in xi:
326        for y in yi:
327            match_res(mnp.in1d, onp.in1d, x, y)
328            match_res(mnp.in1d, onp.in1d, x, y, invert=True)
329
330
331def mnp_logical_or(x1, x2):
332    return mnp.logical_or(x1, x2)
333
334
335def onp_logical_or(x1, x2):
336    return onp.logical_or(x1, x2)
337
338
339@pytest.mark.level1
340@pytest.mark.platform_arm_ascend_training
341@pytest.mark.platform_x86_ascend_training
342@pytest.mark.platform_x86_gpu_training
343@pytest.mark.platform_x86_cpu
344@pytest.mark.env_onecard
345def test_logical_or():
346    run_logical_test(mnp_logical_or, onp_logical_or, test_case)
347
348
349def mnp_logical_xor(x1, x2):
350    return mnp.logical_xor(x1, x2)
351
352
353def onp_logical_xor(x1, x2):
354    return onp.logical_xor(x1, x2)
355
356
357@pytest.mark.level1
358@pytest.mark.platform_arm_ascend_training
359@pytest.mark.platform_x86_ascend_training
360@pytest.mark.platform_x86_gpu_training
361@pytest.mark.platform_x86_cpu
362@pytest.mark.env_onecard
363def test_logical_xor():
364    run_logical_test(mnp_logical_xor, onp_logical_xor, test_case)
365
366
367def mnp_logical_and(x1, x2):
368    return mnp.logical_and(x1, x2)
369
370
371def onp_logical_and(x1, x2):
372    return onp.logical_and(x1, x2)
373
374
375@pytest.mark.level1
376@pytest.mark.platform_arm_ascend_training
377@pytest.mark.platform_x86_ascend_training
378@pytest.mark.platform_x86_gpu_training
379@pytest.mark.platform_x86_cpu
380@pytest.mark.env_onecard
381def test_logical_and():
382    run_logical_test(mnp_logical_and, onp_logical_and, test_case)
383
384
385def mnp_logical_not(x):
386    return mnp.logical_not(x)
387
388
389def onp_logical_not(x):
390    return onp.logical_not(x)
391
392
393@pytest.mark.level1
394@pytest.mark.platform_arm_ascend_training
395@pytest.mark.platform_x86_ascend_training
396@pytest.mark.platform_x86_gpu_training
397@pytest.mark.platform_x86_cpu
398@pytest.mark.env_onecard
399def test_logical_not():
400    for arr in test_case.boolean_arrs:
401        expected = onp_logical_not(arr)
402        actual = mnp_logical_not(to_tensor(arr))
403        onp.testing.assert_equal(actual.asnumpy().tolist(), expected.tolist())
404
405
406@pytest.mark.level1
407@pytest.mark.platform_x86_gpu_training
408@pytest.mark.platform_x86_cpu
409@pytest.mark.env_onecard
410def test_array_equal():
411    a = [0, 1, 2, float('inf'), float('nan')]
412    b = [0, 1, 2, float('inf'), float('nan')]
413    match_all_arrays(mnp.array_equal(a, b), onp.array_equal(a, b))
414    a = [0, 1, 2]
415    b = [[0, 1, 2], [0, 1, 2]]
416    assert mnp.array_equal(a, b) == onp.array_equal(a, b)
417
418
419@pytest.mark.level1
420@pytest.mark.platform_x86_gpu_training
421@pytest.mark.platform_x86_cpu
422@pytest.mark.env_onecard
423def test_array_equiv():
424    a = [0, 1, 2, float('inf'), float('nan')]
425    b = [0, 1, 2, float('inf'), float('nan')]
426    match_all_arrays(mnp.array_equal(a, b), onp.array_equal(a, b))
427    a = [0, 1, 2]
428    b = [[0, 1, 2], [0, 1, 2]]
429    assert mnp.array_equal(a, b) == onp.array_equal(a, b)
430
431
432def mnp_signbit(*arrs):
433    arr1 = arrs[0]
434    arr2 = arrs[1]
435    a = mnp.signbit(arr1)
436    b = mnp.signbit(arr2, dtype=mnp.bool_)
437    return a, b
438
439
440def onp_signbit(*arrs):
441    arr1 = arrs[0]
442    arr2 = arrs[1]
443    a = onp.signbit(arr1)
444    b = onp.signbit(arr2, dtype='bool')
445    return a, b
446
447
448@pytest.mark.level1
449@pytest.mark.platform_arm_ascend_training
450@pytest.mark.platform_x86_ascend_training
451@pytest.mark.platform_x86_gpu_training
452@pytest.mark.platform_x86_cpu
453@pytest.mark.env_onecard
454def test_signbit():
455    onp_arrs = [onp.arange(-10, 10).astype('float32'), onp.arange(-10, 10).astype('int32')]
456    mnp_arrs = [mnp.arange(-10, 10).astype('float32'), mnp.arange(-10, 10).astype('int32')]
457    for actual, expected in zip(mnp_signbit(*mnp_arrs), onp_signbit(*onp_arrs)):
458        onp.testing.assert_equal(actual.asnumpy().tolist(), expected.tolist())
459
460
461def mnp_sometrue(x):
462    a = mnp.sometrue(x)
463    b = mnp.sometrue(x, axis=0)
464    c = mnp.sometrue(x, axis=(0, -1))
465    d = mnp.sometrue(x, axis=(0, 1), keepdims=True)
466    e = mnp.sometrue(x, axis=(0, 1), keepdims=-1)
467    f = mnp.sometrue(x, axis=(0, 1), keepdims=0)
468    return a, b, c, d, e, f
469
470
471def onp_sometrue(x):
472    a = onp.sometrue(x)
473    b = onp.sometrue(x, axis=0)
474    c = onp.sometrue(x, axis=(0, -1))
475    d = onp.sometrue(x, axis=(0, 1), keepdims=True)
476    e = onp.sometrue(x, axis=(0, 1), keepdims=-1)
477    f = onp.sometrue(x, axis=(0, 1), keepdims=0)
478    return a, b, c, d, e, f
479
480
481@pytest.mark.level1
482@pytest.mark.platform_arm_ascend_training
483@pytest.mark.platform_x86_ascend_training
484@pytest.mark.platform_x86_gpu_training
485@pytest.mark.platform_x86_cpu
486@pytest.mark.env_onecard
487def test_sometrue():
488    onp_arr = onp.full((3, 2), [True, False])
489    mnp_arr = to_tensor(onp_arr)
490    for actual, expected in zip(mnp_sometrue(mnp_arr), onp_sometrue(onp_arr)):
491        onp.testing.assert_equal(actual.asnumpy().tolist(), expected.tolist())
492