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 22from mindspore.ops import operations as P 23from mindspore.ops.composite import GradOperation 24from mindspore.ops.operations import _inner_ops as inner 25 26class Grad(nn.Cell): 27 def __init__(self, network): 28 super(Grad, self).__init__() 29 self.grad = GradOperation(get_all=True, sens_param=True) 30 self.network = network 31 32 def construct(self, input_x, dout): 33 return self.grad(self.network)(input_x, dout) 34 35 36class Net(nn.Cell): 37 def __init__(self): 38 super(Net, self).__init__() 39 self.HSigmoid = P.HSigmoid() 40 41 def construct(self, x): 42 return self.HSigmoid(x) 43 44 45class DynamicNet(nn.Cell): 46 def __init__(self): 47 super(DynamicNet, self).__init__() 48 self.HSigmoid = P.HSigmoid() 49 self.d = inner.GpuConvertToDynamicShape() 50 51 def construct(self, x): 52 x = self.d(x) 53 return self.HSigmoid(x) 54 55 56def generate_testcases(nptype): 57 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 58 x = np.array([-1, -2, 0, 4, 5]).astype(nptype) 59 net = Net() 60 output = net(Tensor(x)) 61 expect = np.array([0.33333334, 0.16666667, 0.5, 1, 1]).astype(nptype) 62 np.testing.assert_almost_equal(output.asnumpy(), expect) 63 64 sens = np.array([-1.45, 0.63, 0.34, 6.43, 34.6]).astype(nptype) 65 backward_net = Grad(Net()) 66 output = backward_net(Tensor(x), Tensor(sens)) 67 expect = np.array([-0.2416667, 0.1049999, 5.66666685e-02, 0, 0]).astype(nptype) 68 np.testing.assert_almost_equal(output[0].asnumpy(), expect) 69 70 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 71 x = np.array([-1, -2, 0, 4, 5]).astype(nptype) 72 net = Net() 73 output = net(Tensor(x)) 74 expect = np.array([0.33333334, 0.16666667, 0.5, 1, 1]).astype(nptype) 75 np.testing.assert_almost_equal(output.asnumpy(), expect) 76 77 sens = np.array([-1.45, 0.63, 0.34, 6.43, 34.6]).astype(nptype) 78 backward_net = Grad(Net()) 79 output = backward_net(Tensor(x), Tensor(sens)) 80 expect = np.array([-0.2416667, 0.1049999, 5.66666685e-02, 0, 0]).astype(nptype) 81 np.testing.assert_almost_equal(output[0].asnumpy(), expect) 82 83 84def generate_dynamic_testcase(nptype): 85 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 86 x = np.array([-1, -2, 0, 2, 1]).astype(nptype) 87 net = DynamicNet() 88 output = net(Tensor(x)) 89 expect = np.array([0.33333334, 0.16666667, 0.5, 0.8333333, 0.6666667]).astype(nptype) 90 np.testing.assert_almost_equal(output.asnumpy(), expect) 91 92 93@pytest.mark.level0 94@pytest.mark.platform_x86_gpu_training 95@pytest.mark.env_onecard 96def test_hsigmoid_dynamic_float32(): 97 generate_dynamic_testcase(np.float32) 98 99 100@pytest.mark.level0 101@pytest.mark.platform_x86_gpu_training 102@pytest.mark.env_onecard 103def test_hsigmoid_float32(): 104 generate_testcases(np.float32) 105 106 107@pytest.mark.level0 108@pytest.mark.platform_x86_gpu_training 109@pytest.mark.env_onecard 110def test_hsigmoid_float16(): 111 generate_testcases(np.float16) 112