提交 be6ecec4 编写于 作者: M minqiyang

Fix unittests' division issues

上级 59adf7ce
...@@ -550,7 +550,7 @@ def dynamic_lstmp(input, ...@@ -550,7 +550,7 @@ def dynamic_lstmp(input,
""" """
helper = LayerHelper('lstmp', **locals()) helper = LayerHelper('lstmp', **locals())
size = size / 4 size = size // 4
weight = helper.create_parameter( weight = helper.create_parameter(
attr=helper.param_attr, shape=[proj_size, 4 * size], dtype=dtype) attr=helper.param_attr, shape=[proj_size, 4 * size], dtype=dtype)
proj_weight = helper.create_parameter( proj_weight = helper.create_parameter(
...@@ -778,7 +778,7 @@ def gru_unit(input, ...@@ -778,7 +778,7 @@ def gru_unit(input,
helper = LayerHelper('gru_unit', **locals()) helper = LayerHelper('gru_unit', **locals())
dtype = helper.input_dtype() dtype = helper.input_dtype()
size = size / 3 size = size // 3
# create weight # create weight
weight = helper.create_parameter( weight = helper.create_parameter(
...@@ -1258,7 +1258,7 @@ def sequence_conv(input, ...@@ -1258,7 +1258,7 @@ def sequence_conv(input,
outputs={"Out": pre_bias}, outputs={"Out": pre_bias},
attrs={ attrs={
'contextStride': filter_stride, 'contextStride': filter_stride,
'contextStart': -int(filter_size / 2), 'contextStart': -int(filter_size // 2),
'contextLength': filter_size 'contextLength': filter_size
}) })
pre_act = helper.append_bias_op(pre_bias) pre_act = helper.append_bias_op(pre_bias)
...@@ -1487,7 +1487,7 @@ def conv2d(input, ...@@ -1487,7 +1487,7 @@ def conv2d(input,
else: else:
if num_channels % groups != 0: if num_channels % groups != 0:
raise ValueError("num_channels must be divisible by groups.") raise ValueError("num_channels must be divisible by groups.")
num_filter_channels = num_channels / groups num_filter_channels = num_channels // groups
filter_size = utils.convert_to_list(filter_size, 2, 'filter_size') filter_size = utils.convert_to_list(filter_size, 2, 'filter_size')
stride = utils.convert_to_list(stride, 2, 'stride') stride = utils.convert_to_list(stride, 2, 'stride')
...@@ -1649,7 +1649,7 @@ def conv3d(input, ...@@ -1649,7 +1649,7 @@ def conv3d(input,
else: else:
if num_channels % groups != 0: if num_channels % groups != 0:
raise ValueError("num_channels must be divisible by groups.") raise ValueError("num_channels must be divisible by groups.")
num_filter_channels = num_channels / groups num_filter_channels = num_channels // groups
filter_size = utils.convert_to_list(filter_size, 3, 'filter_size') filter_size = utils.convert_to_list(filter_size, 3, 'filter_size')
stride = utils.convert_to_list(stride, 3, 'stride') stride = utils.convert_to_list(stride, 3, 'stride')
...@@ -2384,16 +2384,16 @@ def conv2d_transpose(input, ...@@ -2384,16 +2384,16 @@ def conv2d_transpose(input,
w_in = input.shape[3] w_in = input.shape[3]
filter_size_h = (output_size[0] - (h_in - 1) * stride[0] + 2 * filter_size_h = (output_size[0] - (h_in - 1) * stride[0] + 2 *
padding[0] - 1) / dilation[0] + 1 padding[0] - 1) // dilation[0] + 1
filter_size_w = (output_size[1] - (w_in - 1) * stride[1] + 2 * filter_size_w = (output_size[1] - (w_in - 1) * stride[1] + 2 *
padding[1] - 1) / dilation[1] + 1 padding[1] - 1) // dilation[1] + 1
filter_size = [filter_size_h, filter_size_w] filter_size = [filter_size_h, filter_size_w]
else: else:
filter_size = utils.convert_to_list(filter_size, 2, filter_size = utils.convert_to_list(filter_size, 2,
'conv2d_transpose.filter_size') 'conv2d_transpose.filter_size')
groups = 1 if groups is None else groups groups = 1 if groups is None else groups
filter_shape = [input_channel, num_filters / groups] + filter_size filter_shape = [input_channel, num_filters // groups] + filter_size
img_filter = helper.create_parameter( img_filter = helper.create_parameter(
dtype=input.dtype, shape=filter_shape, attr=helper.param_attr) dtype=input.dtype, shape=filter_shape, attr=helper.param_attr)
...@@ -2551,18 +2551,18 @@ def conv3d_transpose(input, ...@@ -2551,18 +2551,18 @@ def conv3d_transpose(input,
w_in = input.shape[4] w_in = input.shape[4]
filter_size_d = (output_size[0] - (d_in - 1) * stride[0] + 2 * filter_size_d = (output_size[0] - (d_in - 1) * stride[0] + 2 *
padding[0] - 1) / dilation[0] + 1 padding[0] - 1) // dilation[0] + 1
filter_size_h = (output_size[1] - (h_in - 1) * stride[1] + 2 * filter_size_h = (output_size[1] - (h_in - 1) * stride[1] + 2 *
padding[1] - 1) / dilation[1] + 1 padding[1] - 1) // dilation[1] + 1
filter_size_w = (output_size[2] - (w_in - 1) * stride[2] + 2 * filter_size_w = (output_size[2] - (w_in - 1) * stride[2] + 2 *
padding[2] - 1) / dilation[2] + 1 padding[2] - 1) // dilation[2] + 1
filter_size = [filter_size_d, filter_size_h, filter_size_w] filter_size = [filter_size_d, filter_size_h, filter_size_w]
else: else:
filter_size = utils.convert_to_list(filter_size, 3, filter_size = utils.convert_to_list(filter_size, 3,
'conv3d_transpose.filter_size') 'conv3d_transpose.filter_size')
groups = 1 if groups is None else groups groups = 1 if groups is None else groups
filter_shape = [input_channel, num_filters / groups] + filter_size filter_shape = [input_channel, num_filters // groups] + filter_size
img_filter = helper.create_parameter( img_filter = helper.create_parameter(
dtype=input.dtype, shape=filter_shape, attr=helper.param_attr) dtype=input.dtype, shape=filter_shape, attr=helper.param_attr)
......
...@@ -24,14 +24,14 @@ def conv3d_forward_naive(input, filter, group, conv_param): ...@@ -24,14 +24,14 @@ def conv3d_forward_naive(input, filter, group, conv_param):
out_c, f_c, f_d, f_h, f_w = filter.shape out_c, f_c, f_d, f_h, f_w = filter.shape
assert f_c * group == in_c assert f_c * group == in_c
assert np.mod(out_c, group) == 0 assert np.mod(out_c, group) == 0
sub_out_c = out_c / group sub_out_c = out_c // group
stride, pad, dilation = conv_param['stride'], conv_param['pad'], conv_param[ stride, pad, dilation = conv_param['stride'], conv_param['pad'], conv_param[
'dilations'] 'dilations']
out_d = 1 + (in_d + 2 * pad[0] - (dilation[0] * (f_d - 1) + 1)) / stride[0] out_d = 1 + (in_d + 2 * pad[0] - (dilation[0] * (f_d - 1) + 1)) // stride[0]
out_h = 1 + (in_h + 2 * pad[1] - (dilation[1] * (f_h - 1) + 1)) / stride[1] out_h = 1 + (in_h + 2 * pad[1] - (dilation[1] * (f_h - 1) + 1)) // stride[1]
out_w = 1 + (in_w + 2 * pad[2] - (dilation[2] * (f_w - 1) + 1)) / stride[2] out_w = 1 + (in_w + 2 * pad[2] - (dilation[2] * (f_w - 1) + 1)) // stride[2]
out = np.zeros((in_n, out_c, out_d, out_h, out_w)) out = np.zeros((in_n, out_c, out_d, out_h, out_w))
...@@ -166,7 +166,7 @@ class TestConv3dOp(OpTest): ...@@ -166,7 +166,7 @@ class TestConv3dOp(OpTest):
self.stride = [1, 1, 1] self.stride = [1, 1, 1]
self.input_size = [2, 3, 4, 4, 4] # NCDHW self.input_size = [2, 3, 4, 4, 4] # NCDHW
assert np.mod(self.input_size[1], self.groups) == 0 assert np.mod(self.input_size[1], self.groups) == 0
f_c = self.input_size[1] / self.groups f_c = self.input_size[1] // self.groups
self.filter_size = [6, f_c, 3, 3, 3] self.filter_size = [6, f_c, 3, 3, 3]
def init_dilation(self): def init_dilation(self):
...@@ -185,7 +185,7 @@ class TestCase1(TestConv3dOp): ...@@ -185,7 +185,7 @@ class TestCase1(TestConv3dOp):
self.stride = [1, 1, 1] self.stride = [1, 1, 1]
self.input_size = [2, 3, 4, 4, 4] # NCDHW self.input_size = [2, 3, 4, 4, 4] # NCDHW
assert np.mod(self.input_size[1], self.groups) == 0 assert np.mod(self.input_size[1], self.groups) == 0
f_c = self.input_size[1] / self.groups f_c = self.input_size[1] // self.groups
self.filter_size = [6, f_c, 3, 3, 3] self.filter_size = [6, f_c, 3, 3, 3]
...@@ -205,7 +205,7 @@ class TestWith1x1(TestConv3dOp): ...@@ -205,7 +205,7 @@ class TestWith1x1(TestConv3dOp):
self.stride = [1, 1, 1] self.stride = [1, 1, 1]
self.input_size = [2, 3, 4, 4, 4] # NCHW self.input_size = [2, 3, 4, 4, 4] # NCHW
assert np.mod(self.input_size[1], self.groups) == 0 assert np.mod(self.input_size[1], self.groups) == 0
f_c = self.input_size[1] / self.groups f_c = self.input_size[1] // self.groups
self.filter_size = [6, f_c, 1, 1, 1] self.filter_size = [6, f_c, 1, 1, 1]
def init_dilation(self): def init_dilation(self):
...@@ -221,7 +221,7 @@ class TestWithInput1x1Filter1x1(TestConv3dOp): ...@@ -221,7 +221,7 @@ class TestWithInput1x1Filter1x1(TestConv3dOp):
self.stride = [1, 1, 1] self.stride = [1, 1, 1]
self.input_size = [2, 3, 1, 1, 1] # NCHW self.input_size = [2, 3, 1, 1, 1] # NCHW
assert np.mod(self.input_size[1], self.groups) == 0 assert np.mod(self.input_size[1], self.groups) == 0
f_c = self.input_size[1] / self.groups f_c = self.input_size[1] // self.groups
self.filter_size = [6, f_c, 1, 1, 1] self.filter_size = [6, f_c, 1, 1, 1]
def init_dilation(self): def init_dilation(self):
...@@ -237,7 +237,7 @@ class TestWithDilation(TestConv3dOp): ...@@ -237,7 +237,7 @@ class TestWithDilation(TestConv3dOp):
self.stride = [1, 1, 1] self.stride = [1, 1, 1]
self.input_size = [2, 3, 6, 6, 6] # NCDHW self.input_size = [2, 3, 6, 6, 6] # NCDHW
assert np.mod(self.input_size[1], self.groups) == 0 assert np.mod(self.input_size[1], self.groups) == 0
f_c = self.input_size[1] / self.groups f_c = self.input_size[1] // self.groups
self.filter_size = [6, f_c, 2, 2, 2] self.filter_size = [6, f_c, 2, 2, 2]
def init_dilation(self): def init_dilation(self):
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
import unittest import unittest
import six
import paddle.fluid.core as core import paddle.fluid.core as core
...@@ -27,14 +28,14 @@ class TestInferShape(unittest.TestCase): ...@@ -27,14 +28,14 @@ class TestInferShape(unittest.TestCase):
shape = [10, 20] shape = [10, 20]
# prepare input/output # prepare input/output
x1 = block.var("x1") x1 = block.var(six.b("x1"))
x1.set_type(core.VarDesc.VarType.LOD_TENSOR) x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
x1.set_shape(shape) x1.set_shape(shape)
x2 = block.var("x2") x2 = block.var(six.b("x2"))
x2.set_type(core.VarDesc.VarType.LOD_TENSOR) x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
x2.set_shape(shape) x2.set_shape(shape)
out = block.var("out") out = block.var(six.b("out"))
out.set_type(core.VarDesc.VarType.LOD_TENSOR) out.set_type(core.VarDesc.VarType.LOD_TENSOR)
# prepare the operator # prepare the operator
...@@ -57,14 +58,14 @@ class TestInferShape(unittest.TestCase): ...@@ -57,14 +58,14 @@ class TestInferShape(unittest.TestCase):
y_shape = [20, 30] y_shape = [20, 30]
# prepare input/output # prepare input/output
x1 = block.var("x") x1 = block.var(six.b("x"))
x1.set_type(core.VarDesc.VarType.LOD_TENSOR) x1.set_type(core.VarDesc.VarType.LOD_TENSOR)
x1.set_shape(x_shape) x1.set_shape(x_shape)
x2 = block.var("y") x2 = block.var(six.b("y"))
x2.set_type(core.VarDesc.VarType.LOD_TENSOR) x2.set_type(core.VarDesc.VarType.LOD_TENSOR)
x2.set_shape(y_shape) x2.set_shape(y_shape)
out = block.var("out") out = block.var(six.b("out"))
out.set_type(core.VarDesc.VarType.LOD_TENSOR) out.set_type(core.VarDesc.VarType.LOD_TENSOR)
# prepare the operator # prepare the operator
......
...@@ -158,7 +158,7 @@ class TestBook(unittest.TestCase): ...@@ -158,7 +158,7 @@ class TestBook(unittest.TestCase):
input=crf_decode, input=crf_decode,
label=label, label=label,
chunk_scheme="IOB", chunk_scheme="IOB",
num_chunk_types=(label_dict_len - 1) / 2) num_chunk_types=(label_dict_len - 1) // 2)
self.assertFalse(crf is None) self.assertFalse(crf is None)
self.assertFalse(crf_decode is None) self.assertFalse(crf_decode is None)
...@@ -285,7 +285,7 @@ class TestBook(unittest.TestCase): ...@@ -285,7 +285,7 @@ class TestBook(unittest.TestCase):
name='word_{0}'.format(i), shape=[1], dtype='int64')) name='word_{0}'.format(i), shape=[1], dtype='int64'))
dict_size = 10000 dict_size = 10000
label_word = int(window_size / 2) + 1 label_word = int(window_size // 2) + 1
embs = [] embs = []
for i in range(window_size): for i in range(window_size):
......
...@@ -29,14 +29,14 @@ def max_pool3D_forward_naive(x, ...@@ -29,14 +29,14 @@ def max_pool3D_forward_naive(x,
if global_pool == 1: if global_pool == 1:
ksize = [D, H, W] ksize = [D, H, W]
D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1 D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1
) / strides[0] + 1 if ceil_mode else (H - ksize[0] + 2 * ) // strides[0] + 1 if ceil_mode else (H - ksize[0] + 2 *
paddings[0]) / strides[0] + 1 paddings[0]) // strides[0] + 1
H_out = (H - ksize[1] + 2 * paddings[1] + strides[1] - 1 H_out = (H - ksize[1] + 2 * paddings[1] + strides[1] - 1
) / strides[1] + 1 if ceil_mode else (W - ksize[1] + 2 * ) // strides[1] + 1 if ceil_mode else (W - ksize[1] + 2 *
paddings[1]) / strides[1] + 1 paddings[1]) // strides[1] + 1
W_out = (W - ksize[2] + 2 * paddings[2] + strides[2] - 1 W_out = (W - ksize[2] + 2 * paddings[2] + strides[2] - 1
) / strides[2] + 1 if ceil_mode else (W - ksize[2] + 2 * ) // strides[2] + 1 if ceil_mode else (W - ksize[2] + 2 *
paddings[2]) / strides[2] + 1 paddings[2]) // strides[2] + 1
out = np.zeros((N, C, D_out, H_out, W_out)) out = np.zeros((N, C, D_out, H_out, W_out))
for k in range(D_out): for k in range(D_out):
d_start = np.max((k * strides[0] - paddings[0], 0)) d_start = np.max((k * strides[0] - paddings[0], 0))
...@@ -63,14 +63,14 @@ def avg_pool3D_forward_naive(x, ...@@ -63,14 +63,14 @@ def avg_pool3D_forward_naive(x,
if global_pool == 1: if global_pool == 1:
ksize = [D, H, W] ksize = [D, H, W]
D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1 D_out = (D - ksize[0] + 2 * paddings[0] + strides[0] - 1
) / strides[0] + 1 if ceil_mode else (H - ksize[0] + 2 * ) // strides[0] + 1 if ceil_mode else (H - ksize[0] + 2 *
paddings[0]) / strides[0] + 1 paddings[0]) // strides[0] + 1
H_out = (H - ksize[1] + 2 * paddings[1] + strides[1] - 1 H_out = (H - ksize[1] + 2 * paddings[1] + strides[1] - 1
) / strides[1] + 1 if ceil_mode else (W - ksize[1] + 2 * ) // strides[1] + 1 if ceil_mode else (W - ksize[1] + 2 *
paddings[1]) / strides[1] + 1 paddings[1]) // strides[1] + 1
W_out = (W - ksize[2] + 2 * paddings[2] + strides[2] - 1 W_out = (W - ksize[2] + 2 * paddings[2] + strides[2] - 1
) / strides[2] + 1 if ceil_mode else (W - ksize[2] + 2 * ) // strides[2] + 1 if ceil_mode else (W - ksize[2] + 2 *
paddings[2]) / strides[2] + 1 paddings[2]) // strides[2] + 1
out = np.zeros((N, C, D_out, H_out, W_out)) out = np.zeros((N, C, D_out, H_out, W_out))
for k in range(D_out): for k in range(D_out):
d_start = np.max((k * strides[0] - paddings[0], 0)) d_start = np.max((k * strides[0] - paddings[0], 0))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册