未验证 提交 d12c3636 编写于 作者: T TTerror 提交者: GitHub

fix gather_nd, *test=kunlun (#39283)

上级 9ba3f429
...@@ -47,8 +47,12 @@ class GatherNdXPUKernel : public framework::OpKernel<T> { ...@@ -47,8 +47,12 @@ class GatherNdXPUKernel : public framework::OpKernel<T> {
auto x_shape = paddle::framework::vectorize<int>(x->dims()); auto x_shape = paddle::framework::vectorize<int>(x->dims());
auto index_shape = paddle::framework::vectorize<int>(index->dims()); auto index_shape = paddle::framework::vectorize<int>(index->dims());
if (index_shape.size() == 1) {
index_shape.insert(index_shape.begin(), 1);
}
xpu::VectorParam<int> x_vec = {x_shape.data(), xpu::VectorParam<int> x_vec = {x_shape.data(),
static_cast<int>(x_shape.size()), nullptr}; static_cast<int>(x_shape.size()), nullptr};
auto &dev_ctx = auto &dev_ctx =
ctx.template device_context<paddle::platform::XPUDeviceContext>(); ctx.template device_context<paddle::platform::XPUDeviceContext>();
int ret = XPU_SUCCESS; int ret = XPU_SUCCESS;
......
...@@ -18,251 +18,140 @@ import unittest ...@@ -18,251 +18,140 @@ import unittest
import numpy as np import numpy as np
import sys import sys
sys.path.append("..") sys.path.append("..")
from op_test import OpTest
from op_test_xpu import XPUOpTest
import paddle.fluid as fluid
import paddle
def gather_nd_grad(x, index):
dout_shape = index.shape[:-1] + x.shape[index.shape[-1]:]
numel = 1
for i in dout_shape:
numel = numel * i
dout = np.full(dout_shape, 1. / numel)
dx = np.full_like(x, 0)
index = tuple(index.reshape(-1, index.shape[-1]).T)
np.add.at(dx, index, dout)
return dx
def test_class1(op_type, typename):
class TestGatherNdOpWithEmptyIndex(XPUOpTest):
#Index has empty element, which means copy entire tensor
def setUp(self):
self.set_xpu()
self.place = paddle.XPUPlace(0)
self.op_type = "gather_nd"
xnp = np.random.random((5, 20)).astype(typename)
self.inputs = {
'X': xnp,
'Index': np.array([[], []]).astype("int32")
}
self.outputs = {
'Out': np.vstack((xnp[np.newaxis, :], xnp[np.newaxis, :]))
}
def set_xpu(self):
self.__class__.use_xpu = True
def test_check_output(self):
self.check_output_with_place(self.place)
def test_check_grad(self):
pass
cls_name = "{0}_{1}_1".format(op_type, typename)
TestGatherNdOpWithEmptyIndex.__name__ = cls_name
globals()[cls_name] = TestGatherNdOpWithEmptyIndex
def test_class2(op_type, typename):
class TestGatherNdOpWithIndex1(OpTest):
def setUp(self):
self.set_xpu()
self.place = paddle.XPUPlace(0)
self.op_type = "gather_nd"
xnp = np.random.random((5, 20)).astype(typename)
self.inputs = {'X': xnp, 'Index': np.array([1]).astype("int32")}
self.outputs = {'Out': self.inputs["X"][self.inputs["Index"]]}
def set_xpu(self): import paddle
self.__class__.use_xpu = True from op_test_xpu import XPUOpTest
from xpu.get_test_cover_info import create_test_class, get_xpu_op_support_types, XPUOpTestWrapper
def test_check_output(self):
self.check_output_with_place(self.place)
def test_check_grad(self):
pass
cls_name = "{0}_{1}_2".format(op_type, typename)
TestGatherNdOpWithIndex1.__name__ = cls_name
globals()[cls_name] = TestGatherNdOpWithIndex1
def test_class3(op_type, typename):
class TestGatherNdOpWithLowIndex(OpTest):
#Index has low rank, X has high rank
def setUp(self):
self.set_xpu()
self.place = paddle.XPUPlace(0)
self.op_type = "gather_nd"
xnp = np.random.uniform(0, 100, (10, 10)).astype(typename)
index = np.array([[1], [2]]).astype("int64")
self.inputs = {'X': xnp, 'Index': index}
self.outputs = {'Out': xnp[tuple(index.T)]}
self.x_grad = gather_nd_grad(xnp, index)
def set_xpu(self):
self.__class__.use_xpu = True
def test_check_output(self):
self.check_output_with_place(self.place)
def test_check_grad(self):
pass
cls_name = "{0}_{1}_3".format(op_type, typename) paddle.enable_static()
TestGatherNdOpWithLowIndex.__name__ = cls_name
globals()[cls_name] = TestGatherNdOpWithLowIndex
def test_class4(op_type, typename): class XPUTestGatherNd(XPUOpTestWrapper):
class TestGatherNdOpIndex1(OpTest): def __init__(self):
#Index has low rank, X has high rank self.op_name = 'gather_nd'
class XPUTestGatherNdBase(XPUOpTest):
def setUp(self): def setUp(self):
self.set_xpu()
self.place = paddle.XPUPlace(0)
self.op_type = "gather_nd" self.op_type = "gather_nd"
xnp = np.random.uniform(0, 100, (10, 10)).astype(typename) self.dtype = self.in_type
index = np.array([1, 2]).astype("int64") self.__class__.no_need_check_grad = True
self.inputs = {'X': xnp, 'Index': index}
self.outputs = {'Out': xnp[tuple(index.T)]}
def set_xpu(self):
self.__class__.use_xpu = True
def test_check_output(self):
self.check_output_with_place(self.place)
def test_check_grad(self):
pass
cls_name = "{0}_{1}_4".format(op_type, typename)
TestGatherNdOpIndex1.__name__ = cls_name
globals()[cls_name] = TestGatherNdOpIndex1
def test_class5(op_type, typename):
class TestGatherNdOpWithSameIndexAsX(OpTest):
#Index has same rank as X's rank
def setUp(self):
self.set_xpu()
self.place = paddle.XPUPlace(0) self.place = paddle.XPUPlace(0)
self.op_type = "gather_nd" self.init_data()
xnp = np.random.uniform(0, 100, (10, 10)).astype(typename)
index = np.array([[1, 1], [2, 1]]).astype("int64")
self.inputs = {'X': xnp, 'Index': index}
self.outputs = {'Out': xnp[tuple(index.T)]} #[25, 22]
def set_xpu(self): self.inputs = {'X': self.xnp, 'Index': self.inp}
self.__class__.use_xpu = True self.outputs = {'Out': self.output, }
def test_check_output(self): def test_check_output(self):
self.check_output_with_place(self.place) self.check_output_with_place(self.place)
def test_check_grad(self): def init_data(self):
pass self.xnp = np.random.random((5, 20)).astype(self.in_type)
self.inp = np.array([[], []]).astype("int32")
cls_name = "{0}_{1}_5".format(op_type, typename) self.output = np.vstack(
TestGatherNdOpWithSameIndexAsX.__name__ = cls_name (self.xnp[np.newaxis, :], self.xnp[np.newaxis, :]))
globals()[cls_name] = TestGatherNdOpWithSameIndexAsX
class XPUTestGatherNdOpWithEmptyIndex1(XPUTestGatherNdBase):
def init_data(self):
def test_class6(op_type, typename): self.xnp = np.random.random((5, 20)).astype(self.in_type)
class TestGatherNdOpWithHighRankSame(OpTest): self.inp = np.array([[], []]).astype("int32")
#Both Index and X have high rank, and Rank(Index) = Rank(X) self.output = np.vstack(
(self.xnp[np.newaxis, :], self.xnp[np.newaxis, :]))
def setUp(self):
self.set_xpu() class XPUTestGatherNdOpWithEmptyIndex2(XPUTestGatherNdBase):
self.place = paddle.XPUPlace(0) def init_data(self):
self.op_type = "gather_nd" self.xnp = np.random.random((5, 20)).astype(self.in_type)
self.inp = np.array([[], []]).astype("int64")
self.output = np.vstack(
(self.xnp[np.newaxis, :], self.xnp[np.newaxis, :]))
class XPUTestGatherNdOpWithIndex1(XPUTestGatherNdBase):
def init_data(self):
self.xnp = np.random.random((5, 20)).astype(self.in_type)
self.inp = np.array([1]).astype("int32")
self.output = self.xnp[self.inp]
class XPUTestGatherNdOpWithIndex2(XPUTestGatherNdBase):
def init_data(self):
self.xnp = np.random.random((5, 20)).astype(self.in_type)
self.inp = np.array([1]).astype("int64")
self.output = self.xnp[self.inp]
class XPUTestGatherNdOpWithLowIndex1(XPUTestGatherNdBase):
def init_data(self):
self.xnp = np.random.uniform(0, 100, (10, 10)).astype(self.in_type)
self.inp = np.array([[1], [2]]).astype("int32")
self.output = self.xnp[tuple(self.inp.T)]
class XPUTestGatherNdOpWithLowIndex2(XPUTestGatherNdBase):
def init_data(self):
self.xnp = np.random.uniform(0, 100, (10, 10)).astype(self.in_type)
self.inp = np.array([1, 2]).astype("int64")
self.output = self.xnp[tuple(self.inp.T)]
class XPUTestGatherNdOpWithHighRankSame1(XPUTestGatherNdBase):
def init_data(self):
shape = (5, 2, 3, 1, 10) shape = (5, 2, 3, 1, 10)
xnp = np.random.rand(*shape).astype(typename) self.xnp = np.random.rand(*shape).astype(self.in_type)
index = np.vstack([np.random.randint( self.inp = np.vstack(
0, s, size=2) for s in shape]).T [np.random.randint(
0, s, size=2) for s in shape]).T.astype("int32")
self.inputs = {'X': xnp, 'Index': index.astype("int32")} self.output = self.xnp[tuple(self.inp.T)]
self.outputs = {'Out': xnp[tuple(index.T)]}
def set_xpu(self):
self.__class__.use_xpu = True
def test_check_output(self):
self.check_output_with_place(self.place)
def test_check_grad(self):
pass
cls_name = "{0}_{1}_6".format(op_type, typename)
TestGatherNdOpWithHighRankSame.__name__ = cls_name
globals()[cls_name] = TestGatherNdOpWithHighRankSame
def test_class7(op_type, typename): class XPUTestGatherNdOpWithHighRankSame2(XPUTestGatherNdBase):
class TestGatherNdOpWithHighRankDiff(OpTest): def init_data(self):
#Both Index and X have high rank, Rank(Index) < Rank(X) shape = (5, 2, 3, 1, 10)
self.xnp = np.random.rand(*shape).astype(self.in_type)
self.inp = np.vstack(
[np.random.randint(
0, s, size=2) for s in shape]).T.astype("int64")
self.output = self.xnp[tuple(self.inp.T)]
def setUp(self): class XPUTestGatherNdOpWithHighRankDiff1(XPUTestGatherNdBase):
self.set_xpu() def init_data(self):
self.place = paddle.XPUPlace(0)
self.op_type = "gather_nd"
shape = (2, 3, 4, 1, 10) shape = (2, 3, 4, 1, 10)
xnp = np.random.rand(*shape).astype(typename) self.xnp = np.random.rand(*shape).astype(self.in_type)
index = np.vstack( self.inp = np.vstack(
[np.random.randint( [np.random.randint(
0, s, size=200) for s in shape]).T 0, s, size=200) for s in shape]).T.astype("int32")
index_re = index.reshape([20, 5, 2, 5]) self.output = self.xnp[tuple(self.inp.T)]
self.inputs = {'X': xnp, 'Index': index_re.astype("int32")}
self.outputs = {'Out': xnp[tuple(index.T)].reshape([20, 5, 2])}
def set_xpu(self): class XPUTestGatherNdOpWithHighRankDiff2(XPUTestGatherNdBase):
self.__class__.use_xpu = True def init_data(self):
shape = (2, 3, 4, 1, 10)
def test_check_output(self): self.xnp = np.random.rand(*shape).astype(self.in_type)
self.check_output_with_place(self.place) self.inp = np.vstack(
[np.random.randint(
def test_check_grad(self): 0, s, size=200) for s in shape]).T.astype("int64")
pass self.output = self.xnp[tuple(self.inp.T)]
cls_name = "{0}_{1}_7".format(op_type, typename) class XPUTestGatherNdOpWithSameIndexAsX1(XPUTestGatherNdBase):
TestGatherNdOpWithHighRankDiff.__name__ = cls_name def init_data(self):
globals()[cls_name] = TestGatherNdOpWithHighRankDiff self.xnp = np.random.uniform(0, 100, (10, 10)).astype(self.in_type)
self.inp = np.array([[1, 1], [2, 1]]).astype("int32")
self.output = self.xnp[tuple(self.inp.T)]
class TestGatherNdAPI(unittest.TestCase):
def test_imperative(self): class XPUTestGatherNdOpWithSameIndexAsX2(XPUTestGatherNdBase):
paddle.disable_static() def init_data(self):
input_1 = np.array([[1, 2], [3, 4], [5, 6]]) self.xnp = np.random.uniform(0, 100, (10, 10)).astype(self.in_type)
index_1 = np.array([[1]]) self.inp = np.array([[1, 1], [2, 1]]).astype("int64")
input = fluid.dygraph.to_variable(input_1) self.output = self.xnp[tuple(self.inp.T)]
index = fluid.dygraph.to_variable(index_1)
output = paddle.fluid.layers.gather(input, index) class XPUTestGatherNdOpIndex1(XPUTestGatherNdBase):
output_np = output.numpy() def init_data(self):
expected_output = np.array([3, 4]) self.xnp = np.random.uniform(0, 100, (10, 10)).astype(self.in_type)
self.assertTrue(np.allclose(output_np, expected_output)) self.inp = np.array([1, 2]).astype("int32")
paddle.enable_static() self.output = self.xnp[tuple(self.inp.T)]
class XPUTestGatherNdOpIndex2(XPUTestGatherNdBase):
for _typename in {'float32', 'int', 'int64'}: def init_data(self):
test_class1('gather_nd', _typename) self.xnp = np.random.uniform(0, 100, (10, 10)).astype(self.in_type)
test_class2('gather_nd', _typename) self.inp = np.array([1, 2]).astype("int64")
test_class3('gather_nd', _typename) self.output = self.xnp[tuple(self.inp.T)]
test_class4('gather_nd', _typename)
test_class5('gather_nd', _typename)
test_class6('gather_nd', _typename) support_types = get_xpu_op_support_types('gather_nd')
test_class7('gather_nd', _typename) for stype in support_types:
create_test_class(globals(), XPUTestGatherNd, stype)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册