• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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="GPU")
26
27class NetExpm1(nn.Cell):
28    def __init__(self):
29        super(NetExpm1, self).__init__()
30        self.expm1 = P.Expm1()
31
32    def construct(self, x):
33        return self.expm1(x)
34
35
36@pytest.mark.level0
37@pytest.mark.platform_x86_gpu_training
38@pytest.mark.env_onecard
39def test_expm1_fp32():
40    expm1 = NetExpm1()
41    x = np.random.rand(3, 8).astype(np.float32)
42    output = expm1(Tensor(x, dtype=dtype.float32))
43    expect = np.expm1(x)
44    tol = 1e-6
45    assert (np.abs(output.asnumpy() - expect) < tol).all()
46
47@pytest.mark.level0
48@pytest.mark.platform_x86_gpu_training
49@pytest.mark.env_onecard
50def test_expm1_fp16():
51    expm1 = NetExpm1()
52    x = np.random.rand(3, 8).astype(np.float16)
53    output = expm1(Tensor(x, dtype=dtype.float16))
54    expect = np.expm1(x)
55    tol = 1e-3
56    assert (np.abs(output.asnumpy() - expect) < tol).all()
57