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 23 24context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 25 26 27class GeluNet(nn.Cell): 28 def __init__(self): 29 super(GeluNet, self).__init__() 30 self.gelu = P.GeLU() 31 32 def construct(self, x): 33 return self.gelu(x) 34 35 36def GeluCompute(x): 37 return 0.5 * x * (1.0 + np.tanh(np.sqrt(2 / np.pi) * (x + 0.044715 * x * x * x))) 38 39 40@pytest.mark.level0 41@pytest.mark.platform_x86_cpu 42@pytest.mark.env_onecard 43def test_gelu_1d(): 44 x_np = np.random.random((50,)).astype(np.float32) 45 y_np = GeluCompute(x_np) 46 47 x_ms = Tensor(x_np) 48 net = GeluNet() 49 y_ms = net(x_ms) 50 51 assert np.allclose(y_np, y_ms.asnumpy()) 52 53 54@pytest.mark.level0 55@pytest.mark.platform_x86_cpu 56@pytest.mark.env_onecard 57def test_gelu_2d(): 58 x_np = np.random.random((50, 40)).astype(np.float32) 59 y_np = GeluCompute(x_np) 60 61 x_ms = Tensor(x_np) 62 net = GeluNet() 63 y_ms = net(x_ms) 64 65 assert np.allclose(y_np, y_ms.asnumpy()) 66 67 68@pytest.mark.level0 69@pytest.mark.platform_x86_cpu 70@pytest.mark.env_onecard 71def test_gelu_4d(): 72 x_np = np.random.random((32, 3, 224, 224)).astype(np.float32) 73 y_np = GeluCompute(x_np) 74 75 x_ms = Tensor(x_np) 76 net = GeluNet() 77 y_ms = net(x_ms) 78 79 assert np.allclose(y_np, y_ms.asnumpy()) 80 81 82@pytest.mark.level0 83@pytest.mark.platform_x86_cpu 84@pytest.mark.env_onecard 85def test_gelu_neg(): 86 x_np = np.random.random((32, 3, 224, 224)).astype(np.float32) * -1 87 y_np = GeluCompute(x_np) 88 89 x_ms = Tensor(x_np) 90 net = GeluNet() 91 y_ms = net(x_ms) 92 93 assert np.allclose(y_np, y_ms.asnumpy()) 94