• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# ============================================================================
15import numpy as np
16import pytest
17import mindspore.context as context
18import mindspore.nn as nn
19from mindspore import Tensor
20from mindspore.ops import operations as P
21
22
23class ErfNet(nn.Cell):
24    def __init__(self):
25        super(ErfNet, self).__init__()
26        self.erf = P.Erf()
27
28    def construct(self, x):
29        return self.erf(x)
30
31class ErfcNet(nn.Cell):
32    def __init__(self):
33        super(ErfcNet, self).__init__()
34        self.erfc = P.Erfc()
35
36    def construct(self, x):
37        return self.erfc(x)
38
39def get_output(net, inp, enable_graph_kernel=False):
40    context.set_context(enable_graph_kernel=enable_graph_kernel)
41    output = net()(inp)
42    return output
43
44def basic_test(net, datatype):
45    inp = Tensor(np.random.random((2, 3)).astype(datatype))
46    expect = get_output(net, inp, False)
47    output = get_output(net, inp, True)
48    expect_np = expect.asnumpy().copy()
49    output_np = output.asnumpy().copy()
50    assert np.allclose(expect_np, output_np, 1.e-4, 1.e-7)
51
52    inp = Tensor(np.random.random((2, 3, 3, 4, 5)).astype(datatype))
53    expect = get_output(net, inp, False)
54    output = get_output(net, inp, True)
55    expect_np = expect.asnumpy().copy()
56    output_np = output.asnumpy().copy()
57    assert np.allclose(expect_np, output_np, 1.e-4, 1.e-7)
58
59@pytest.mark.level0
60@pytest.mark.platform_x86_gpu_training
61@pytest.mark.env_onecard
62def test_gpu_fp16():
63    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
64    basic_test(ErfNet, np.float16)
65    basic_test(ErfcNet, np.float16)
66
67@pytest.mark.level0
68@pytest.mark.platform_x86_gpu_training
69@pytest.mark.env_onecard
70def test_gpu_fp32():
71    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
72    basic_test(ErfNet, np.float32)
73    basic_test(ErfcNet, np.float32)
74