未验证 提交 934d25e7 编写于 作者: zhouweiwei2014's avatar zhouweiwei2014 提交者: GitHub

fix XPU unittest problem for shape check (#54365)

上级 9fb2c603
...@@ -348,6 +348,7 @@ tensor_method_func = [ # noqa ...@@ -348,6 +348,7 @@ tensor_method_func = [ # noqa
'logit', 'logit',
'exp', 'exp',
'exp_', 'exp_',
'expm1',
'floor', 'floor',
'floor_', 'floor_',
'increment', 'increment',
......
...@@ -48,11 +48,9 @@ class XPUTestAccuracyOp(XPUOpTestWrapper): ...@@ -48,11 +48,9 @@ class XPUTestAccuracyOp(XPUOpTestWrapper):
num_correct += 1 num_correct += 1
break break
self.outputs = { self.outputs = {
'Accuracy': np.array([num_correct / float(n)]).astype( 'Accuracy': np.array(num_correct / float(n)).astype(self.dtype),
self.dtype 'Correct': np.array(num_correct).astype("int32"),
), 'Total': np.array(n).astype("int32"),
'Correct': np.array([num_correct]).astype("int32"),
'Total': np.array([n]).astype("int32"),
} }
self.attrs = {'use_xpu': True} self.attrs = {'use_xpu': True}
......
...@@ -93,7 +93,7 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper): ...@@ -93,7 +93,7 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper):
def set_inputs(self): def set_inputs(self):
x = np.random.rand(1, 1, 7, 16).astype(self.dtype) x = np.random.rand(1, 1, 7, 16).astype(self.dtype)
self.inputs = {'X': x} self.inputs = {'X': x}
target_tensor = np.random.rand(1, 1, 7, 16).astype(self.dtype) target_tensor = np.random.rand(4, 6, 7, 16).astype(self.dtype)
self.attrs = {'target_shape': target_tensor.shape} self.attrs = {'target_shape': target_tensor.shape}
def set_output(self): def set_output(self):
...@@ -105,7 +105,7 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper): ...@@ -105,7 +105,7 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper):
def set_inputs(self): def set_inputs(self):
x = np.random.rand(1, 1, 7, 16, 1).astype(self.dtype) x = np.random.rand(1, 1, 7, 16, 1).astype(self.dtype)
self.inputs = {'X': x} self.inputs = {'X': x}
target_tensor = np.random.rand(1, 1, 7, 16, 1).astype(self.dtype) target_tensor = np.random.rand(4, 6, 7, 16, 1).astype(self.dtype)
self.attrs = {'target_shape': target_tensor.shape} self.attrs = {'target_shape': target_tensor.shape}
def set_output(self): def set_output(self):
...@@ -117,7 +117,7 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper): ...@@ -117,7 +117,7 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper):
def set_inputs(self): def set_inputs(self):
x = np.random.rand(1, 1, 7, 16, 1, 1).astype(self.dtype) x = np.random.rand(1, 1, 7, 16, 1, 1).astype(self.dtype)
self.inputs = {'X': x} self.inputs = {'X': x}
target_tensor = np.random.rand(1, 1, 7, 16, 1, 1).astype(self.dtype) target_tensor = np.random.rand(4, 6, 7, 16, 1, 1).astype(self.dtype)
self.attrs = {'target_shape': target_tensor.shape} self.attrs = {'target_shape': target_tensor.shape}
def set_output(self): def set_output(self):
...@@ -129,8 +129,8 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper): ...@@ -129,8 +129,8 @@ class XPUTestExpandAsV2Op(XPUOpTestWrapper):
# Test python API # Test python API
class TestExpandAsV2API(unittest.TestCase): class TestExpandAsV2API(unittest.TestCase):
def test_api(self): def test_api(self):
input1 = np.random.random([12, 14]).astype("float32") x_np = np.random.random([12, 14]).astype("float32")
input2 = np.random.random([2, 12, 14]).astype("float32") y_np = np.random.random([2, 12, 14]).astype("float32")
x = paddle.static.data(name='x', shape=[12, 14], dtype="float32") x = paddle.static.data(name='x', shape=[12, 14], dtype="float32")
y = paddle.static.data( y = paddle.static.data(
...@@ -144,10 +144,10 @@ class TestExpandAsV2API(unittest.TestCase): ...@@ -144,10 +144,10 @@ class TestExpandAsV2API(unittest.TestCase):
exe = fluid.Executor(place=fluid.XPUPlace(0)) exe = fluid.Executor(place=fluid.XPUPlace(0))
res_1 = exe.run( res_1 = exe.run(
fluid.default_main_program(), fluid.default_main_program(),
feed={"x": input1, "target_tensor": input2}, feed={"x": x_np, "target_tensor": y_np},
fetch_list=[out_1], fetch_list=[out_1],
) )
assert np.array_equal(res_1[0], np.tile(input1, (2, 1, 1))) assert np.array_equal(res_1[0], np.tile(x_np, (2, 1, 1)))
support_types = get_xpu_op_support_types('expand_as_v2') support_types = get_xpu_op_support_types('expand_as_v2')
......
...@@ -81,13 +81,13 @@ class XPUTestGatherNd(XPUOpTestWrapper): ...@@ -81,13 +81,13 @@ class XPUTestGatherNd(XPUOpTestWrapper):
def init_data(self): def init_data(self):
self.xnp = np.random.random((5, 20)).astype(self.in_type) self.xnp = np.random.random((5, 20)).astype(self.in_type)
self.inp = np.array([1]).astype("int32") self.inp = np.array([1]).astype("int32")
self.output = self.xnp[self.inp] self.output = self.xnp[tuple(self.inp)]
class XPUTestGatherNdOpWithIndex2(XPUTestGatherNdBase): class XPUTestGatherNdOpWithIndex2(XPUTestGatherNdBase):
def init_data(self): def init_data(self):
self.xnp = np.random.random((5, 20)).astype(self.in_type) self.xnp = np.random.random((5, 20)).astype(self.in_type)
self.inp = np.array([1]).astype("int64") self.inp = np.array([1]).astype("int64")
self.output = self.xnp[self.inp] self.output = self.xnp[tuple(self.inp)]
class XPUTestGatherNdOpWithLowIndex1(XPUTestGatherNdBase): class XPUTestGatherNdOpWithLowIndex1(XPUTestGatherNdBase):
def init_data(self): def init_data(self):
......
...@@ -45,16 +45,16 @@ def nll_loss_1d( ...@@ -45,16 +45,16 @@ def nll_loss_1d(
out[i] = -logs[i][cur_target] * cur_weight out[i] = -logs[i][cur_target] * cur_weight
if reduction == 'sum': if reduction == 'sum':
out = np.sum(out) out = np.sum(out)
total_weight = np.array([total_weight]).astype(dtype) total_weight = np.array(total_weight).astype(dtype)
return {'Out': out, 'Total_weight': total_weight} return {'Out': out, 'Total_weight': total_weight}
elif reduction == 'mean': elif reduction == 'mean':
out = np.sum(out) out = np.sum(out)
if total_weight != 0: if total_weight != 0:
out /= total_weight out /= total_weight
total_weight = np.array([total_weight]).astype(dtype) total_weight = np.array(total_weight).astype(dtype)
return {'Out': out, 'Total_weight': total_weight} return {'Out': out, 'Total_weight': total_weight}
elif reduction == 'none': elif reduction == 'none':
total_weight = np.array([0]).astype(dtype) total_weight = np.array(0).astype(dtype)
return {'Out': out, 'Total_weight': total_weight} return {'Out': out, 'Total_weight': total_weight}
...@@ -79,16 +79,16 @@ def nll_loss_2d( ...@@ -79,16 +79,16 @@ def nll_loss_2d(
out[i][h][w] = -logs[i][cur_target][h][w] * cur_weight out[i][h][w] = -logs[i][cur_target][h][w] * cur_weight
if reduction == 'sum': if reduction == 'sum':
out = np.sum(out) out = np.sum(out)
total_weight = np.array([total_weight]).astype(dtype) total_weight = np.array(total_weight).astype(dtype)
return {'Out': out, 'Total_weight': total_weight} return {'Out': out, 'Total_weight': total_weight}
elif reduction == 'mean': elif reduction == 'mean':
out = np.sum(out) out = np.sum(out)
if total_weight != 0: if total_weight != 0:
out /= total_weight out /= total_weight
total_weight = np.array([total_weight]).astype(dtype) total_weight = np.array(total_weight).astype(dtype)
return {'Out': out, 'Total_weight': total_weight} return {'Out': out, 'Total_weight': total_weight}
elif reduction == 'none': elif reduction == 'none':
total_weight = np.array([0]).astype(dtype) total_weight = np.array(0).astype(dtype)
return {'Out': out, 'Total_weight': total_weight} return {'Out': out, 'Total_weight': total_weight}
......
...@@ -40,8 +40,8 @@ class XPUTestReduceAnyOp(XPUOpTestWrapper): ...@@ -40,8 +40,8 @@ class XPUTestReduceAnyOp(XPUOpTestWrapper):
self.op_type = 'reduce_any' self.op_type = 'reduce_any'
self.attrs = { self.attrs = {
'use_xpu': True, 'use_xpu': True,
'reduce_all': True, 'reduce_all': False,
'keep_dim': True, 'keep_dim': False,
'dim': (3, 5, 4), 'dim': (3, 5, 4),
} }
self.inputs = { self.inputs = {
...@@ -76,7 +76,7 @@ class XPUTestReduceAnyOp(XPUOpTestWrapper): ...@@ -76,7 +76,7 @@ class XPUTestReduceAnyOp(XPUOpTestWrapper):
self.op_type = 'reduce_any' self.op_type = 'reduce_any'
self.attrs = { self.attrs = {
'use_xpu': True, 'use_xpu': True,
'reduce_all': True, 'reduce_all': False,
'keep_dim': False, 'keep_dim': False,
'dim': (3, 6), 'dim': (3, 6),
} }
......
...@@ -43,7 +43,7 @@ class XPUTestSizeOP(XPUOpTestWrapper): ...@@ -43,7 +43,7 @@ class XPUTestSizeOP(XPUOpTestWrapper):
self.inputs = { self.inputs = {
'Input': x, 'Input': x,
} }
self.outputs = {'Out': np.array([np.size(x)])} self.outputs = {'Out': np.array(np.size(x))}
def initTestCase(self): def initTestCase(self):
self.shape = (6, 56, 8, 55) self.shape = (6, 56, 8, 55)
......
...@@ -97,7 +97,9 @@ class XPUTestUnbindOP(XPUOpTestWrapper): ...@@ -97,7 +97,9 @@ class XPUTestUnbindOP(XPUOpTestWrapper):
pass pass
def outReshape(self): def outReshape(self):
pass self.out[0] = self.out[0].reshape((2, 2))
self.out[1] = self.out[1].reshape((2, 2))
self.out[2] = self.out[2].reshape((2, 2))
def setAxis(self): def setAxis(self):
pass pass
......
...@@ -58,7 +58,7 @@ class XPUTestWhereIndexOp(XPUOpTestWrapper): ...@@ -58,7 +58,7 @@ class XPUTestWhereIndexOp(XPUOpTestWrapper):
self.inputs = { self.inputs = {
'Condition': np.array([False, False, False]).astype(self.dtype), 'Condition': np.array([False, False, False]).astype(self.dtype),
} }
self.outputs = {'Out': np.array([], dtype='int64')} self.outputs = {'Out': np.array([], dtype='int64').reshape(0, 1)}
class TestRank2(TestWhereIndexOp): class TestRank2(TestWhereIndexOp):
def init_data(self): def init_data(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册