• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2024 Huawei Technoeluies 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 pytest
16import numpy as np
17import mindspore as ms
18from mindspore import mint, context, Tensor
19from tests.st.utils import test_utils
20from tests.st.ops.dynamic_shape.test_op_utils import TEST_OP
21
22
23def generate_random_input(shape, dtype):
24    return np.random.randn(*shape).astype(dtype)
25
26def generate_expect_forward_output(x, alpha, dtype):
27    return np.where(x > 0, x * 1, alpha * 1 * (np.exp(x * 1) - 1)).astype(dtype)
28
29def generate_expect_backward_output(x, alpha, dtype):
30    return  np.where(x > 0, 1, alpha * 1 * 1 * np.exp(x * 1)).astype(dtype)
31
32
33@test_utils.run_with_cell
34def elu_forward_func(x, alpha):
35    return mint.nn.functional.elu(x, alpha)
36
37@test_utils.run_with_cell
38def elu_backward_func(x, alpha):
39    return ms.ops.grad(elu_forward_func, (0))(x, alpha)
40
41@test_utils.run_with_cell
42def elu_vmap_func(x, alpha):
43    return ms.ops.vmap(elu_forward_func, in_axes=(0, None), out_axes=0)(x, alpha)
44
45
46
47@pytest.mark.level0
48@pytest.mark.env_onecard
49@pytest.mark.platform_arm_ascend_training
50@pytest.mark.platform_x86_ascend_training
51@pytest.mark.parametrize("mode", [ms.PYNATIVE_MODE, ms.GRAPH_MODE])
52def test_elu_forward(mode):
53    """
54    Feature: test elu operator
55    Description: test elu run by pyboost
56    Expectation: success
57    """
58    context.set_context(mode=mode)
59
60    alpha = np.random.uniform(0.5, 2)
61    x_np = generate_random_input((2, 3, 4), np.float32)
62    x_tensor = Tensor(x_np, ms.float32)
63    output = elu_forward_func(x_tensor, alpha)
64    expect = generate_expect_forward_output(x_np, alpha, np.float32)
65    np.testing.assert_allclose(output.asnumpy(), expect, 1e-4, 1e-4)
66
67
68
69
70@pytest.mark.level0
71@pytest.mark.env_onecard
72@pytest.mark.platform_arm_ascend910b_training
73@pytest.mark.parametrize("mode", [ms.PYNATIVE_MODE, ms.GRAPH_MODE])
74def test_elu_backward_alpha_neg(mode):
75    """
76    Feature: test elu operator
77    Description: test elu run by pyboost
78    Expectation: success
79    """
80    context.set_context(mode=mode)
81
82    alpha = np.random.uniform(-0.5, -2)
83    x_np = generate_random_input((2, 3, 4), np.float32)
84    x_tensor = Tensor(x_np, ms.float32)
85    output = elu_backward_func(x_tensor, alpha)
86    expect = generate_expect_backward_output(x_np, alpha, np.float32)
87    np.testing.assert_allclose(output.asnumpy(), expect, rtol=1e-4, atol=1e-4)
88
89
90
91@pytest.mark.level0
92@pytest.mark.env_onecard
93@pytest.mark.platform_arm_ascend_training
94@pytest.mark.platform_x86_ascend_training
95@pytest.mark.parametrize("mode", [ms.PYNATIVE_MODE, ms.GRAPH_MODE])
96def test_elu_backward(mode):
97    """
98    Feature: test elu operator
99    Description: test elu run by pyboost
100    Expectation: success
101    """
102    context.set_context(mode=mode)
103
104    alpha = np.random.uniform(0.5, 2)
105    x_np = generate_random_input((2, 3, 4), np.float32)
106    x_tensor = Tensor(x_np, ms.float32)
107    output = elu_backward_func(x_tensor, alpha)
108    expect = generate_expect_backward_output(x_np, alpha, np.float32)
109    np.testing.assert_allclose(output.asnumpy(), expect, rtol=1e-4, atol=1e-4)
110
111
112
113@pytest.mark.level1
114@pytest.mark.env_onecard
115@pytest.mark.platform_arm_ascend_training
116@pytest.mark.platform_x86_ascend_training
117@pytest.mark.parametrize("mode", [ms.PYNATIVE_MODE, ms.GRAPH_MODE])
118def test_elu_vmap(mode):
119    """
120    Feature: pyboost function.
121    Description: test function elu vmap feature.
122    Expectation: expect correct result.
123    """
124    context.set_context(mode=mode)
125
126    alpha = np.random.uniform(0.5, 2)
127    x_np = generate_random_input((2, 3, 4, 5), np.float32)
128    x_tensor = Tensor(x_np, ms.float32)
129    output = elu_vmap_func(x_tensor, alpha)
130    expect = generate_expect_forward_output(x_np, alpha, np.float32)
131    np.testing.assert_allclose(output.asnumpy(), expect, rtol=1e-4, atol=1e-4)
132
133
134
135@pytest.mark.level1
136@pytest.mark.env_onecard
137@pytest.mark.platform_arm_ascend_training
138@pytest.mark.platform_x86_ascend_training
139def test_elu_dynamic_shape_testop():
140    """
141    Feature: Test elu with dynamic shape in graph mode using TEST_OP.
142    Description: call ops.elu with valid input and index.
143    Expectation: return the correct value.
144    """
145
146    alpha1 = np.random.uniform(0.5, 2)
147    alpha2 = np.random.uniform(0.5, 2)
148    x1 = generate_random_input((3, 4, 5), np.float32)
149    x2 = generate_random_input((3, 7, 8, 3), np.float32)
150    TEST_OP(mint.nn.functional.elu, [[ms.Tensor(x1), alpha1], [ms.Tensor(x2), alpha2]], 'elu')
151