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 Net(Cell): 26 def __init__(self): 27 super(Net, self).__init__() 28 self.lessequal = P.LessEqual() 29 30 def construct(self, x, y): 31 return self.lessequal(x, y) 32 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_gpu_training 36@pytest.mark.env_onecard 37def test_lessequal(): 38 x = Tensor(np.array([[1, 2, 3]]).astype(np.float32)) 39 y = Tensor(np.array([[2, 2, 2]]).astype(np.float32)) 40 expect = np.array([[True, True, False]]) 41 x1 = Tensor(np.array([[1, 2, 3]]).astype(np.int16)) 42 y1 = Tensor(np.array([[2]]).astype(np.int16)) 43 expect1 = np.array([[True, True, False]]) 44 x2 = Tensor(np.array([[1, 2, 3]]).astype(np.uint8)) 45 y2 = Tensor(np.array([[2]]).astype(np.uint8)) 46 expect2 = np.array([[True, True, False]]) 47 x3 = Tensor(np.array([[1, 2, 3]]).astype(np.float64)) 48 y3 = Tensor(np.array([[2]]).astype(np.float64)) 49 expect3 = np.array([[True, True, False]]) 50 x4 = Tensor(np.array([[1, 2, 3]]).astype(np.float16)) 51 y4 = Tensor(np.array([[2]]).astype(np.float16)) 52 expect4 = np.array([[True, True, False]]) 53 x5 = Tensor(np.array([[1, 2, 3]]).astype(np.int64)) 54 y5 = Tensor(np.array([[2]]).astype(np.int64)) 55 expect5 = np.array([[True, True, False]]) 56 x6 = Tensor(np.array([[1, 2, 3]]).astype(np.int32)) 57 y6 = Tensor(np.array([[2, 2, 2]]).astype(np.int32)) 58 expect6 = np.array([[True, True, False]]) 59 x7 = Tensor(np.array([[1, 2, 3]]).astype(np.int8)) 60 y7 = Tensor(np.array([[2]]).astype(np.int8)) 61 expect7 = np.array([[True, True, False]]) 62 63 x = [x, x1, x2, x3, x4, x5, x6, x7] 64 y = [y, y1, y2, y3, y4, y5, y6, y7] 65 expect = [expect, expect1, expect2, expect3, expect4, expect5, expect6, expect7] 66 67 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 68 lessequal = Net() 69 for i, xi in enumerate(x): 70 output = lessequal(xi, y[i]) 71 assert np.all(output.asnumpy() == expect[i]) 72 assert output.shape == expect[i].shape 73 print('test [%d/%d] passed!' % (i, len(x))) 74 75 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 76 lessequal = Net() 77 for i, xi in enumerate(x): 78 output = lessequal(xi, y[i]) 79 assert np.all(output.asnumpy() == expect[i]) 80 assert output.shape == expect[i].shape 81 print('test [%d/%d] passed!' % (i, len(x))) 82