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 20from mindspore import Tensor 21import mindspore.ops.operations._grad_ops as P 22context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 23 24@pytest.mark.level0 25@pytest.mark.platform_x86_gpu_training 26@pytest.mark.env_onecard 27def test_acosgrad_fp32(): 28 error = np.ones(4) * 1.0e-7 29 x_np = np.array([0, -0.25, 0.5, 0.3]).astype(np.float32) 30 dout_np = np.array([1, 1, 1, 1]).astype(np.float32) 31 output_ms = P.ACosGrad()(Tensor(x_np), Tensor(dout_np)) 32 expect = np.array([-1, -1.0327955, -1.1547005, -1.0482849]) 33 diff = output_ms.asnumpy() - expect 34 assert np.all(diff < error) 35 36@pytest.mark.level0 37@pytest.mark.platform_x86_gpu_training 38@pytest.mark.env_onecard 39def test_acosgrad_fp16(): 40 error = np.ones(4) * 1.0e-3 41 x_np = np.array([0, -0.25, 0.5, 0.3]).astype(np.float16) 42 dout_np = np.array([1, 1, 1, 1]).astype(np.float16) 43 output_ms = P.ACosGrad()(Tensor(x_np), Tensor(dout_np)) 44 expect = np.array([-1, -1.033, -1.154, -1.048]) 45 diff = output_ms.asnumpy() - expect 46 assert np.all(diff < error) 47