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# ============================================================================ 15 16import numpy as np 17import pytest 18 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.ops.operations import _grad_ops as G 23 24 25class NetReciprocalGrad(nn.Cell): 26 def __init__(self): 27 super(NetReciprocalGrad, self).__init__() 28 self.grad = G.ReciprocalGrad() 29 30 def construct(self, y, dy): 31 return self.grad(y, dy) 32 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_gpu_training 36@pytest.mark.env_onecard 37def test_reciprocal_grad_float32(): 38 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 39 y = Tensor(np.array([[[[-1, 1, 12], 40 [5, 34, 6], 41 [10, 2, -1]]]]).astype(np.float32)) 42 dy = Tensor(np.array([[[[29, 1, 55], 43 [2.2, 63, 2], 44 [3, 3, 12]]]]).astype(np.float32)) 45 expect = np.array([[[[-29, -1, -7920], 46 [-55, -72828, -72], 47 [-300, -12, -12]]]]).astype(np.float32) 48 net = NetReciprocalGrad() 49 output = net(y, dy) 50 np.testing.assert_array_almost_equal(output.asnumpy(), expect) 51 52 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 53 y = Tensor(np.array([[[[-1, 1, 12], 54 [5, 34, 6], 55 [10, 2, -1]]]]).astype(np.float32)) 56 dy = Tensor(np.array([[[[29, 1, 55], 57 [2.2, 63, 2], 58 [3, 3, 12]]]]).astype(np.float32)) 59 expect = np.array([[[[-29, -1, -7920], 60 [-55, -72828, -72], 61 [-300, -12, -12]]]]).astype(np.float32) 62 net = NetReciprocalGrad() 63 output = net(y, dy) 64 np.testing.assert_array_almost_equal(output.asnumpy(), expect) 65 66 67@pytest.mark.level0 68@pytest.mark.platform_x86_gpu_training 69@pytest.mark.env_onecard 70def test_reciprocal_grad_float16(): 71 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 72 y = Tensor(np.array([[0.01, 0.2, 0.22], 73 [10.002, 2, -1]]).astype(np.float16)) 74 dy = Tensor(np.array([[34, 1, 55], 75 [3, 3, 63]]).astype(np.float16)) 76 expect = np.array([[-0.0034, -0.03998, -2.662], 77 [-300, -12, -63]]).astype(np.float16) 78 net = NetReciprocalGrad() 79 output = net(y, dy) 80 np.testing.assert_array_almost_equal(output.asnumpy(), expect) 81 82 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 83 y = Tensor(np.array([[0.01, 0.2, 0.22], 84 [10.002, 2, -1]]).astype(np.float16)) 85 dy = Tensor(np.array([[34, 1, 55], 86 [3, 3, 63]]).astype(np.float16)) 87 expect = np.array([[-0.0034, -0.03998, -2.662], 88 [-300, -12, -63]]).astype(np.float16) 89 net = NetReciprocalGrad() 90 output = net(y, dy) 91 np.testing.assert_array_almost_equal(output.asnumpy(), expect) 92