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 NetAnd(Cell): 26 def __init__(self): 27 super(NetAnd, self).__init__() 28 self.logicaland = P.LogicalAnd() 29 30 def construct(self, input_x, input_y): 31 return self.logicaland(input_x, input_y) 32 33 34class NetOr(Cell): 35 def __init__(self): 36 super(NetOr, self).__init__() 37 self.logicalor = P.LogicalOr() 38 39 def construct(self, input_x, input_y): 40 return self.logicalor(input_x, input_y) 41 42 43class NetNot(Cell): 44 def __init__(self): 45 super(NetNot, self).__init__() 46 self.logicalnot = P.LogicalNot() 47 48 def construct(self, input_x): 49 return self.logicalnot(input_x) 50 51 52x = np.array([True, False, False]).astype(np.bool) 53y = np.array([False]).astype(np.bool) 54 55 56@pytest.mark.level0 57@pytest.mark.platform_x86_gpu_training 58@pytest.mark.env_onecard 59def test_logicaland(): 60 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 61 logicaland = NetAnd() 62 output = logicaland(Tensor(x), Tensor(y)) 63 assert np.all(output.asnumpy() == np.logical_and(x, y)) 64 65 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 66 logicaland = NetAnd() 67 output = logicaland(Tensor(x), Tensor(y)) 68 assert np.all(output.asnumpy() == np.logical_and(x, y)) 69 70 71@pytest.mark.level0 72@pytest.mark.platform_x86_gpu_training 73@pytest.mark.env_onecard 74def test_logicalor(): 75 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 76 logicalor = NetOr() 77 output = logicalor(Tensor(x), Tensor(y)) 78 assert np.all(output.asnumpy() == np.logical_or(x, y)) 79 80 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 81 logicalor = NetOr() 82 output = logicalor(Tensor(x), Tensor(y)) 83 assert np.all(output.asnumpy() == np.logical_or(x, y)) 84 85 86@pytest.mark.level0 87@pytest.mark.platform_x86_gpu_training 88@pytest.mark.env_onecard 89def test_logicalnot(): 90 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 91 logicalnot = NetNot() 92 output = logicalnot(Tensor(x)) 93 assert np.all(output.asnumpy() == np.logical_not(x)) 94 95 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 96 logicalnot = NetNot() 97 output = logicalnot(Tensor(x)) 98 assert np.all(output.asnumpy() == np.logical_not(x)) 99