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 16import numpy as np 17import pytest 18 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor, ops 22 23 24class Net(nn.Cell): 25 def __init__(self): 26 super(Net, self).__init__() 27 self.sign = ops.Sign() 28 29 def construct(self, x): 30 return self.sign(x) 31 32 33def generate_testcases(nptype): 34 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 35 x = np.array([2.0, 0.0, -1.0]).astype(nptype) 36 net = Net() 37 output = net(Tensor(x)) 38 expect = np.array([1.0, 0.0, -1.0]).astype(nptype) 39 np.testing.assert_almost_equal(output.asnumpy(), expect) 40 41 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 42 x = np.array([2.0, 0.0, -1.0]).astype(nptype) 43 net = Net() 44 output = net(Tensor(x)) 45 expect = np.array([1.0, 0.0, -1.0]).astype(nptype) 46 np.testing.assert_almost_equal(output.asnumpy(), expect) 47 48 49@pytest.mark.level0 50@pytest.mark.platform_x86_gpu_training 51@pytest.mark.env_onecard 52def test_sign_int32(): 53 generate_testcases(np.int32) 54 55 56@pytest.mark.level0 57@pytest.mark.platform_x86_gpu_training 58@pytest.mark.env_onecard 59def test_sign_float32(): 60 generate_testcases(np.float32) 61 62 63@pytest.mark.level0 64@pytest.mark.platform_x86_gpu_training 65@pytest.mark.env_onecard 66def test_sign_float16(): 67 generate_testcases(np.float16) 68