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 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.ops import operations as P 23 24context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 25 26 27class Net(nn.Cell): 28 def __init__(self): 29 super(Net, self).__init__() 30 self.ops = P.IsFinite() 31 32 def construct(self, x): 33 return self.ops(x) 34 35 36@pytest.mark.level0 37@pytest.mark.platform_x86_cpu 38@pytest.mark.env_onecard 39def test_net(): 40 x0 = Tensor(np.array([np.log(-1), 0.4, np.log(0)]).astype(np.float16)) 41 x1 = Tensor(np.array([np.log(-1), 0.4, np.log(0)]).astype(np.float32)) 42 x2 = Tensor(np.array([np.log(-1), 0.4, np.log(0)]).astype(np.float64)) 43 x3 = Tensor(np.array([4, 1, -5]).astype(np.int8)) 44 x4 = Tensor(np.array([4, 1, -5]).astype(np.int16)) 45 x5 = Tensor(np.array([4, 1, -5]).astype(np.int32)) 46 x6 = Tensor(np.array([4, 1, -5]).astype(np.int64)) 47 x7 = Tensor(np.array([4, 1, -5]).astype(np.uint8)) 48 x8 = Tensor(np.array([4, 1, -5]).astype(np.uint16)) 49 x9 = Tensor(np.array([4, 1, -5]).astype(np.uint32)) 50 x10 = Tensor(np.array([4, 1, -5]).astype(np.uint64)) 51 x11 = Tensor(np.array([False, True, False]).astype(np.bool_)) 52 53 net = Net() 54 out = net(x0).asnumpy() 55 expect = [False, True, False] 56 assert np.all(out == expect) 57 58 out = net(x1).asnumpy() 59 expect = [False, True, False] 60 assert np.all(out == expect) 61 62 out = net(x2).asnumpy() 63 expect = [False, True, False] 64 assert np.all(out == expect) 65 66 out = net(x3).asnumpy() 67 expect = [True, True, True] 68 assert np.all(out == expect) 69 70 out = net(x4).asnumpy() 71 expect = [True, True, True] 72 assert np.all(out == expect) 73 74 out = net(x5).asnumpy() 75 expect = [True, True, True] 76 assert np.all(out == expect) 77 78 out = net(x6).asnumpy() 79 expect = [True, True, True] 80 assert np.all(out == expect) 81 82 out = net(x7).asnumpy() 83 expect = [True, True, True] 84 assert np.all(out == expect) 85 86 out = net(x8).asnumpy() 87 expect = [True, True, True] 88 assert np.all(out == expect) 89 90 out = net(x9).asnumpy() 91 expect = [True, True, True] 92 assert np.all(out == expect) 93 94 out = net(x10).asnumpy() 95 expect = [True, True, True] 96 assert np.all(out == expect) 97 98 out = net(x11).asnumpy() 99 expect = [True, True, True] 100 assert np.all(out == expect) 101