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 composite as C 23from mindspore.ops import operations as P 24 25context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 26 27 28class GeluNet(nn.Cell): 29 def __init__(self): 30 super(GeluNet, self).__init__() 31 self.gelu = P.GeLU() 32 33 def construct(self, x): 34 return self.gelu(x) 35 36 37class Grad(nn.Cell): 38 def __init__(self, network): 39 super(Grad, self).__init__() 40 self.grad = C.GradOperation(get_all=True, sens_param=True) 41 self.network = network 42 43 def construct(self, input_data, sens): 44 gout = self.grad(self.network)(input_data, sens) 45 return gout 46 47 48@pytest.mark.level0 49@pytest.mark.platform_x86_gpu_training 50@pytest.mark.env_onecard 51def test_gelugrad(): 52 x_ms = Tensor(np.array([0.58401114, 0.68800163, 0.9760397, 0.14702141, 0.46563736, 0.9607501, 53 0.14567593, 0.12261796, 0.37054458, 0.46421242]).astype(np.float32)) 54 dy_ms = Tensor(np.array([0.5559598, 0.96994054, 0.24770357, 0.34646875, 0.2984393, 0.03287048, 55 0.55681044, 0.966908, 0.06015943, 0.6099489]).astype(np.float32)) 56 57 net = GeluNet() 58 grad = Grad(net) 59 60 output = grad(x_ms, dy_ms) 61 expect = [0.50963277, 0.9414753, 0.2667653, 0.21358444, 0.25243032, 0.0352667, 62 0.34266686, 0.57757664, 0.04707306, 0.51536125] 63 assert np.allclose(output[0].asnumpy(), expect) 64 65@pytest.mark.level0 66@pytest.mark.platform_x86_gpu_training 67@pytest.mark.env_onecard 68def test_gelugrad_fp16(): 69 np.random.seed(42) 70 x_np = np.random.randn(5, 3, 6).astype(np.float16) 71 dy_np = np.random.randn(5, 3, 6).astype(np.float16) 72 net = GeluNet() 73 grad = Grad(net) 74 output = grad(Tensor(x_np), Tensor(dy_np)) 75 expect = [[[8.4045e-02, 3.7817e-01, -6.6748e-01, -3.6914e-01, -1.2415e-01, -4.6362e-01], 76 [3.3301e-01, 2.6270e-01, 7.7534e-04, -2.0947e-01, -2.2021e-01, -6.4880e-02], 77 [-2.3633e-01, 7.6538e-02, 1.8280e-02, 3.8635e-02, -1.6235e-01, 1.2964e-01]], 78 79 [[-1.4801e-02, 9.6130e-03, -2.1660e+00, -8.5602e-03, 3.3356e-02, -3.1885e-01], 80 [-2.0355e-02, 1.7737e-01, 3.8719e-03, -9.1895e-01, 8.4717e-02, 2.0593e-01], 81 [5.8350e-02, -1.0020e+00, 6.8652e-01, 1.3428e-01, 6.0352e-01, -2.6270e-01]], 82 83 [[-6.5820e-01, 5.1147e-02, -1.2650e-02, -3.2983e-01, -1.5410e+00, 4.3518e-02], 84 [-4.3359e-01, 1.2659e-01, 1.1792e-01, 2.2705e-02, -1.2329e-01, -3.5278e-01], 85 [6.2109e-01, 1.3611e-01, 1.7041e-01, 2.7124e-01, -5.5908e-02, 1.7212e-01]], 86 87 [[2.8320e-01, 8.3252e-01, 4.2480e-02, -3.4473e-01, 3.9429e-01, 3.1958e-01], 88 [3.6499e-02, 1.2250e-01, 7.1350e-02, -2.7267e-02, 3.0029e-01, -8.0566e-01], 89 [8.2617e-01, 5.1367e-01, -9.2480e-01, 3.3203e-02, -7.5684e-01, 8.8623e-01]], 90 91 [[5.4590e-01, -9.2383e-01, -2.8107e-02, 4.2432e-01, 4.6826e-01, 5.0879e-01], 92 [-1.4062e-01, 6.6284e-02, -2.9126e-01, -6.3086e-01, -8.6975e-02, 4.1504e-02], 93 [-6.3171e-03, 1.0852e-01, 1.3779e-02, 1.0947e+00, -3.0334e-02, 2.3828e+00]]] 94 assert np.allclose(output[0].asnumpy(), expect, rtol=1e-2) 95