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.operations import _grad_ops as G 24 25 26class NetSigmoid(nn.Cell): 27 def __init__(self): 28 super(NetSigmoid, self).__init__() 29 self.sigmoid = P.Sigmoid() 30 31 def construct(self, x): 32 return self.sigmoid(x) 33 34 35class NetSigmoidGrad(nn.Cell): 36 def __init__(self): 37 super(NetSigmoidGrad, self).__init__() 38 self.sigmoid_grad = G.SigmoidGrad() 39 40 def construct(self, y, dy): 41 return self.sigmoid_grad(y, dy) 42 43 44@pytest.mark.level0 45@pytest.mark.platform_x86_gpu_training 46@pytest.mark.env_onecard 47def test_sigmoid(): 48 x = Tensor(np.array([[[[-1, 1, 10], 49 [1, -1, 1], 50 [10, 1, -1]]]]).astype(np.float32)) 51 52 error = np.ones(shape=[1, 1, 3, 3]) * 1.0e-6 53 54 context.set_context(mode=context.GRAPH_MODE, 55 enable_graph_kernel=True, device_target="GPU") 56 net = NetSigmoid() 57 result_open_gk = net(x) 58 59 context.set_context(mode=context.GRAPH_MODE, 60 enable_graph_kernel=False, device_target="GPU") 61 net_beta = NetSigmoid() 62 result_close_gk = net_beta(x) 63 diff = result_open_gk.asnumpy() - result_close_gk.asnumpy() 64 assert np.all(abs(diff) < error) 65 66 67@pytest.mark.level0 68@pytest.mark.platform_x86_gpu_training 69@pytest.mark.env_onecard 70def test_sigmoid_grad(): 71 y = Tensor(np.array([[[[-1, 1, 2], 72 [1, -1, 1], 73 [2, 1, -1]]]]).astype(np.float32)) 74 dy = Tensor(np.array([[[[-11, 2, 4], 75 [-1, 1, -1], 76 [-4, 4, -4]]]]).astype(np.float32)) 77 78 error = np.ones(shape=[1, 1, 3, 3]) * 1.0e-6 79 80 context.set_context(mode=context.GRAPH_MODE, 81 enable_graph_kernel=True, device_target="GPU") 82 net = NetSigmoidGrad() 83 result_open_gk = net(y, dy) 84 85 context.set_context(mode=context.GRAPH_MODE, 86 enable_graph_kernel=False, device_target="GPU") 87 net_beta = NetSigmoidGrad() 88 result_close_gk = net_beta(y, dy) 89 diff = result_open_gk.asnumpy() - result_close_gk.asnumpy() 90 assert np.all(abs(diff) < error) 91