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

fix XPU unittest problem for shape check (#54365)

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