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 operations as P 23from mindspore import dtype 24 25context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 26 27 28class NetExpm1(nn.Cell): 29 def __init__(self): 30 super(NetExpm1, self).__init__() 31 self.expm1 = P.Expm1() 32 33 def construct(self, x): 34 return self.expm1(x) 35 36 37@pytest.mark.level0 38@pytest.mark.platform_x86_cpu 39@pytest.mark.env_onecard 40def test_expm1_op(): 41 x = np.random.rand(3, 8).astype(np.float32) 42 y = np.random.rand(3, 8).astype(np.float16) 43 44 expm1 = NetExpm1() 45 output_x = expm1(Tensor(x, dtype=dtype.float32)) 46 expect_x = np.expm1(x) 47 tol_x = 1e-6 48 assert (np.abs(output_x.asnumpy() - expect_x) < tol_x).all() 49 50 output_y = expm1(Tensor(y, dtype=dtype.float16)) 51 expect_y = np.expm1(y) 52 tol_y = 1e-3 53 assert (np.abs(output_y.asnumpy() - expect_y) < tol_y).all() 54