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 18from scipy import special 19 20import mindspore.context as context 21import mindspore.nn as nn 22from mindspore import Tensor 23from mindspore.ops import operations as P 24from mindspore import dtype 25 26context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 27 28class NetErf(nn.Cell): 29 def __init__(self): 30 super(NetErf, self).__init__() 31 self.erf = P.Erf() 32 33 def construct(self, x): 34 return self.erf(x) 35 36 37@pytest.mark.level0 38@pytest.mark.platform_x86_gpu_training 39@pytest.mark.env_onecard 40def test_erf_fp32(): 41 erf = NetErf() 42 x = np.random.rand(3, 8).astype(np.float32) 43 output = erf(Tensor(x, dtype=dtype.float32)) 44 expect = special.erf(x) 45 tol = 1e-6 46 assert (np.abs(output.asnumpy() - expect) < tol).all() 47 48@pytest.mark.level0 49@pytest.mark.platform_x86_gpu_training 50@pytest.mark.env_onecard 51def test_erf_fp16(): 52 erf = NetErf() 53 x = np.random.rand(3, 8).astype(np.float16) 54 output = erf(Tensor(x, dtype=dtype.float16)) 55 expect = special.erf(x) 56 tol = 1e-3 57 assert (np.abs(output.asnumpy() - expect) < tol).all() 58