• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022 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.operations.math_ops import Polygamma
21
22context.set_context(mode=context.GRAPH_MODE)
23
24
25class PolygammaNet(nn.Cell):
26
27    def __init__(self):
28        super(PolygammaNet, self).__init__()
29        self.polygamma = Polygamma()
30
31    def construct(self, a, x):
32        return self.polygamma(a, x)
33
34
35@pytest.mark.level0
36@pytest.mark.platform_x86_gpu_training
37@pytest.mark.platform_arm_ascend_training
38@pytest.mark.platform_x86_ascend_training
39@pytest.mark.env_onecard
40def test_polygamma_1d_a_1_int64_float16():
41    """
42    Feature: Polygamma
43    Description: test cases for Polygamma of float16
44    Expectation: the result match to torch
45    """
46    net = PolygammaNet()
47    a = np.array(1).astype(np.int64)
48    x_ms = np.array([1, 0.4273, 9, -3.12, 12246.345]).astype(np.float16)
49    z_ms = net(Tensor(a), Tensor(x_ms))
50    expect = np.array([1.64493407e+00, 6.47734100e+00, 1.17512015e-01, 7.35594209e+01,
51                       8.16493161e-05]).astype(np.float64)
52    assert np.allclose(z_ms.asnumpy(), expect.astype(np.float16), 0.001, 0.001)
53
54
55@pytest.mark.level1
56@pytest.mark.platform_x86_gpu_training
57@pytest.mark.env_onecard
58def test_polygamma_1d_a_1_int64_float32():
59    """
60    Feature: Polygamma
61    Description: test cases for Polygamma of float16
62    Expectation: the result match to torch
63    """
64    net = PolygammaNet()
65    a = np.array(1).astype(np.int64)
66    x_ms = np.array([1, 0.5273, 9, -3.12, 13250]).astype(np.float32)
67    z_ms = net(Tensor(a), Tensor(x_ms))
68    expect = np.array([1.6449341e+00, 4.5092258e+00, 1.1751202e-01, 7.2555374e+01,
69                       7.5474542e-05]).astype(np.float32)
70    assert np.allclose(z_ms.asnumpy(), expect, 0.0001, 0.0001)
71
72
73@pytest.mark.level1
74@pytest.mark.platform_x86_gpu_training
75@pytest.mark.env_onecard
76def test_polygamma_1d_a_1_int64_float64():
77    """
78    Feature: Polygamma
79    Description: test cases for Polygamma of float64
80    Expectation: the result match to torch
81    """
82    net = PolygammaNet()
83    a = np.array(1).astype(np.int64)
84    x_ms = np.array([1, 0.5273, 9, -3.12, 13250]).astype(np.float64)
85    z_ms = net(Tensor(a), Tensor(x_ms))
86    expect = np.array([1.64493407e+00, 4.50922599e+00, 1.17512015e-01, 7.25554469e+01,
87                       7.54745462e-05]).astype(np.float64)
88    assert np.allclose(z_ms.asnumpy(), expect, 0.00001, 0.00001)
89
90
91@pytest.mark.level1
92@pytest.mark.platform_x86_gpu_training
93@pytest.mark.env_onecard
94def test_polygamma_1d_a_2_int64_float16():
95    """
96    Feature: Polygamma
97    Description: test cases for Polygamma of float16
98    Expectation: the result match to torch
99    """
100    net = PolygammaNet()
101    a = np.array(2).astype(np.int64)
102    x_ms = np.array([1, 0.4273, 9, -3.12, 12246.345]).astype(np.float16)
103    z_ms = net(Tensor(a), Tensor(x_ms))
104    expect = np.array([-2.40411381e+00, -2.65858621e+01, -1.37933192e-02,
105                       1.18094081e+03, -6.66661082e-09]).astype(np.float64)
106    assert np.allclose(z_ms.asnumpy(), expect.astype(np.float16), 0.001, 0.001)
107
108
109@pytest.mark.level1
110@pytest.mark.platform_x86_gpu_training
111@pytest.mark.env_onecard
112def test_polygamma_1d_a_2_int64_float32():
113    """
114    Feature: Polygamma
115    Description: test cases for Polygamma of float16
116    Expectation: the result match to torch
117    """
118    net = PolygammaNet()
119    a = np.array(2).astype(np.int64)
120    x_ms = np.array([1, 0.5273, 9, -3.12, 13250]).astype(np.float64)
121    z_ms = net(Tensor(a), Tensor(x_ms))
122    expect = np.array([-2.40411381e+00, -1.44329154e+01, -1.37933192e-02,
123                       1.15570148e+03, -5.69640712e-09]).astype(np.float64)
124    assert np.allclose(z_ms.asnumpy(), expect, 0.0001, 0.0001)
125
126
127@pytest.mark.level1
128@pytest.mark.platform_x86_gpu_training
129@pytest.mark.env_onecard
130def test_polygamma_1d_a_2_int64_float64():
131    """
132    Feature: Polygamma
133    Description: test cases for Polygamma of float64
134    Expectation: the result match to torch
135    """
136    net = PolygammaNet()
137    a = np.array(2).astype(np.int64)
138    x_ms = np.array([1, 0.5273, 9, -3.12, 13250]).astype(np.float64)
139    z_ms = net(Tensor(a), Tensor(x_ms))
140    expect = np.array([-2.40411381e+00, -1.44329154e+01, -1.37933192e-02,
141                       1.15570148e+03, -5.69640712e-09]).astype(np.float64)
142    assert np.allclose(z_ms.asnumpy(), expect, 0.00001, 0.00001)
143