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 20import mindspore.nn as nn 21import mindspore as ms 22import mindspore.ops.operations as P 23import mindspore.ops.operations._grad_ops as G 24from mindspore.ops.composite import GradOperation 25from mindspore import Tensor 26 27class GatherDNet(nn.Cell): 28 def __init__(self, dim=0): 29 super(GatherDNet, self).__init__() 30 self.gather_d = P.GatherD() 31 self.dim = dim 32 33 def construct(self, x, index): 34 return self.gather_d(x, self.dim, index) 35 36@pytest.mark.level0 37@pytest.mark.platform_x86_gpu_training 38@pytest.mark.env_onecard 39def test_gather_grad_graph_int32_fp32(): 40 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 41 x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float32) 42 dim = 0 43 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32) 44 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 45 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32) 46 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 47 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32) 48 net = GatherDNet(dim) 49 grad_net = GradOperation(get_all=True, sens_param=True)(net) 50 output = grad_net(x, index, grad) 51 error = 1e-4 52 diff = output[0].asnumpy() - expect 53 assert np.all(diff < error) 54 55@pytest.mark.level0 56@pytest.mark.platform_x86_gpu_training 57@pytest.mark.env_onecard 58def test_gather_grad_graph_int64_fp32(): 59 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 60 x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float32) 61 dim = 0 62 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64) 63 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 64 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32) 65 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 66 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32) 67 net = GatherDNet(dim) 68 grad_net = GradOperation(get_all=True, sens_param=True)(net) 69 output = grad_net(x, index, grad) 70 error = 1e-4 71 diff = output[0].asnumpy() - expect 72 assert np.all(diff < error) 73 74@pytest.mark.level0 75@pytest.mark.platform_x86_gpu_training 76@pytest.mark.env_onecard 77def test_gather_grad_graph_int32_fp16(): 78 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 79 x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float16) 80 dim = 0 81 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32) 82 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 83 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16) 84 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 85 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16) 86 net = GatherDNet(dim) 87 grad_net = GradOperation(get_all=True, sens_param=True)(net) 88 output = grad_net(x, index, grad) 89 error = 1e-4 90 diff = output[0].asnumpy() - expect 91 assert np.all(diff < error) 92 93@pytest.mark.level0 94@pytest.mark.platform_x86_gpu_training 95@pytest.mark.env_onecard 96def test_gather_grad_graph_int64_fp16(): 97 context.set_context(mode=context.GRAPH_MODE, device_target="GPU") 98 x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float16) 99 dim = 0 100 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64) 101 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 102 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16) 103 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 104 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16) 105 net = GatherDNet(dim) 106 grad_net = GradOperation(get_all=True, sens_param=True)(net) 107 output = grad_net(x, index, grad) 108 error = 1e-4 109 diff = output[0].asnumpy() - expect 110 assert np.all(diff < error) 111 112@pytest.mark.level0 113@pytest.mark.platform_x86_gpu_training 114@pytest.mark.env_onecard 115def test_gather_grad_pynative_int32_fp32(): 116 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 117 x_shape = (2, 5) 118 dim = 0 119 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32) 120 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 121 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32) 122 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 123 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32) 124 output = G.GatherDGrad(dim, x_shape)(index, grad) 125 error = 1e-4 126 diff = output.asnumpy() - expect 127 assert np.all(diff < error) 128 129@pytest.mark.level0 130@pytest.mark.platform_x86_gpu_training 131@pytest.mark.env_onecard 132def test_gather_grad_pynative_int64_fp32(): 133 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 134 x_shape = (2, 5) 135 dim = 0 136 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64) 137 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 138 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32) 139 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 140 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32) 141 output = G.GatherDGrad(dim, x_shape)(index, grad) 142 error = 1e-4 143 diff = output.asnumpy() - expect 144 assert np.all(diff < error) 145 146@pytest.mark.level0 147@pytest.mark.platform_x86_gpu_training 148@pytest.mark.env_onecard 149def test_gather_grad_pynative_int32_fp16(): 150 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 151 x_shape = (2, 5) 152 dim = 0 153 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32) 154 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 155 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16) 156 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 157 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16) 158 output = G.GatherDGrad(dim, x_shape)(index, grad) 159 error = 1e-4 160 diff = output.asnumpy() - expect 161 assert np.all(diff < error) 162 163@pytest.mark.level0 164@pytest.mark.platform_x86_gpu_training 165@pytest.mark.env_onecard 166def test_gather_grad_pynative_int64_fp16(): 167 context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") 168 x_shape = (2, 5) 169 dim = 0 170 index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64) 171 grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710], 172 [0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16) 173 expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710], 174 [0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16) 175 output = G.GatherDGrad(dim, x_shape)(index, grad) 176 error = 1e-4 177 diff = output.asnumpy() - expect 178 assert np.all(diff < error) 179