提交 5bb9deb3 编写于 作者: J juncaipeng 提交者: Tao Luo

Update the precision of some op tests from fp32 to fp64 (#21774)

上级 a86f11b5
......@@ -42,7 +42,6 @@ class TestSqrtOpError(unittest.TestCase):
class TestActivation(OpTest):
def setUp(self):
self.op_type = "exp"
self.dtype = np.float32
self.init_dtype()
self.init_kernel_type()
......@@ -61,7 +60,7 @@ class TestActivation(OpTest):
self.check_grad(['X'], 'Out')
def init_dtype(self):
self.dtype = np.float32
self.dtype = np.float64
def init_kernel_type(self):
pass
......@@ -78,6 +77,9 @@ class TestSigmoid(TestActivation):
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)}
self.outputs = {'Out': out}
def init_dtype(self):
self.dtype = np.float32
def test_check_grad(self):
if self.dtype == np.float16:
return
......@@ -105,7 +107,6 @@ class TestTanh(TestActivation):
def setUp(self):
self.op_type = "tanh"
self.init_dtype()
x = np.random.uniform(0.1, 1, [11, 17]).astype(self.dtype)
out = np.tanh(x)
......@@ -117,6 +118,12 @@ class TestTanh(TestActivation):
return
self.check_grad(['X'], 'Out')
def init_dtype(self):
#TODO If dtype is float64, the output (Out) has diff at CPUPlace
# when using and not using inplace. Therefore, set dtype as float32
# for now.
self.dtype = np.float32
class TestAtan(TestActivation):
def setUp(self):
......
......@@ -28,7 +28,7 @@ class TestAddPositionEncodingTensorOp(OpTest):
the prepared section for add position encoding op
"""
self.op_type = "add_position_encoding"
self.dtype = np.float32
self.dtype = np.float64
self.init_input_output()
self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(self.x), }
......@@ -82,7 +82,7 @@ class TestAddPositionEncodingLoDTensorOp(OpTest):
the prepared section for add position encoding LoDTensor op
"""
self.op_type = "add_position_encoding"
self.dtype = np.float32
self.dtype = np.float64
self.init_input_output()
self.inputs = {'X': (self.x, self.lod), }
......
......@@ -26,7 +26,7 @@ from paddle.fluid import compiler, Program, program_guard
class TestAssignOp(op_test.OpTest):
def setUp(self):
self.op_type = "assign"
x = np.random.random(size=(100, 10))
x = np.random.random(size=(100, 10)).astype('float64')
self.inputs = {'X': x}
self.outputs = {'Out': x}
......
......@@ -100,7 +100,7 @@ class TestBilinearInterpOp(OpTest):
self.data_layout = 'NCHW'
self.init_test_case()
self.op_type = "bilinear_interp"
input_np = np.random.random(self.input_shape).astype("float32")
input_np = np.random.random(self.input_shape).astype("float64")
if self.data_layout == "NCHW":
in_h = self.input_shape[2]
......@@ -402,7 +402,7 @@ class TestBilinearInterpOp_attr_tensor(OpTest):
'align_corners': self.align_corners,
}
input_np = np.random.random(self.input_shape).astype("float32")
input_np = np.random.random(self.input_shape).astype("float64")
self.inputs = {'X': input_np}
if self.scale_by_1Dtensor:
......
......@@ -26,11 +26,11 @@ class TestBilinearTensorProductOp(OpTest):
size0 = 5
size1 = 4
size2 = 5
a = np.random.random((batch_size, size0)).astype("float32")
b = np.random.random((batch_size, size1)).astype("float32")
w = np.random.random((size2, size0, size1)).astype("float32")
bias = np.random.random((1, size2)).astype("float32")
output = np.zeros((batch_size, size2)).astype("float32")
a = np.random.random((batch_size, size0)).astype("float64")
b = np.random.random((batch_size, size1)).astype("float64")
w = np.random.random((size2, size0, size1)).astype("float64")
bias = np.random.random((1, size2)).astype("float64")
output = np.zeros((batch_size, size2)).astype("float64")
for i in range(size2):
w_i = w[i, :, :]
output[:, i] = np.sum(np.matmul(a, w_i) * b, axis=1)
......
......@@ -23,7 +23,7 @@ import paddle.fluid.core as core
class TestCenterLossOp(OpTest):
def setUp(self):
self.op_type = "center_loss"
self.dtype = np.float32
self.dtype = np.float64
self.init_dtype_type()
batch_size = 12
feet_dim = 10
......@@ -34,9 +34,9 @@ class TestCenterLossOp(OpTest):
self.config()
self.attrs['need_update'] = self.need_update
labels = np.random.randint(cluster_num, size=batch_size, dtype='int64')
feat = np.random.random((batch_size, feet_dim)).astype(np.float32)
centers = np.random.random((cluster_num, feet_dim)).astype(np.float32)
var_sum = np.zeros((cluster_num, feet_dim), dtype=np.float32)
feat = np.random.random((batch_size, feet_dim)).astype(np.float64)
centers = np.random.random((cluster_num, feet_dim)).astype(np.float64)
var_sum = np.zeros((cluster_num, feet_dim), dtype=np.float64)
centers_select = centers[labels]
output = feat - centers_select
diff_square = np.square(output).reshape(batch_size, feet_dim)
......@@ -51,7 +51,7 @@ class TestCenterLossOp(OpTest):
var_sum[i] /= (1 + cout[i])
var_sum *= 0.1
result = centers + var_sum
rate = np.array([0.1]).astype(np.float32)
rate = np.array([0.1]).astype(np.float64)
self.inputs = {
'X': feat,
......
......@@ -40,7 +40,7 @@ class TestConcatOp(OpTest):
}
def get_dtype(self):
return "float32"
return "float64"
def test_check_output(self):
self.check_output()
......
......@@ -49,13 +49,13 @@ class TestCropOp(OpTest):
self.initTestCase()
if self.crop_by_input:
self.inputs = {
'X': np.random.random(self.x_shape).astype("float32"),
'Y': np.random.random(self.crop_shape).astype("float32")
'X': np.random.random(self.x_shape).astype("float64"),
'Y': np.random.random(self.crop_shape).astype("float64")
}
else:
self.attrs['shape'] = self.crop_shape
self.inputs = {
'X': np.random.random(self.x_shape).astype("float32"),
'X': np.random.random(self.x_shape).astype("float64"),
}
if self.offset_by_input:
self.inputs['Offsets'] = np.array(self.offsets).astype('int32')
......
......@@ -52,13 +52,13 @@ class TestCropTensorOp(OpTest):
if self.shape_by_input:
self.inputs = {
'X': np.random.random(self.x_shape).astype("float32"),
'X': np.random.random(self.x_shape).astype("float64"),
'Shape': np.array(self.crop_shape).astype("int32")
}
else:
self.attrs['shape'] = self.crop_shape
self.inputs = {
'X': np.random.random(self.x_shape).astype("float32"),
'X': np.random.random(self.x_shape).astype("float64"),
}
if self.offset_by_input:
self.inputs['Offsets'] = np.array(self.offsets).astype('int32')
......@@ -144,7 +144,7 @@ class TestCropTensorOpTensorAttr(OpTest):
shape_tensor.append(("x" + str(index), np.ones(
(1)).astype('int32') * ele))
self.inputs = {
'X': np.random.random(self.x_shape).astype("float32"),
'X': np.random.random(self.x_shape).astype("float64"),
'ShapeTensor': shape_tensor
}
self.attrs['shape'] = self.shape_attr
......@@ -155,7 +155,7 @@ class TestCropTensorOpTensorAttr(OpTest):
offsets_tensor.append(("x" + str(index), np.ones(
(1)).astype('int32') * ele))
self.inputs = {
'X': np.random.random(self.x_shape).astype("float32"),
'X': np.random.random(self.x_shape).astype("float64"),
'OffsetsTensor': offsets_tensor
}
self.attrs['offsets'] = self.offsets_attr
......
......@@ -20,7 +20,7 @@ import six
class CrossEntropy2OpTestBase(OpTest):
def initParameters(self):
return [32, 64], 'float32', -100, False
return [32, 64], 'float64', -100, False
def calc_output(self, logits, label, ignore_index):
ret = np.zeros(shape=label.shape, dtype=logits.dtype)
......@@ -78,17 +78,17 @@ class CrossEntropy2OpTest2RemoveLastDim(CrossEntropy2OpTestBase):
class CrossEntropy2OpTest3(CrossEntropy2OpTestBase):
def initParameters(self):
return [4, 8, 16, 32], 'float32', -100, False
return [4, 8, 16, 32], 'float64', -100, False
class CrossEntropy2OpTest3RemoveLastDim(CrossEntropy2OpTestBase):
def initParameters(self):
return [4, 8, 16, 32], 'float32', -100, True
return [4, 8, 16, 32], 'float64', -100, True
class CrossEntropy2OpTest4(CrossEntropy2OpTestBase):
def initParameters(self):
return [4, 8, 16, 32], 'float32', 3, False
return [4, 8, 16, 32], 'float64', 3, False
if __name__ == '__main__':
......
......@@ -112,7 +112,7 @@ class TestCrossEntropyOp2(TestCrossEntropyOp):
self.soft_label = True
def init_dtype_type(self):
self.dtype = np.float32
self.dtype = np.float64
def init_bs_class_num(self):
self.batch_size = 5
......@@ -142,7 +142,7 @@ class TestCrossEntropyOp3(TestCrossEntropyOp):
self.soft_label = True
def init_dtype_type(self):
self.dtype = np.float32
self.dtype = np.float64
def init_bs_class_num(self):
self.batch_size = 5
......@@ -229,7 +229,7 @@ class TestCrossEntropyOp5(TestCrossEntropyOp):
self.soft_label = True
def init_dtype_type(self):
self.dtype = np.float32
self.dtype = np.float64
def init_bs_class_num(self):
self.class_num = 37
......@@ -269,7 +269,7 @@ class TestCrossEntropyOp6(TestCrossEntropyOp):
self.soft_label = True
def init_dtype_type(self):
self.dtype = np.float32
self.dtype = np.float64
def init_bs_class_num(self):
self.class_num = 17
......
......@@ -27,7 +27,7 @@ class TestElementwiseAddOp(OpTest):
def setUp(self):
self.op_type = "elementwise_add"
self.dtype = np.float32
self.dtype = np.float64
self.axis = -1
self.init_dtype()
self.init_input_output()
......
......@@ -22,7 +22,7 @@ from op_test import OpTest
class ElementwiseDivOp(OpTest):
def setUp(self):
self.op_type = "elementwise_div"
self.dtype = np.float32
self.dtype = np.float64
self.init_dtype()
""" Warning
CPU gradient check error!
......@@ -57,8 +57,8 @@ class TestElementwiseDivOp_scalar(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype(np.float32),
'Y': np.random.uniform(0.1, 1, [1]).astype(np.float32)
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype(np.float64),
'Y': np.random.uniform(0.1, 1, [1]).astype(np.float64)
}
self.outputs = {'Out': self.inputs['X'] / self.inputs['Y']}
......@@ -67,8 +67,8 @@ class TestElementwiseDivOp_Vector(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [32]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [32]).astype("float32")
'X': np.random.uniform(0.1, 1, [32]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [32]).astype("float64")
}
self.outputs = {'Out': np.divide(self.inputs['X'], self.inputs['Y'])}
......@@ -77,8 +77,8 @@ class TestElementwiseDivOp_broadcast_0(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [2]).astype("float32")
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [2]).astype("float64")
}
self.attrs = {'axis': 0}
......@@ -92,8 +92,8 @@ class TestElementwiseDivOp_broadcast_1(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [3]).astype("float32")
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [3]).astype("float64")
}
self.attrs = {'axis': 1}
......@@ -107,8 +107,8 @@ class TestElementwiseDivOp_broadcast_2(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [4]).astype("float32")
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [4]).astype("float64")
}
self.outputs = {
......@@ -121,8 +121,8 @@ class TestElementwiseDivOp_broadcast_3(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [3, 4]).astype("float32")
'X': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [3, 4]).astype("float64")
}
self.attrs = {'axis': 1}
......@@ -136,8 +136,8 @@ class TestElementwiseDivOp_broadcast_4(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [2, 1, 4]).astype("float32")
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [2, 1, 4]).astype("float64")
}
self.outputs = {'Out': np.divide(self.inputs['X'], self.inputs['Y'])}
......@@ -146,8 +146,8 @@ class TestElementwiseDivOp_broadcast_5(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [2, 3, 1, 5]).astype("float32")
'X': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [2, 3, 1, 5]).astype("float64")
}
self.outputs = {'Out': np.divide(self.inputs['X'], self.inputs['Y'])}
......@@ -156,8 +156,8 @@ class TestElementwiseDivOp_commonuse_1(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [1, 1, 4]).astype("float32"),
'X': np.random.uniform(0.1, 1, [2, 3, 4]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [1, 1, 4]).astype("float64"),
}
self.outputs = {'Out': np.divide(self.inputs['X'], self.inputs['Y'])}
......@@ -166,8 +166,8 @@ class TestElementwiseDivOp_commonuse_2(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 1, 5]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [2, 1, 4, 1]).astype("float32"),
'X': np.random.uniform(0.1, 1, [2, 3, 1, 5]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [2, 1, 4, 1]).astype("float64"),
}
self.outputs = {'Out': np.divide(self.inputs['X'], self.inputs['Y'])}
......@@ -176,8 +176,8 @@ class TestElementwiseDivOp_xsize_lessthan_ysize(ElementwiseDivOp):
def setUp(self):
self.op_type = "elementwise_div"
self.inputs = {
'X': np.random.uniform(0.1, 1, [4, 5]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float32"),
'X': np.random.uniform(0.1, 1, [4, 5]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float64"),
}
self.attrs = {'axis': 2}
......
......@@ -28,7 +28,7 @@ class ElementwiseMulOp(OpTest):
def setUp(self):
self.op_type = "elementwise_mul"
self.dtype = np.float32
self.dtype = np.float64
self.axis = -1
self.init_dtype()
self.init_input_output()
......@@ -83,8 +83,8 @@ class TestElementwiseMulOp_scalar(ElementwiseMulOp):
def setUp(self):
self.op_type = "elementwise_mul"
self.inputs = {
'X': np.random.rand(10, 3, 4).astype(np.float32),
'Y': np.random.rand(1).astype(np.float32)
'X': np.random.rand(10, 3, 4).astype(np.float64),
'Y': np.random.rand(1).astype(np.float64)
}
self.outputs = {'Out': self.inputs['X'] * self.inputs['Y']}
self.init_kernel_type()
......
......@@ -22,8 +22,8 @@ class TestElementwiseOp(OpTest):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float32"),
'Y': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float32")
'X': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float64"),
'Y': np.random.uniform(0.1, 1, [2, 3, 4, 5]).astype("float64")
}
self.outputs = {'Out': self.inputs['X'] - self.inputs['Y']}
......@@ -46,8 +46,8 @@ class TestElementwiseSubOp_scalar(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(10, 3, 4).astype(np.float32),
'Y': np.random.rand(1).astype(np.float32)
'X': np.random.rand(10, 3, 4).astype(np.float64),
'Y': np.random.rand(1).astype(np.float64)
}
self.outputs = {'Out': self.inputs['X'] - self.inputs['Y']}
......@@ -56,8 +56,8 @@ class TestElementwiseSubOp_Vector(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.random((100, )).astype("float32"),
'Y': np.random.random((100, )).astype("float32")
'X': np.random.random((100, )).astype("float64"),
'Y': np.random.random((100, )).astype("float64")
}
self.outputs = {'Out': self.inputs['X'] - self.inputs['Y']}
......@@ -66,8 +66,8 @@ class TestElementwiseSubOp_broadcast_0(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(2, 13, 4).astype(np.float32),
'Y': np.random.rand(2).astype(np.float32)
'X': np.random.rand(2, 13, 4).astype(np.float64),
'Y': np.random.rand(2).astype(np.float64)
}
self.attrs = {'axis': 0}
......@@ -80,8 +80,8 @@ class TestElementwiseSubOp_broadcast_1(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(2, 3, 4).astype(np.float32),
'Y': np.random.rand(3).astype(np.float32)
'X': np.random.rand(2, 3, 4).astype(np.float64),
'Y': np.random.rand(3).astype(np.float64)
}
self.attrs = {'axis': 1}
......@@ -94,8 +94,8 @@ class TestElementwiseSubOp_broadcast_2(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(2, 3, 4).astype(np.float32),
'Y': np.random.rand(4).astype(np.float32)
'X': np.random.rand(2, 3, 4).astype(np.float64),
'Y': np.random.rand(4).astype(np.float64)
}
self.outputs = {
......@@ -107,8 +107,8 @@ class TestElementwiseSubOp_broadcast_3(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(2, 3, 4, 5).astype(np.float32),
'Y': np.random.rand(3, 4).astype(np.float32)
'X': np.random.rand(2, 3, 4, 5).astype(np.float64),
'Y': np.random.rand(3, 4).astype(np.float64)
}
self.attrs = {'axis': 1}
......@@ -121,8 +121,8 @@ class TestElementwiseSubOp_broadcast_4(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(2, 3, 4, 5).astype(np.float32),
'Y': np.random.rand(2, 3, 1, 5).astype(np.float32)
'X': np.random.rand(2, 3, 4, 5).astype(np.float64),
'Y': np.random.rand(2, 3, 1, 5).astype(np.float64)
}
self.outputs = {'Out': self.inputs['X'] - self.inputs['Y']}
......@@ -131,8 +131,8 @@ class TestElementwiseSubOp_commonuse_1(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(2, 3, 4).astype(np.float32),
'Y': np.random.rand(1, 1, 4).astype(np.float32)
'X': np.random.rand(2, 3, 4).astype(np.float64),
'Y': np.random.rand(1, 1, 4).astype(np.float64)
}
self.outputs = {'Out': self.inputs['X'] - self.inputs['Y']}
......@@ -141,8 +141,8 @@ class TestElementwiseSubOp_commonuse_2(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(2, 3, 1, 5).astype(np.float32),
'Y': np.random.rand(2, 1, 4, 1).astype(np.float32)
'X': np.random.rand(2, 3, 1, 5).astype(np.float64),
'Y': np.random.rand(2, 1, 4, 1).astype(np.float64)
}
self.outputs = {'Out': self.inputs['X'] - self.inputs['Y']}
......@@ -151,8 +151,8 @@ class TestElementwiseSubOp_xsize_lessthan_ysize(TestElementwiseOp):
def setUp(self):
self.op_type = "elementwise_sub"
self.inputs = {
'X': np.random.rand(4, 5).astype(np.float32),
'Y': np.random.rand(2, 3, 4, 5).astype(np.float32)
'X': np.random.rand(4, 5).astype(np.float64),
'Y': np.random.rand(2, 3, 4, 5).astype(np.float64)
}
self.attrs = {'axis': 2}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册