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# ============================================================================ 15 16import pytest 17import numpy as np 18 19from mindspore import Tensor 20import mindspore.context as context 21from mindspore.ops import functional as F 22from mindspore.common import dtype as mstype 23 24 25@pytest.mark.level0 26@pytest.mark.platform_x86_cpu 27@pytest.mark.platform_arm_ascend_training 28@pytest.mark.platform_x86_ascend_training 29@pytest.mark.env_onecard 30@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 31def test_igamma_functional_api_modes(mode): 32 """ 33 Feature: Test igamma functional api. 34 Description: Test igamma functional api for Graph and PyNative modes. 35 Expectation: The result match to the expect value. 36 """ 37 context.set_context(mode=mode) 38 a = Tensor([2.0, 4.0, 6.0, 8.0], mstype.float32) 39 x = Tensor([2.0, 3.0, 4.0, 5.0], mstype.float32) 40 output = F.igamma(a, x) 41 expected = np.array([0.593994, 0.35276785, 0.21486944, 0.13337152]) 42 np.testing.assert_array_almost_equal(output.asnumpy(), expected, decimal=4) 43 44 45@pytest.mark.level0 46@pytest.mark.platform_x86_cpu 47@pytest.mark.platform_arm_ascend_training 48@pytest.mark.platform_x86_ascend_training 49@pytest.mark.env_onecard 50@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 51def test_igamma_functional_api_compile(mode): 52 """ 53 Feature: Test igamma functional api. 54 Description: Test igamma functional api for Graph and PyNative modes with scalar input. 55 Expectation: Network compile succeed. 56 """ 57 context.set_context(mode=mode) 58 a = Tensor(np.random.uniform(0, 15, (3,)), mstype.float64) 59 x = Tensor(np.random.uniform(0, 15, (1,)), mstype.float64) 60 F.igamma(a, x).asnumpy() 61 62 63@pytest.mark.level1 64@pytest.mark.platform_x86_cpu 65@pytest.mark.env_onecard 66@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 67def test_igamma_tensor_api_modes(mode): 68 """ 69 Feature: Test igamma tensor api. 70 Description: Test igamma tensor api for Graph and PyNative modes. 71 Expectation: The result match to the expect value. 72 """ 73 context.set_context(mode=mode, device_target="CPU") 74 a = Tensor([2.0, 4.0, 6.0, 8.0], mstype.float32) 75 x = Tensor([2.0, 3.0, 4.0, 5.0], mstype.float32) 76 output = a.igamma(x) 77 expected = np.array([0.593994, 0.35276785, 0.21486944, 0.13337152]) 78 np.testing.assert_array_almost_equal(output.asnumpy(), expected, decimal=4) 79 80 81@pytest.mark.level0 82@pytest.mark.platform_x86_cpu 83@pytest.mark.platform_arm_ascend_training 84@pytest.mark.platform_x86_ascend_training 85@pytest.mark.env_onecard 86@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 87def test_igammac_functional_api_modes(mode): 88 """ 89 Feature: Test igamma functional api. 90 Description: Test igamma functional api for Graph and PyNative modes. 91 Expectation: The result match to the expect value. 92 """ 93 context.set_context(mode=mode) 94 a = Tensor([2.0, 4.0, 6.0, 8.0], mstype.float32) 95 x = Tensor([2.0, 3.0, 4.0, 5.0], mstype.float32) 96 output = F.igammac(a, x) 97 expected = np.array([0.40600586, 0.6472318, 0.7851304, 0.8666283]) 98 np.testing.assert_array_almost_equal(output.asnumpy(), expected, decimal=4) 99 100 101@pytest.mark.level1 102@pytest.mark.platform_x86_cpu 103@pytest.mark.env_onecard 104@pytest.mark.parametrize('mode', [context.GRAPH_MODE, context.PYNATIVE_MODE]) 105def test_igammac_tensor_api_modes(mode): 106 """ 107 Feature: Test igamma tensor api. 108 Description: Test igamma tensor api for Graph and PyNative modes. 109 Expectation: The result match to the expect value. 110 """ 111 context.set_context(mode=mode, device_target="CPU") 112 a = Tensor([2.0, 4.0, 6.0, 8.0], mstype.float32) 113 x = Tensor([2.0, 3.0, 4.0, 5.0], mstype.float32) 114 output = a.igammac(x) 115 expected = np.array([0.40600586, 0.6472318, 0.7851304, 0.8666283]) 116 np.testing.assert_array_almost_equal(output.asnumpy(), expected, decimal=4) 117