|
|
|
@ -19,32 +19,37 @@ import pytest
|
|
|
|
|
import mindspore.context as context
|
|
|
|
|
import mindspore.nn as nn
|
|
|
|
|
import mindspore as ms
|
|
|
|
|
import mindspore.ops.operations._grad_ops as P
|
|
|
|
|
import mindspore.ops.operations as P
|
|
|
|
|
import mindspore.ops.operations._grad_ops as G
|
|
|
|
|
from mindspore.ops.composite import GradOperation
|
|
|
|
|
from mindspore import Tensor
|
|
|
|
|
|
|
|
|
|
class GatherDGradNet(nn.Cell):
|
|
|
|
|
class GatherDNet(nn.Cell):
|
|
|
|
|
def __init__(self, dim=0):
|
|
|
|
|
super(GatherDGradNet, self).__init__()
|
|
|
|
|
self.gather_d_grad = P.GatherDGrad(dim)
|
|
|
|
|
super(GatherDNet, self).__init__()
|
|
|
|
|
self.gather_d = P.GatherD()
|
|
|
|
|
self.dim = dim
|
|
|
|
|
|
|
|
|
|
def construct(self, index, grad):
|
|
|
|
|
return self.gather_d_grad(index, grad)
|
|
|
|
|
def construct(self, x, index):
|
|
|
|
|
return self.gather_d(x, self.dim, index)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
|
@pytest.mark.platform_x86_gpu_training
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_graph_int32_fp32():
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
|
|
|
|
|
x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float32)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32)
|
|
|
|
|
net = GatherDGradNet(dim)
|
|
|
|
|
output = net(index, grad)
|
|
|
|
|
net = GatherDNet(dim)
|
|
|
|
|
grad_net = GradOperation(get_all=True, sens_param=True)(net)
|
|
|
|
|
output = grad_net(x, index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
diff = output[0].asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
@ -52,16 +57,18 @@ def test_gather_grad_graph_int32_fp32():
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_graph_int64_fp32():
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
|
|
|
|
|
x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float32)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32)
|
|
|
|
|
net = GatherDGradNet(dim)
|
|
|
|
|
output = net(index, grad)
|
|
|
|
|
net = GatherDNet(dim)
|
|
|
|
|
grad_net = GradOperation(get_all=True, sens_param=True)(net)
|
|
|
|
|
output = grad_net(x, index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
diff = output[0].asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
@ -69,16 +76,18 @@ def test_gather_grad_graph_int64_fp32():
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_graph_int32_fp16():
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
|
|
|
|
|
x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float16)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16)
|
|
|
|
|
net = GatherDGradNet(dim)
|
|
|
|
|
output = net(index, grad)
|
|
|
|
|
net = GatherDNet(dim)
|
|
|
|
|
grad_net = GradOperation(get_all=True, sens_param=True)(net)
|
|
|
|
|
output = grad_net(x, index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
diff = output[0].asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
@ -86,16 +95,18 @@ def test_gather_grad_graph_int32_fp16():
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_graph_int64_fp16():
|
|
|
|
|
context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
|
|
|
|
|
x = Tensor(np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]), ms.float16)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16)
|
|
|
|
|
net = GatherDGradNet(dim)
|
|
|
|
|
output = net(index, grad)
|
|
|
|
|
net = GatherDNet(dim)
|
|
|
|
|
grad_net = GradOperation(get_all=True, sens_param=True)(net)
|
|
|
|
|
output = grad_net(x, index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
diff = output[0].asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.level0
|
|
|
|
@ -103,13 +114,14 @@ def test_gather_grad_graph_int64_fp16():
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_pynative_int32_fp32():
|
|
|
|
|
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
|
|
|
|
|
x_shape = (2, 5)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32)
|
|
|
|
|
output = P.GatherDGrad(dim)(index, grad)
|
|
|
|
|
output = G.GatherDGrad(dim, x_shape)(index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
@ -119,13 +131,14 @@ def test_gather_grad_pynative_int32_fp32():
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_pynative_int64_fp32():
|
|
|
|
|
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
|
|
|
|
|
x_shape = (2, 5)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float32)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float32)
|
|
|
|
|
output = P.GatherDGrad(dim)(index, grad)
|
|
|
|
|
output = G.GatherDGrad(dim, x_shape)(index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
@ -135,13 +148,14 @@ def test_gather_grad_pynative_int64_fp32():
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_pynative_int32_fp16():
|
|
|
|
|
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
|
|
|
|
|
x_shape = (2, 5)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int32)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16)
|
|
|
|
|
output = P.GatherDGrad(dim)(index, grad)
|
|
|
|
|
output = G.GatherDGrad(dim, x_shape)(index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
@ -151,13 +165,14 @@ def test_gather_grad_pynative_int32_fp16():
|
|
|
|
|
@pytest.mark.env_onecard
|
|
|
|
|
def test_gather_grad_pynative_int64_fp16():
|
|
|
|
|
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
|
|
|
|
|
x_shape = (2, 5)
|
|
|
|
|
dim = 0
|
|
|
|
|
index = Tensor(np.array([[0, 1, 1, 0, 0], [1, 0, 0, 1, 1]]), ms.int64)
|
|
|
|
|
grad = Tensor(np.array([[0.9031, 0.0890, 0.2779, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.8439, 0.2003, 0.6868, 0.4437]]), ms.float16)
|
|
|
|
|
expect = np.array([[0.9031, 0.8439, 0.2003, 0.3198, 0.5710],
|
|
|
|
|
[0.6949, 0.0890, 0.2779, 0.6868, 0.4437]], np.float16)
|
|
|
|
|
output = P.GatherDGrad(dim)(index, grad)
|
|
|
|
|
output = G.GatherDGrad(dim, x_shape)(index, grad)
|
|
|
|
|
error = 1e-4
|
|
|
|
|
diff = output.asnumpy() - expect
|
|
|
|
|
assert np.all(diff < error)
|
|
|
|
|